Skip to content

Commit 5b5f6e3

Browse files
authored
fix: check whether a pointer-type implements a Marshaler interface (#94)
1 parent 505c602 commit 5b5f6e3

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

musttag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func implementsInterface(typ types.Type, ifaces []string, imports []*types.Packa
273273
if !ok {
274274
continue
275275
}
276-
if types.Implements(typ, iface) {
276+
if types.Implements(typ, iface) || types.Implements(types.NewPointer(typ), iface) {
277277
return true
278278
}
279279
}

testdata/src/tests/tests.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,17 @@ func shouldBeIgnored() {
131131
json.Marshal(nil) // nil argument, see issue #20.
132132
}
133133

134-
type WithInterface struct {
135-
NoTag string
136-
}
137-
138-
func (w WithInterface) MarshalJSON() ([]byte, error) {
139-
return json.Marshal(w.NoTag)
140-
}
141-
142134
func nestedTypeWithInterface() {
143135
type Foo struct {
144-
Nested WithInterface `json:"nested"`
136+
Nested Marshaler `json:"nested"`
145137
}
146138
var foo Foo
147-
json.Marshal(foo) // no error
148-
json.Marshal(&foo) // no error
149-
json.Marshal(Foo{}) // no error
150-
json.Marshal(&Foo{}) // no error
139+
json.Marshal(foo)
140+
json.Marshal(&foo)
141+
json.Marshal(Foo{})
142+
json.Marshal(&Foo{})
143+
json.Unmarshal(nil, &foo)
144+
json.Unmarshal(nil, &Foo{})
151145
}
152146

153147
func ignoredNestedType() {
@@ -159,10 +153,10 @@ func ignoredNestedType() {
159153
Exported string `json:"exported"`
160154
}
161155
var foo Foo
162-
json.Marshal(foo) // no error
163-
json.Marshal(&foo) // no error
164-
json.Marshal(Foo{}) // no error
165-
json.Marshal(&Foo{}) // no error
156+
json.Marshal(foo)
157+
json.Marshal(&foo)
158+
json.Marshal(Foo{})
159+
json.Marshal(&Foo{})
166160
}
167161

168162
func ignoredNestedTypeWithSubsequentNoTagField() {

0 commit comments

Comments
 (0)