Fix encoding of empty struct interface type

This commit is contained in:
Masaaki Goshima 2021-09-01 11:41:33 +09:00
parent 4f058093a3
commit 559d70d706
2 changed files with 15 additions and 1 deletions

View File

@ -2134,3 +2134,17 @@ func TestImplementedMethodInterfaceType(t *testing.T) {
t.Fatalf("failed to encode implemented method interface type. expected:[%q] but got:[%q]", expected, got) t.Fatalf("failed to encode implemented method interface type. expected:[%q] but got:[%q]", expected, got)
} }
} }
func TestEmptyStructInterface(t *testing.T) {
expected, err := stdjson.Marshal([]interface{}{struct{}{}})
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal([]interface{}{struct{}{}})
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("failed to encode empty struct interface. expected:[%q] but got:[%q]", expected, got)
}
}

View File

@ -1506,7 +1506,6 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
structEndCode := &Opcode{ structEndCode := &Opcode{
Op: OpStructEnd, Op: OpStructEnd,
Next: newEndOp(ctx),
Type: nil, Type: nil,
Indent: ctx.indent, Indent: ctx.indent,
} }
@ -1531,6 +1530,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
structEndCode.DisplayIdx = ctx.opcodeIndex structEndCode.DisplayIdx = ctx.opcodeIndex
structEndCode.Idx = opcodeOffset(ctx.ptrIndex) structEndCode.Idx = opcodeOffset(ctx.ptrIndex)
ctx.incIndex() ctx.incIndex()
structEndCode.Next = newEndOp(ctx)
if prevField != nil && prevField.NextField == nil { if prevField != nil && prevField.NextField == nil {
prevField.NextField = structEndCode prevField.NextField = structEndCode