Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshal, unmarshal. For many cql types unmarshaling data that have smaller len than cql type, do not return an error. #252

Open
illia-li opened this issue Aug 29, 2024 · 0 comments
Assignees
Labels

Comments

@illia-li
Copy link

Currently the functions for Unmarshaling many cql types, do not return an error, then the data has a smaller length than the cql type.
Type list: TypeSmallInt, TypeInt, TypeBigInt, TypeCounter, TypeDouble, TypeFloat, TypeInet, TypeTime.

Test code:

func TestMarshalSmallerData(t *testing.T) {
	types := []TypeInfo{
		NativeType{proto: 2, typ: TypeSmallInt},
		NativeType{proto: 2, typ: TypeInt},
		NativeType{proto: 2, typ: TypeBigInt},
		NativeType{proto: 2, typ: TypeCounter},
		NativeType{proto: 2, typ: TypeDouble},
		NativeType{proto: 2, typ: TypeFloat},
		NativeType{proto: 2, typ: TypeInet},
		NativeType{proto: 2, typ: TypeTime},
		NativeType{proto: 2, typ: TypeDate},
		NativeType{proto: 2, typ: TypeTimestamp},
		NativeType{proto: 2, typ: TypeDuration},
		NativeType{proto: 2, typ: TypeDecimal},
		NativeType{proto: 2, typ: TypeUUID},
		NativeType{proto: 2, typ: TypeTimeUUID},
	}

	errPanic := fmt.Errorf("was panic")

	wrappedUnmarshal := func(info TypeInfo, data []byte, value interface{}) (err error) {
		defer func() {
			if p := recover(); p != nil {
				err = errors.Join(errPanic, p.(error))
			}
		}()
		return Unmarshal(info, data, value)
	}

	noErrs := make([]string, 0)
	panics := make([]string, 0)
	for _, tp := range types {
		data := []byte{0}
		val := tp.New()
		if err := wrappedUnmarshal(tp, data, val); err != nil {
			if errors.Is(err, errPanic) {
				panics = append(panics, fmt.Sprintf("type:<%s> - panic: <%v>", tp.Type().String(), err))
			}
			continue
		}
		if val != nil {
			val = dereference(val)
		}
		noErrs = append(noErrs, fmt.Sprintf("type:<%s> - value: <%v>", tp.Type().String(), val))
	}
	if len(noErrs) > 0 {
		t.Errorf("\nOut of %d test cases, %d cases without errors:\n%s", len(types), len(noErrs), strings.Join(noErrs, "\n"))
	}
	if len(panics) > 0 {
		t.Errorf("\nOut of %d test cases, %d cases with panic:\n%s", len(types), len(panics), strings.Join(panics, "\n"))
	}
}

Output:

=== RUN   TestUnmarshalSmallerData
    marshal_0_all_test.go:189: 
        Out of 14 test cases, 9 cases without errors:
        type:<smallint> - value: <0>
        type:<int> - value: <0>
        type:<bigint> - value: <0>
        type:<counter> - value: <0>
        type:<double> - value: <0>
        type:<float> - value: <0>
        type:<inet> - value: <?00>
        type:<time> - value: <0s>
        type:<timestamp> - value: <1970-01-01 00:00:00 +0000 UTC>
    marshal_0_all_test.go:192: 
        Out of 14 test cases, 1 cases with panic:
        type:<date> - panic: <was panic
        runtime error: index out of range [3] with length 1>
--- FAIL: TestUnmarshalSmallerData (0.00s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants