From 1400b498ab8a56ec6628db967780a752ea746837 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 29 Jun 2021 11:35:37 +0900 Subject: [PATCH] Fix encoding of pointer type in empty interface --- encode_test.go | 46 ++++++++++++++++++++++++++++++++++++++ internal/encoder/opcode.go | 1 - 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/encode_test.go b/encode_test.go index 41a14cc..6167f20 100644 --- a/encode_test.go +++ b/encode_test.go @@ -1958,3 +1958,49 @@ func TestEncodeContextOption(t *testing.T) { } }) } + +func TestInterfaceWithPointer(t *testing.T) { + var ( + ivalue int = 10 + uvalue uint = 20 + svalue string = "value" + bvalue bool = true + fvalue float32 = 3.14 + nvalue json.Number = "1.23" + structv = struct{ A int }{A: 10} + slice = []int{1, 2, 3, 4} + array = [4]int{1, 2, 3, 4} + mapvalue = map[string]int{"a": 1} + ) + data := map[string]interface{}{ + "ivalue": ivalue, + "uvalue": uvalue, + "svalue": svalue, + "bvalue": bvalue, + "fvalue": fvalue, + "nvalue": nvalue, + "struct": structv, + "slice": slice, + "array": array, + "map": mapvalue, + "pivalue": &ivalue, + "puvalue": &uvalue, + "psvalue": &svalue, + "pbvalue": &bvalue, + "pfvalue": &fvalue, + "pnvalue": &nvalue, + "pstruct": &structv, + "pslice": &slice, + "parray": &array, + "pmap": &mapvalue, + } + expected, err := stdjson.Marshal(data) + if err != nil { + t.Fatal(err) + } + actual, err := json.Marshal(data) + if err != nil { + t.Fatal(err) + } + assertEq(t, "interface{}", string(expected), string(actual)) +} diff --git a/internal/encoder/opcode.go b/internal/encoder/opcode.go index 37dd3f5..c23a90b 100644 --- a/internal/encoder/opcode.go +++ b/internal/encoder/opcode.go @@ -317,7 +317,6 @@ func copyToInterfaceOpcode(code *Opcode) *Opcode { copied := copyOpcode(code) c := copied c = ToEndCode(c) - copied.Op = copied.Op.PtrHeadToHead() c.Idx += uintptrSize c.ElemIdx = c.Idx + uintptrSize c.Length = c.Idx + 2*uintptrSize