Skip to content

Commit 854e4f1

Browse files
authored
feat: check structs within arrays/slices/maps (#80)
1 parent 43e0e03 commit 854e4f1

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

musttag.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,19 @@ type checker struct {
150150
}
151151

152152
func (c *checker) parseStruct(typ types.Type) (*types.Struct, bool) {
153-
for {
154-
// unwrap pointers (if any) first.
155-
ptr, ok := typ.(*types.Pointer)
156-
if !ok {
157-
break
158-
}
159-
typ = ptr.Elem()
160-
}
161-
162153
switch typ := typ.(type) {
154+
case *types.Pointer:
155+
return c.parseStruct(typ.Elem())
156+
157+
case *types.Array:
158+
return c.parseStruct(typ.Elem())
159+
160+
case *types.Slice:
161+
return c.parseStruct(typ.Elem())
162+
163+
case *types.Map:
164+
return c.parseStruct(typ.Elem())
165+
163166
case *types.Named: // a struct of the named type.
164167
pkg := typ.Obj().Pkg()
165168
if pkg == nil {

testdata/src/tests/tests.go

+56
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,62 @@ func embeddedType() {
5151
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
5252
}
5353

54+
func nestedArrayType() {
55+
type Bar struct {
56+
NoTag string
57+
}
58+
type Foo struct {
59+
Bars [5]Bar `json:"bars"`
60+
}
61+
var foo Foo
62+
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
63+
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
64+
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
65+
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
66+
}
67+
68+
func nestedSliceType() {
69+
type Bar struct {
70+
NoTag string
71+
}
72+
type Foo struct {
73+
Bars []Bar `json:"bars"`
74+
}
75+
var foo Foo
76+
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
77+
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
78+
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
79+
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
80+
}
81+
82+
func nestedMapType() {
83+
type Bar struct {
84+
NoTag string
85+
}
86+
type Foo struct {
87+
Bars map[string]Bar `json:"bars"`
88+
}
89+
var foo Foo
90+
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
91+
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
92+
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
93+
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
94+
}
95+
96+
func nestedComplexType() {
97+
type Bar struct {
98+
NoTag string
99+
}
100+
type Foo struct {
101+
Bars **[][]map[string][][5][5]map[string]*Bar `json:"bars"`
102+
}
103+
var foo Foo
104+
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
105+
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
106+
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
107+
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
108+
}
109+
54110
func recursiveType() {
55111
// should not cause panic; see issue #16.
56112
type Foo struct {

0 commit comments

Comments
 (0)