Fix encoding of pointer type in empty interface

This commit is contained in:
Masaaki Goshima 2021-06-29 11:35:37 +09:00
parent 8129998093
commit 1400b498ab
2 changed files with 46 additions and 1 deletions

View File

@ -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))
}

View File

@ -317,7 +317,6 @@ func copyToInterfaceOpcode(code *Opcode) *Opcode {
copied := copyOpcode(code) copied := copyOpcode(code)
c := copied c := copied
c = ToEndCode(c) c = ToEndCode(c)
copied.Op = copied.Op.PtrHeadToHead()
c.Idx += uintptrSize c.Idx += uintptrSize
c.ElemIdx = c.Idx + uintptrSize c.ElemIdx = c.Idx + uintptrSize
c.Length = c.Idx + 2*uintptrSize c.Length = c.Idx + 2*uintptrSize