Skip to content

Commit

Permalink
fix: confusing nil in direct interface with typed nil
Browse files Browse the repository at this point in the history
  • Loading branch information
orisano committed Jul 2, 2022
1 parent 27bd0f2 commit c8d6da8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
18 changes: 18 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2406,3 +2406,21 @@ func TestIssue339(t *testing.T) {
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)
}
}
4 changes: 3 additions & 1 deletion internal/cmd/generator/vm.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm

import (
"math"
"reflect"
"sort"
"unsafe"

Expand Down Expand Up @@ -193,7 +194,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
ifacePtr = iface.ptr
typ = iface.typ
}
if ifacePtr == nil {
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if ifacePtr == nil && !isDirectedNil {
b = appendNullComma(ctx, b)
code = code.Next
break
Expand Down
4 changes: 3 additions & 1 deletion internal/encoder/vm/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/encoder/vm_color/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/encoder/vm_color_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/encoder/vm_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c8d6da8

Please sign in to comment.