Skip to content

Commit

Permalink
refactor mapping (zeromicro#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jun 23, 2021
1 parent 01c92a6 commit 9ccb997
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 9ccb997

Please sign in to comment.