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 marshal tinyint - optimization #294

Merged
Merged
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
33 changes: 27 additions & 6 deletions marshal/tinyint/marshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,34 @@ func EncStringR(v *string) ([]byte, error) {
}

func EncReflect(v reflect.Value) ([]byte, error) {
switch v.Type().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())
switch v.Kind() {
case reflect.Int8:
return []byte{byte(v.Int())}, nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16:
val := v.Int()
if val > math.MaxInt8 || val < math.MinInt8 {
return nil, fmt.Errorf("failed to marshal tinyint: value %#v out of range", v.Interface())
}
return []byte{byte(val)}, nil
case reflect.Uint8:
return []byte{byte(v.Uint())}, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16:
val := v.Uint()
if val > math.MaxUint8 {
return nil, fmt.Errorf("failed to marshal tinyint: value %#v out of range", v.Interface())
}
return []byte{byte(val)}, nil
case reflect.String:
return EncString(v.String())
val := v.String()
if val == "" {
return nil, nil
}

n, err := strconv.ParseInt(val, 10, 8)
if err != nil {
return nil, fmt.Errorf("failed to marshal tinyint: can not marshal %#v %s", v.Interface(), err)
}
return []byte{byte(n)}, nil
default:
return nil, fmt.Errorf("failed to marshal tinyint: unsupported value type (%T)(%#[1]v)", v.Interface())
}
Expand Down
2 changes: 1 addition & 1 deletion marshal/tinyint/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Unmarshal(data []byte, value interface{}) error {
rv := reflect.ValueOf(value)
rt := rv.Type()
if rt.Kind() != reflect.Ptr {
return fmt.Errorf("failed to unmarshal tinyint: unsupported value type (%T)(%#[1]v)", value)
return fmt.Errorf("failed to unmarshal tinyint: unsupported value type %#v", value)
}
if rt.Elem().Kind() != reflect.Ptr {
return DecReflect(data, rv)
Expand Down
Loading
Loading