Skip to content

Commit

Permalink
feat: check structs within arrays/slices/maps (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
shashkin authored Feb 27, 2024
1 parent 43e0e03 commit 854e4f1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
21 changes: 12 additions & 9 deletions musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,19 @@ type checker struct {
}

func (c *checker) parseStruct(typ types.Type) (*types.Struct, bool) {
for {
// unwrap pointers (if any) first.
ptr, ok := typ.(*types.Pointer)
if !ok {
break
}
typ = ptr.Elem()
}

switch typ := typ.(type) {
case *types.Pointer:
return c.parseStruct(typ.Elem())

case *types.Array:
return c.parseStruct(typ.Elem())

case *types.Slice:
return c.parseStruct(typ.Elem())

case *types.Map:
return c.parseStruct(typ.Elem())

case *types.Named: // a struct of the named type.
pkg := typ.Obj().Pkg()
if pkg == nil {
Expand Down
56 changes: 56 additions & 0 deletions testdata/src/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,62 @@ func embeddedType() {
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
}

func nestedArrayType() {
type Bar struct {
NoTag string
}
type Foo struct {
Bars [5]Bar `json:"bars"`
}
var foo Foo
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
}

func nestedSliceType() {
type Bar struct {
NoTag string
}
type Foo struct {
Bars []Bar `json:"bars"`
}
var foo Foo
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
}

func nestedMapType() {
type Bar struct {
NoTag string
}
type Foo struct {
Bars map[string]Bar `json:"bars"`
}
var foo Foo
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
}

func nestedComplexType() {
type Bar struct {
NoTag string
}
type Foo struct {
Bars **[][]map[string][][5][5]map[string]*Bar `json:"bars"`
}
var foo Foo
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
}

func recursiveType() {
// should not cause panic; see issue #16.
type Foo struct {
Expand Down

0 comments on commit 854e4f1

Please sign in to comment.