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

support for empty slce, Same behavior as json.Unmarshal
master
aimuz 3 years ago committed by GitHub
parent 278cd123c8
commit 70e51bb352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -450,6 +450,12 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
refValue := reflect.ValueOf(mapValue) refValue := reflect.ValueOf(mapValue)
conv := reflect.MakeSlice(reflect.SliceOf(baseType), refValue.Len(), refValue.Cap()) 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 var valid bool
for i := 0; i < refValue.Len(); i++ { for i := 0; i < refValue.Len(); i++ {
ithValue := refValue.Index(i).Interface() ithValue := refValue.Index(i).Interface()

@ -330,28 +330,34 @@ func TestUnmarshalFloat(t *testing.T) {
func TestUnmarshalInt64Slice(t *testing.T) { func TestUnmarshalInt64Slice(t *testing.T) {
var v struct { var v struct {
Ages []int64 `key:"ages"` Ages []int64 `key:"ages"`
Slice []int64 `key:"slice"`
} }
m := map[string]interface{}{ m := map[string]interface{}{
"ages": []int64{1, 2}, "ages": []int64{1, 2},
"slice": []interface{}{},
} }
ast := assert.New(t) ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v)) ast.Nil(UnmarshalKey(m, &v))
ast.ElementsMatch([]int64{1, 2}, v.Ages) ast.ElementsMatch([]int64{1, 2}, v.Ages)
ast.Equal([]int64{}, v.Slice)
} }
func TestUnmarshalIntSlice(t *testing.T) { func TestUnmarshalIntSlice(t *testing.T) {
var v struct { var v struct {
Ages []int `key:"ages"` Ages []int `key:"ages"`
Slice []int `key:"slice"`
} }
m := map[string]interface{}{ m := map[string]interface{}{
"ages": []int{1, 2}, "ages": []int{1, 2},
"slice": []interface{}{},
} }
ast := assert.New(t) ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v)) ast.Nil(UnmarshalKey(m, &v))
ast.ElementsMatch([]int{1, 2}, v.Ages) ast.ElementsMatch([]int{1, 2}, v.Ages)
ast.Equal([]int{}, v.Slice)
} }
func TestUnmarshalString(t *testing.T) { func TestUnmarshalString(t *testing.T) {

Loading…
Cancel
Save