fix: change isPtr to true on listElemCode

fix #370
This commit is contained in:
Nao Yonashiro 2022-07-05 01:15:08 +09:00
parent 1468eefb01
commit 565e07e45c
2 changed files with 31 additions and 1 deletions

View File

@ -2424,3 +2424,32 @@ func TestIssue376(t *testing.T) {
t.Errorf("unexpected result: %v != %v", got, expected) 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)
}
}

View File

@ -487,7 +487,8 @@ func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
case typ.Kind() == reflect.Map: case typ.Kind() == reflect.Map:
return c.ptrCode(runtime.PtrTo(typ)) return c.ptrCode(runtime.PtrTo(typ))
default: default:
code, err := c.typeToCodeWithPtr(typ, false) // Strictly not isPtr == true, but reflect.ValueOf().Index() is canAddr, so set isPtr == true.
code, err := c.typeToCodeWithPtr(typ, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }