From ac9a7dd8e30a998880b16813b7f23409c256e737 Mon Sep 17 00:00:00 2001 From: peterlimg Date: Tue, 20 Jul 2021 23:14:26 +1000 Subject: [PATCH] Fix encode issue for embed struct with tag --- encode_test.go | 53 ++++++++++++++++++++++++++ internal/cmd/generator/vm.go.tmpl | 6 ++- internal/encoder/vm/vm.go | 6 ++- internal/encoder/vm_color/vm.go | 6 ++- internal/encoder/vm_color_indent/vm.go | 6 ++- internal/encoder/vm_indent/vm.go | 6 ++- 6 files changed, 73 insertions(+), 10 deletions(-) diff --git a/encode_test.go b/encode_test.go index 5b271b0..98f71b0 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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"` diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl index 78da5e4..f2c992a 100644 --- a/internal/cmd/generator/vm.go.tmpl +++ b/internal/cmd/generator/vm.go.tmpl @@ -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 diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index 78da5e4..f2c992a 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -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 diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go index 0d4c472..70de3e7 100644 --- a/internal/encoder/vm_color/vm.go +++ b/internal/encoder/vm_color/vm.go @@ -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 diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go index de8b858..2d014e5 100644 --- a/internal/encoder/vm_color_indent/vm.go +++ b/internal/encoder/vm_color_indent/vm.go @@ -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 diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index d10ce75..25d2ebc 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -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