Merge pull request #377 from orisano/fix/#376

Fix encoding of directed interface with typed nil
This commit is contained in:
Masaaki Goshima 2022-07-04 14:07:04 +09:00 committed by GitHub
commit 1468eefb01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 15 deletions

View File

@ -2406,3 +2406,21 @@ func TestIssue339(t *testing.T) {
t.Errorf("unexpected result: %v != %v", got, expected) t.Errorf("unexpected result: %v != %v", got, expected)
} }
} }
func TestIssue376(t *testing.T) {
type Container struct {
V interface{} `json:"value"`
}
type MapOnly struct {
Map map[string]int64 `json:"map"`
}
b, err := json.Marshal(Container{MapOnly{}})
if err != nil {
t.Fatal(err)
}
got := string(b)
expected := `{"value":{"map":null}}`
if got != expected {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}

View File

@ -3,6 +3,7 @@ package vm
import ( import (
"math" "math"
"reflect"
"sort" "sort"
"unsafe" "unsafe"
@ -194,10 +195,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ typ = iface.typ
} }
if ifacePtr == nil { if ifacePtr == nil {
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b) b = appendNullComma(ctx, b)
code = code.Next code = code.Next
break break
} }
}
ctx.KeepRefs = append(ctx.KeepRefs, up) ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ))) ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package vm
import ( import (
"math" "math"
"reflect"
"sort" "sort"
"unsafe" "unsafe"
@ -194,10 +195,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ typ = iface.typ
} }
if ifacePtr == nil { if ifacePtr == nil {
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b) b = appendNullComma(ctx, b)
code = code.Next code = code.Next
break break
} }
}
ctx.KeepRefs = append(ctx.KeepRefs, up) ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ))) ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package vm_color
import ( import (
"math" "math"
"reflect"
"sort" "sort"
"unsafe" "unsafe"
@ -194,10 +195,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ typ = iface.typ
} }
if ifacePtr == nil { if ifacePtr == nil {
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b) b = appendNullComma(ctx, b)
code = code.Next code = code.Next
break break
} }
}
ctx.KeepRefs = append(ctx.KeepRefs, up) ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ))) ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package vm_color_indent
import ( import (
"math" "math"
"reflect"
"sort" "sort"
"unsafe" "unsafe"
@ -194,10 +195,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ typ = iface.typ
} }
if ifacePtr == nil { if ifacePtr == nil {
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b) b = appendNullComma(ctx, b)
code = code.Next code = code.Next
break break
} }
}
ctx.KeepRefs = append(ctx.KeepRefs, up) ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ))) ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package vm_indent
import ( import (
"math" "math"
"reflect"
"sort" "sort"
"unsafe" "unsafe"
@ -194,10 +195,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ typ = iface.typ
} }
if ifacePtr == nil { if ifacePtr == nil {
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b) b = appendNullComma(ctx, b)
code = code.Next code = code.Next
break break
} }
}
ctx.KeepRefs = append(ctx.KeepRefs, up) ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ))) ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
if err != nil { if err != nil {