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: check whether a pointer-type implements a Marshaler interface #94

Merged
merged 1 commit into from
May 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func implementsInterface(typ types.Type, ifaces []string, imports []*types.Packa
if !ok {
continue
}
if types.Implements(typ, iface) {
if types.Implements(typ, iface) || types.Implements(types.NewPointer(typ), iface) {
return true
}
}
Expand Down
28 changes: 11 additions & 17 deletions testdata/src/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,17 @@ func shouldBeIgnored() {
json.Marshal(nil) // nil argument, see issue #20.
}

type WithInterface struct {
NoTag string
}

func (w WithInterface) MarshalJSON() ([]byte, error) {
return json.Marshal(w.NoTag)
}

func nestedTypeWithInterface() {
type Foo struct {
Nested WithInterface `json:"nested"`
Nested Marshaler `json:"nested"`
}
var foo Foo
json.Marshal(foo) // no error
json.Marshal(&foo) // no error
json.Marshal(Foo{}) // no error
json.Marshal(&Foo{}) // no error
json.Marshal(foo)
json.Marshal(&foo)
json.Marshal(Foo{})
json.Marshal(&Foo{})
json.Unmarshal(nil, &foo)
json.Unmarshal(nil, &Foo{})
}

func ignoredNestedType() {
Expand All @@ -159,10 +153,10 @@ func ignoredNestedType() {
Exported string `json:"exported"`
}
var foo Foo
json.Marshal(foo) // no error
json.Marshal(&foo) // no error
json.Marshal(Foo{}) // no error
json.Marshal(&Foo{}) // no error
json.Marshal(foo)
json.Marshal(&foo)
json.Marshal(Foo{})
json.Marshal(&Foo{})
}

func ignoredNestedTypeWithSubsequentNoTagField() {
Expand Down
Loading