Fix encode issue for embed struct with tag

This commit is contained in:
peterlimg 2021-07-20 23:14:26 +10:00
parent 85a5597342
commit ac9a7dd8e3
No known key found for this signature in database
GPG Key ID: 4F031F0B25AD570C
6 changed files with 73 additions and 10 deletions

View File

@ -191,6 +191,59 @@ func Test_Marshal(t *testing.T) {
})
})
})
t.Run("embedded with tag", func(t *testing.T) {
type T struct {
A string `json:"a"`
}
type U struct {
*T `json:"t"`
B string `json:"b"`
}
type T2 struct {
A string `json:"a,omitempty"`
}
type U2 struct {
*T2 `json:"t,omitempty"`
B string `json:"b,omitempty"`
}
t.Run("exists field", func(t *testing.T) {
bytes, err := json.Marshal(&U{
T: &T{
A: "aaa",
},
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"t":{"a":"aaa"},"b":"bbb"}`, string(bytes))
t.Run("omitempty", func(t *testing.T) {
bytes, err := json.Marshal(&U2{
T2: &T2{
A: "aaa",
},
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"t":{"a":"aaa"},"b":"bbb"}`, string(bytes))
})
})
t.Run("none field", func(t *testing.T) {
bytes, err := json.Marshal(&U{
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"t":null,"b":"bbb"}`, string(bytes))
t.Run("omitempty", func(t *testing.T) {
bytes, err := json.Marshal(&U2{
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"b":"bbb"}`, string(bytes))
})
})
})
t.Run("omitempty", func(t *testing.T) {
type T struct {
A int `json:",omitempty"`

View File

@ -578,8 +578,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.AnonymousHeadFlags == 0 {
b = appendStructHead(ctx, b)
}
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
b = appendStructKey(ctx, code, b)
if len(code.Key) > 0 {
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b)
}
}
p += uintptr(code.Offset)
code = code.Next

View File

@ -578,8 +578,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.AnonymousHeadFlags == 0 {
b = appendStructHead(ctx, b)
}
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
b = appendStructKey(ctx, code, b)
if len(code.Key) > 0 {
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b)
}
}
p += uintptr(code.Offset)
code = code.Next

View File

@ -578,8 +578,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.AnonymousHeadFlags == 0 {
b = appendStructHead(ctx, b)
}
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
b = appendStructKey(ctx, code, b)
if len(code.Key) > 0 {
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b)
}
}
p += uintptr(code.Offset)
code = code.Next

View File

@ -578,8 +578,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.AnonymousHeadFlags == 0 {
b = appendStructHead(ctx, b)
}
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
b = appendStructKey(ctx, code, b)
if len(code.Key) > 0 {
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b)
}
}
p += uintptr(code.Offset)
code = code.Next

View File

@ -578,8 +578,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if code.Flags&encoder.AnonymousHeadFlags == 0 {
b = appendStructHead(ctx, b)
}
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
b = appendStructKey(ctx, code, b)
if len(code.Key) > 0 {
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b)
}
}
p += uintptr(code.Offset)
code = code.Next