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 {
ithVal := slice.Index(index)
switch v := value.(type) {
case json.Number:
return setValue(baseKind, slice.Index(index), v.String())
return setValue(baseKind, ithVal, v.String())
default:
// 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.
if slice.Index(index).Kind() == reflect.Ptr {
baseType := Deref(slice.Index(index).Type())
if ithVal.Kind() == reflect.Ptr {
baseType := Deref(ithVal.Type())
if baseType.Kind() != reflect.TypeOf(value).Kind() {
return errTypeMismatch
}
target := reflect.New(baseType).Elem()
target.Set(reflect.ValueOf(value))
slice.Index(index).Set(target.Addr())
ithVal.Set(target.Addr())
return nil
} else {
if slice.Index(index).Kind() != reflect.TypeOf(value).Kind() {
if ithVal.Kind() != reflect.TypeOf(value).Kind() {
return errTypeMismatch
}
slice.Index(index).Set(reflect.ValueOf(value))
ithVal.Set(reflect.ValueOf(value))
return nil
}
}

Loading…
Cancel
Save