Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: confusing nil in direct interface with typed nil #377

Merged
merged 2 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions encode_test.go
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)
}
}
10 changes: 7 additions & 3 deletions internal/cmd/generator/vm.go.tmpl
Expand Up @@ -3,6 +3,7 @@ package vm

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

Expand Down Expand Up @@ -194,9 +195,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ
}
if ifacePtr == nil {
b = appendNullComma(ctx, b)
code = code.Next
break
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b)
code = code.Next
break
}
}
ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
Expand Down
10 changes: 7 additions & 3 deletions internal/encoder/vm/vm.go

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

10 changes: 7 additions & 3 deletions internal/encoder/vm_color/vm.go

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

10 changes: 7 additions & 3 deletions internal/encoder/vm_color_indent/vm.go

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

10 changes: 7 additions & 3 deletions internal/encoder/vm_indent/vm.go

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