Fix encoding of nested map

This commit is contained in:
Masaaki Goshima 2021-03-15 13:29:08 +09:00
parent 3ee1ab2711
commit c75b9e57e2
4 changed files with 34 additions and 2 deletions

View File

@ -50,6 +50,10 @@ func TestCoverMap(t *testing.T) {
name string
data interface{}
}{
{
name: "NestedMap",
data: map[string]map[string]int{"a": {"b": 1}},
},
// HeadMapZero
{
name: "HeadMapZero",

View File

@ -726,8 +726,7 @@ func encodeCompileMap(ctx *encodeCompileContext) (*opcode, error) {
value := newMapValueCode(ctx, header)
ctx.incIndex()
valueType := typ.Elem()
valueCode, err := encodeCompile(ctx.withType(valueType), false)
valueCode, err := encodeCompileMapValue(ctx.withType(typ.Elem()))
if err != nil {
return nil, err
}
@ -756,6 +755,15 @@ func encodeCompileMap(ctx *encodeCompileContext) (*opcode, error) {
return (*opcode)(unsafe.Pointer(header)), nil
}
func encodeCompileMapValue(ctx *encodeCompileContext) (*opcode, error) {
switch ctx.typ.Kind() {
case reflect.Map:
return encodeCompilePtr(ctx.withType(rtype_ptrTo(ctx.typ)))
default:
return encodeCompile(ctx, false)
}
}
func encodeTypeToHeaderType(code *opcode) opType {
switch code.op {
case opInt:

View File

@ -278,6 +278,16 @@ func encodeRunEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
b = append(b, ']', ',', '\n')
code = code.end.next
}
case opMapPtr:
p := load(ctxptr, code.idx)
if p == 0 {
b = encodeNull(b)
b = encodeComma(b)
code = code.end.next
break
}
store(ctxptr, code.idx, ptrToPtr(p))
fallthrough
case opMap:
ptr := load(ctxptr, code.idx)
if ptr == 0 {

View File

@ -278,6 +278,16 @@ func encodeRunIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, op
b = append(b, ']', ',', '\n')
code = code.end.next
}
case opMapPtr:
p := load(ctxptr, code.idx)
if p == 0 {
b = encodeNull(b)
b = encodeComma(b)
code = code.end.next
break
}
store(ctxptr, code.idx, ptrToPtr(p))
fallthrough
case opMap:
ptr := load(ctxptr, code.idx)
if ptr == 0 {