fix: empty slice are set to nil (#1702)

support for empty slce, Same behavior as json.Unmarshal
This commit is contained in:
aimuz
2022-03-24 21:41:38 +08:00
committed by GitHub
parent 278cd123c8
commit 70e51bb352
2 changed files with 16 additions and 4 deletions

View File

@@ -450,6 +450,12 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
refValue := reflect.ValueOf(mapValue)
conv := reflect.MakeSlice(reflect.SliceOf(baseType), refValue.Len(), refValue.Cap())
// support for empty slice
if !refValue.IsNil() && refValue.Len() == 0 {
value.Set(conv)
return nil
}
var valid bool
for i := 0; i < refValue.Len(); i++ {
ithValue := refValue.Index(i).Interface()