Skip to content

Commit

Permalink
fix: back comp for omitnil && omitempty (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
sesky4 committed Mar 14, 2024
1 parent daa26f0 commit 579b857
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions tencentcloud/common/json/encode.go
Expand Up @@ -640,7 +640,7 @@ func (se *structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
first := true
for i, f := range se.fields {
fv := fieldByIndex(v, f.index)
if !fv.IsValid() || f.omitNil && isNilValue(fv) {
if !fv.IsValid() || (f.omitNil || f.omitEmpty) && isNilValue(fv) {
continue
}
if first {
Expand Down Expand Up @@ -1043,11 +1043,12 @@ type field struct {
nameBytes []byte // []byte(name)
equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent

tag bool
index []int
typ reflect.Type
omitNil bool
quoted bool
tag bool
index []int
typ reflect.Type
omitEmpty bool
omitNil bool
quoted bool
}

func fillField(f field) field {
Expand Down Expand Up @@ -1160,12 +1161,13 @@ func typeFields(t reflect.Type) []field {
name = sf.Name
}
fields = append(fields, fillField(field{
name: name,
tag: tagged,
index: index,
typ: ft,
omitNil: opts.Contains("omitnil"),
quoted: quoted,
name: name,
tag: tagged,
index: index,
typ: ft,
omitEmpty: opts.Contains("omitempty"),
omitNil: opts.Contains("omitnil"),
quoted: quoted,
}))
if count[f.typ] > 1 {
// If there were multiple instances, add a second,
Expand Down

0 comments on commit 579b857

Please sign in to comment.