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

feat: check whether nested types implement a Marshaler interface #84

Merged
merged 1 commit into from
Mar 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 28 additions & 25 deletions musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,14 @@ func run(pass *analysis.Pass, mainModule string, funcs map[string]Func) (_ any,
return // no type info found.
}

// TODO: check nested structs too.
if implementsInterface(typ, fn.ifaceWhitelist, pass.Pkg.Imports()) {
return // the type implements a Marshaler interface; see issue #64.
}

checker := checker{
mainModule: mainModule,
seenTypes: make(map[string]struct{}),
}

styp, ok := checker.parseStruct(typ)
if !ok {
return // not a struct.
mainModule: mainModule,
seenTypes: make(map[string]struct{}),
ifaceWhitelist: fn.ifaceWhitelist,
imports: pass.Pkg.Imports(),
}

if valid := checker.checkStruct(styp, fn.Tag); valid {
if valid := checker.checkType(typ, fn.Tag); valid {
return // nothing to report.
}

Expand All @@ -145,8 +137,28 @@ func run(pass *analysis.Pass, mainModule string, funcs map[string]Func) (_ any,
}

type checker struct {
mainModule string
seenTypes map[string]struct{}
mainModule string
seenTypes map[string]struct{}
ifaceWhitelist []string
imports []*types.Package
}

func (c *checker) checkType(typ types.Type, tag string) bool {
if _, ok := c.seenTypes[typ.String()]; ok {
return true // already checked.
}
c.seenTypes[typ.String()] = struct{}{}

if implementsInterface(typ, c.ifaceWhitelist, c.imports) {
return true // the type implements a Marshaler interface; see issue #64.
}

styp, ok := c.parseStruct(typ)
if !ok {
return true // not a struct.
}

return c.checkStruct(styp, tag)
}

func (c *checker) parseStruct(typ types.Type) (*types.Struct, bool) {
Expand Down Expand Up @@ -186,8 +198,6 @@ func (c *checker) parseStruct(typ types.Type) (*types.Struct, bool) {
}

func (c *checker) checkStruct(styp *types.Struct, tag string) (valid bool) {
c.seenTypes[styp.String()] = struct{}{}

for i := 0; i < styp.NumFields(); i++ {
field := styp.Field(i)
if !field.Exported() {
Expand All @@ -201,14 +211,7 @@ func (c *checker) checkStruct(styp *types.Struct, tag string) (valid bool) {
}
}

nested, ok := c.parseStruct(field.Type())
if !ok {
continue
}
if _, ok := c.seenTypes[nested.String()]; ok {
continue
}
if valid := c.checkStruct(nested, tag); !valid {
if valid := c.checkType(field.Type(), tag); !valid {
return false
}
}
Expand Down
19 changes: 19 additions & 0 deletions testdata/src/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,22 @@ func shouldBeIgnored() {
json.Marshal(0) // a non-struct argument.
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"`
}
var foo Foo
json.Marshal(foo) // no error
json.Marshal(&foo) // no error
json.Marshal(Foo{}) // no error
json.Marshal(&Foo{}) // no error
}