Skip to content

Commit

Permalink
fix: check whether a pointer-type implements a Marshaler interface (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored May 8, 2024
1 parent 505c602 commit 5b5f6e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
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

0 comments on commit 5b5f6e3

Please sign in to comment.