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: change isPtr to true on listElemCode #379

Merged
merged 1 commit into from Jul 7, 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
29 changes: 29 additions & 0 deletions encode_test.go
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)
}
}
3 changes: 2 additions & 1 deletion internal/encoder/compiler.go
Expand Up @@ -487,7 +487,8 @@ 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)
// Strictly not isPtr == true, but reflect.ValueOf().Index() is canAddr, so set isPtr == true.
code, err := c.typeToCodeWithPtr(typ, true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm..., I feel that the fix to set isPtr to true is not correct to fix this issue (
since elem is not ptr type )

Copy link
Sponsor Contributor Author

@orisano orisano Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment!

if err != nil {
return nil, err
}
Expand Down