mirror of https://github.com/goccy/go-json.git
Fix the case where the embedded field is at the end
This commit is contained in:
parent
8923f69dd5
commit
0940ff3198
|
@ -2358,3 +2358,32 @@ func TestEmptyStringMarshaler(t *testing.T) {
|
|||
assertErr(t, err)
|
||||
assertEq(t, "struct", string(expected), string(got))
|
||||
}
|
||||
|
||||
func TestIssue324(t *testing.T) {
|
||||
type T struct {
|
||||
FieldA *string `json:"fieldA,omitempty"`
|
||||
FieldB *string `json:"fieldB,omitempty"`
|
||||
FieldC *bool `json:"fieldC"`
|
||||
FieldD []string `json:"fieldD,omitempty"`
|
||||
}
|
||||
v := &struct {
|
||||
Code string `json:"code"`
|
||||
*T
|
||||
}{
|
||||
T: &T{},
|
||||
}
|
||||
var sv = "Test Field"
|
||||
v.Code = "Test"
|
||||
v.T.FieldB = &sv
|
||||
expected, err := stdjson.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(expected, got) {
|
||||
t.Fatalf("failed to encode. expected %q but got %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -436,12 +436,13 @@ func (c *StructCode) ToOpcode(ctx *compileContext) Opcodes {
|
|||
}
|
||||
if isEndField {
|
||||
endField := fieldCodes.Last()
|
||||
if len(codes) > 0 {
|
||||
codes.First().End = endField
|
||||
} else if field.isAnonymous {
|
||||
if field.isAnonymous {
|
||||
firstField.End = endField
|
||||
lastField := c.lastAnonymousFieldCode(firstField)
|
||||
lastField.NextField = endField
|
||||
}
|
||||
if len(codes) > 0 {
|
||||
codes.First().End = endField
|
||||
} else {
|
||||
firstField.End = endField
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue