Merge pull request #265 from peterlimg/master

Fix encode issue for embed struct with tags
This commit is contained in:
Masaaki Goshima 2021-08-12 12:10:40 +09:00 committed by GitHub
commit a1780c18a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 11 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

@ -170,6 +170,7 @@ RETRY:
s.buf = append(s.buf[:s.cursor-1], s.buf[s.cursor:]...)
s.length--
s.cursor--
p = s.bufptr()
return p, nil
}

View File

@ -1417,7 +1417,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
valueCode = code
}
if field.Anonymous {
if field.Anonymous && !tag.IsTaggedKey {
tagKey := ""
if tag.IsTaggedKey {
tagKey = tag.Key
@ -1425,6 +1425,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
for k, v := range anonymousStructFieldPairMap(tags, tagKey, valueCode) {
anonymousFields[k] = append(anonymousFields[k], v...)
}
valueCode.decIndent()
// fix issue144

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