refactor mapping (#782)

master v1.1.8
Kevin Wan 3 years ago committed by GitHub
parent 01c92a6bc5
commit 9ccb997ed8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -496,28 +496,29 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.
} }
func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind reflect.Kind, value interface{}) error { func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind reflect.Kind, value interface{}) error {
ithVal := slice.Index(index)
switch v := value.(type) { switch v := value.(type) {
case json.Number: case json.Number:
return setValue(baseKind, slice.Index(index), v.String()) return setValue(baseKind, ithVal, v.String())
default: default:
// don't need to consider the difference between int, int8, int16, int32, int64, // don't need to consider the difference between int, int8, int16, int32, int64,
// uint, uint8, uint16, uint32, uint64, because they're handled as json.Number. // uint, uint8, uint16, uint32, uint64, because they're handled as json.Number.
if ithVal.Kind() == reflect.Ptr {
if slice.Index(index).Kind() == reflect.Ptr { baseType := Deref(ithVal.Type())
baseType := Deref(slice.Index(index).Type())
if baseType.Kind() != reflect.TypeOf(value).Kind() { if baseType.Kind() != reflect.TypeOf(value).Kind() {
return errTypeMismatch return errTypeMismatch
} }
target := reflect.New(baseType).Elem() target := reflect.New(baseType).Elem()
target.Set(reflect.ValueOf(value)) target.Set(reflect.ValueOf(value))
slice.Index(index).Set(target.Addr()) ithVal.Set(target.Addr())
return nil return nil
} else { } else {
if slice.Index(index).Kind() != reflect.TypeOf(value).Kind() { if ithVal.Kind() != reflect.TypeOf(value).Kind() {
return errTypeMismatch return errTypeMismatch
} }
slice.Index(index).Set(reflect.ValueOf(value)) ithVal.Set(reflect.ValueOf(value))
return nil return nil
} }
} }

Loading…
Cancel
Save