Fix encoding of embedded struct that isn't first field

This commit is contained in:
Masaaki Goshima 2021-08-12 13:03:34 +09:00
parent a1780c18a6
commit 104829e78f
6 changed files with 27 additions and 5 deletions

View File

@ -2086,3 +2086,25 @@ func TestIssue263(t *testing.T) {
t.Fatalf("expected:[%s] but got:[%s]", string(expected), string(actual)) t.Fatalf("expected:[%s] but got:[%s]", string(expected), string(actual))
} }
} }
func TestEmbeddedNotFirstField(t *testing.T) {
type Embedded struct {
Has bool `json:"has"`
}
type T struct {
X int `json:"is"`
Embedded `json:"child"`
}
p := T{X: 10, Embedded: Embedded{Has: true}}
expected, err := stdjson.Marshal(&p)
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal(&p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("failed to encode embedded structure. expected = %q but got %q", expected, got)
}
}

View File

@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.Next code = code.Next
} }
case encoder.OpStructField: case encoder.OpStructField:
if code.Flags&encoder.AnonymousKeyFlags == 0 { if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
} }
p := load(ctxptr, code.Idx) + uintptr(code.Offset) p := load(ctxptr, code.Idx) + uintptr(code.Offset)

View File

@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.Next code = code.Next
} }
case encoder.OpStructField: case encoder.OpStructField:
if code.Flags&encoder.AnonymousKeyFlags == 0 { if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
} }
p := load(ctxptr, code.Idx) + uintptr(code.Offset) p := load(ctxptr, code.Idx) + uintptr(code.Offset)

View File

@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.Next code = code.Next
} }
case encoder.OpStructField: case encoder.OpStructField:
if code.Flags&encoder.AnonymousKeyFlags == 0 { if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
} }
p := load(ctxptr, code.Idx) + uintptr(code.Offset) p := load(ctxptr, code.Idx) + uintptr(code.Offset)

View File

@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.Next code = code.Next
} }
case encoder.OpStructField: case encoder.OpStructField:
if code.Flags&encoder.AnonymousKeyFlags == 0 { if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
} }
p := load(ctxptr, code.Idx) + uintptr(code.Offset) p := load(ctxptr, code.Idx) + uintptr(code.Offset)

View File

@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.Next code = code.Next
} }
case encoder.OpStructField: case encoder.OpStructField:
if code.Flags&encoder.AnonymousKeyFlags == 0 { if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
} }
p := load(ctxptr, code.Idx) + uintptr(code.Offset) p := load(ctxptr, code.Idx) + uintptr(code.Offset)