Skip to content

Commit

Permalink
codec: fix omitempty when array has all zero values (even if len != 0)
Browse files Browse the repository at this point in the history
An array is empty if it has all zero values (even if len != 0)

Previously, we treated an array as empty only if len == 0. That is incorrect.

Instead, an array is empty if it is equal to its zero value.

This is now honored, making omitEmpty work effectively for arrays also.
  • Loading branch information
ugorji committed Dec 19, 2022
1 parent fb4413c commit ebaaab4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion codec/helper_not_unsafe.go
Expand Up @@ -114,8 +114,19 @@ func isEmptyValue(v reflect.Value, tinfos *TypeInfos, recursive bool) bool {
switch v.Kind() {
case reflect.Invalid:
return true
case reflect.Array, reflect.String:
case reflect.String:
return v.Len() == 0
case reflect.Array:
// zero := reflect.Zero(v.Type().Elem())
// can I just check if the whole value is equal to zeros? seems not.
// can I just check if the whole value is equal to its zero value? no.
// Well, then we check if each value is empty without recursive.
for i, vlen := 0, v.Len(); i < vlen; i++ {
if !isEmptyValue(v.Index(i), tinfos, false) {
return false
}
}
return true
case reflect.Map, reflect.Slice, reflect.Chan:
return v.IsNil() || v.Len() == 0
case reflect.Bool:
Expand Down
6 changes: 5 additions & 1 deletion codec/helper_unsafe.go
Expand Up @@ -463,7 +463,11 @@ func isEmptyValueFallbackRecur(urv *unsafeReflectValue, v reflect.Value, tinfos
case reflect.Map:
return urv.ptr == nil || len_map(rvRefPtr(urv)) == 0
case reflect.Array:
return v.Len() == 0
return v.Len() == 0 ||
urv.ptr == nil ||
urv.typ == nil ||
rtsize2(urv.typ) == 0 ||
unsafeCmpZero(urv.ptr, int(rtsize2(urv.typ)))
}
return false
}
Expand Down

0 comments on commit ebaaab4

Please sign in to comment.