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

Fix int like cql types with negative range, add error when processing uint like go types #306

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ import (
"gopkg.in/inf.v0"
)

type AliasInt int
type AliasUint uint
type AliasUint8 uint8
type AliasUint16 uint16
type AliasUint32 uint32
type AliasUint64 uint64

var marshalTests = []struct {
Info TypeInfo
Data []byte
Expand Down Expand Up @@ -699,76 +692,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
uint8(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
uint64(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
uint32(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
uint16(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
uint(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
AliasUint8(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
AliasUint64(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
AliasUint32(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
AliasUint16(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\xff"),
AliasUint(255),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBlob},
[]byte(nil),
Expand Down
22 changes: 19 additions & 3 deletions serialization/bigint/marshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func EncUint32R(v *uint32) ([]byte, error) {
}

func EncUint64(v uint64) ([]byte, error) {
return []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}, nil
if v > math.MaxInt64 {
return nil, fmt.Errorf("failed to marshal bigint: value %#v out of range", v)
}
return encUint64(v), nil
}

func EncUint64R(v *uint64) ([]byte, error) {
Expand All @@ -122,6 +125,9 @@ func EncUint64R(v *uint64) ([]byte, error) {
}

func EncUint(v uint) ([]byte, error) {
if v > math.MaxInt64 {
return nil, fmt.Errorf("failed to marshal bigint: value %#v out of range", v)
}
return []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}, nil
}

Expand Down Expand Up @@ -172,8 +178,14 @@ func EncReflect(v reflect.Value) ([]byte, error) {
switch v.Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return EncInt64(v.Int())
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
return EncUint64(v.Uint())
case reflect.Uint32, reflect.Uint16, reflect.Uint8:
return encUint64(v.Uint()), nil
case reflect.Uint, reflect.Uint64:
val := v.Uint()
if val > math.MaxInt64 {
return nil, fmt.Errorf("failed to marshal bigint: value (%T)(%[1]v) out of range", v.Interface())
}
return encUint64(val), nil
Comment on lines +183 to +188
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just call EncUint64() here? there is the MaxInt64 check there so no need to duplicate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To track exactly where the error occurred, some differences are needed in the errors.
In this case, these differences - the type of value (%T)(%[1]v).
If you do not do this, it will be more difficult to track the location of the error.

For me, if the content of all errors included the name of the function and something like a stack trace , this would be an ideal option.

case reflect.String:
val := v.String()
if val == "" {
Expand All @@ -199,3 +211,7 @@ func EncReflectR(v reflect.Value) ([]byte, error) {
func encInt64(v int64) []byte {
return []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
}

func encUint64(v uint64) []byte {
return []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
}
Loading
Loading