Skip to content

Commit

Permalink
private/protocol/json/jsonutil: avoid unnecessary allocation in unmar…
Browse files Browse the repository at this point in the history
…shalScalar (#2044)
  • Loading branch information
bpot authored and xibz committed Jul 11, 2018
1 parent c01ebd0 commit 2fa26aa
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions private/protocol/json/jsonutil/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ func unmarshalMap(value reflect.Value, data interface{}, tag reflect.StructTag)
}

func unmarshalScalar(value reflect.Value, data interface{}, tag reflect.StructTag) error {
errf := func() error {
return fmt.Errorf("unsupported value: %v (%s)", value.Interface(), value.Type())
}

switch d := data.(type) {
case nil:
Expand All @@ -197,7 +194,7 @@ func unmarshalScalar(value reflect.Value, data interface{}, tag reflect.StructTa
}
value.Set(reflect.ValueOf(v))
default:
return errf()
return fmt.Errorf("unsupported value: %v (%s)", value.Interface(), value.Type())
}
case float64:
switch value.Interface().(type) {
Expand All @@ -210,14 +207,14 @@ func unmarshalScalar(value reflect.Value, data interface{}, tag reflect.StructTa
t := time.Unix(int64(d), 0).UTC()
value.Set(reflect.ValueOf(&t))
default:
return errf()
return fmt.Errorf("unsupported value: %v (%s)", value.Interface(), value.Type())
}
case bool:
switch value.Interface().(type) {
case *bool:
value.Set(reflect.ValueOf(&d))
default:
return errf()
return fmt.Errorf("unsupported value: %v (%s)", value.Interface(), value.Type())
}
default:
return fmt.Errorf("unsupported JSON value (%v)", data)
Expand Down

0 comments on commit 2fa26aa

Please sign in to comment.