Skip to content

Commit

Permalink
fix: change isPtr to true on listElemCode
Browse files Browse the repository at this point in the history
fix #370
  • Loading branch information
orisano committed Jul 4, 2022
1 parent 1468eef commit fb71671
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2424,3 +2424,32 @@ func TestIssue376(t *testing.T) {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}

type Issue370 struct {
String string
Valid bool
}

func (i *Issue370) MarshalJSON() ([]byte, error) {
if !i.Valid {
return json.Marshal(nil)
}
return json.Marshal(i.String)
}

func TestIssue370(t *testing.T) {
v := []struct {
V Issue370
}{
{V: Issue370{String: "test", Valid: true}},
}
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
got := string(b)
expected := `[{"V":"test"}]`
if got != expected {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}
2 changes: 1 addition & 1 deletion internal/encoder/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
case typ.Kind() == reflect.Map:
return c.ptrCode(runtime.PtrTo(typ))
default:
code, err := c.typeToCodeWithPtr(typ, false)
code, err := c.typeToCodeWithPtr(typ, true)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fb71671

Please sign in to comment.