@@ -21,10 +21,10 @@ type analyzer struct {
21
21
include PatternsList
22
22
exclude PatternsList
23
23
24
- typesProcessCache map [string ]bool
24
+ typesProcessCache map [types. Type ]bool
25
25
typesProcessCacheMu sync.RWMutex
26
26
27
- structFieldsCache map [string ]* StructFields
27
+ structFieldsCache map [types. Type ]* StructFields
28
28
structFieldsCacheMu sync.RWMutex
29
29
}
30
30
@@ -33,9 +33,9 @@ type analyzer struct {
33
33
// -e arguments adds exclude patterns
34
34
func NewAnalyzer (include []string , exclude []string ) (* analysis.Analyzer , error ) {
35
35
a := analyzer { //nolint:exhaustruct
36
- typesProcessCache : map [string ]bool {},
36
+ typesProcessCache : map [types. Type ]bool {},
37
37
38
- structFieldsCache : map [string ]* StructFields {},
38
+ structFieldsCache : map [types. Type ]* StructFields {},
39
39
}
40
40
41
41
var err error
@@ -123,7 +123,7 @@ func (a *analyzer) newVisitor(pass *analysis.Pass) func(node ast.Node) {
123
123
return
124
124
}
125
125
126
- if ! a .shouldProcessType (typ . String () ) {
126
+ if ! a .shouldProcessType (typ ) {
127
127
return
128
128
}
129
129
@@ -138,7 +138,7 @@ func (a *analyzer) newVisitor(pass *analysis.Pass) func(node ast.Node) {
138
138
}
139
139
}
140
140
141
- missingFields := a .structMissingFields (lit , strct , typ .String (), pass .Pkg .Path ())
141
+ missingFields := a .structMissingFields (lit , strct , strings . HasPrefix ( typ .String (), pass .Pkg .Path () + "." ))
142
142
143
143
if len (missingFields ) == 1 {
144
144
pass .Reportf (node .Pos (), "%s is missing in %s" , missingFields [0 ], strctName )
@@ -148,7 +148,7 @@ func (a *analyzer) newVisitor(pass *analysis.Pass) func(node ast.Node) {
148
148
}
149
149
}
150
150
151
- func (a * analyzer ) shouldProcessType (typ string ) bool {
151
+ func (a * analyzer ) shouldProcessType (typ types. Type ) bool {
152
152
if len (a .include ) == 0 && len (a .exclude ) == 0 {
153
153
// skip whole part with cache, since we have no restrictions and have to check everything
154
154
return true
@@ -163,12 +163,13 @@ func (a *analyzer) shouldProcessType(typ string) bool {
163
163
defer a .typesProcessCacheMu .Unlock ()
164
164
165
165
v = true
166
+ typStr := typ .String ()
166
167
167
- if len (a .include ) > 0 && ! a .include .MatchesAny (typ ) {
168
+ if len (a .include ) > 0 && ! a .include .MatchesAny (typStr ) {
168
169
v = false
169
170
}
170
171
171
- if v && a .exclude .MatchesAny (typ ) {
172
+ if v && a .exclude .MatchesAny (typStr ) {
172
173
v = false
173
174
}
174
175
@@ -178,18 +179,13 @@ func (a *analyzer) shouldProcessType(typ string) bool {
178
179
return v
179
180
}
180
181
181
- func (a * analyzer ) structMissingFields (
182
- lit * ast.CompositeLit ,
183
- strct * types.Struct ,
184
- typ string ,
185
- pkgPath string ,
186
- ) []string {
182
+ func (a * analyzer ) structMissingFields (lit * ast.CompositeLit , strct * types.Struct , private bool ) []string {
187
183
keys , unnamed := literalKeys (lit )
188
- fields := a .structFields (typ , strct )
184
+ fields := a .structFields (strct )
189
185
190
186
var fieldNames []string
191
187
192
- if strings . HasPrefix ( typ , pkgPath + "." ) {
188
+ if private {
193
189
// we're in same package and should match private fields
194
190
fieldNames = fields .All
195
191
} else {
@@ -203,7 +199,9 @@ func (a *analyzer) structMissingFields(
203
199
return difference (fieldNames , keys )
204
200
}
205
201
206
- func (a * analyzer ) structFields (typ string , strct * types.Struct ) * StructFields {
202
+ func (a * analyzer ) structFields (strct * types.Struct ) * StructFields {
203
+ typ := strct .Underlying ()
204
+
207
205
a .structFieldsCacheMu .RLock ()
208
206
fields , ok := a .structFieldsCache [typ ]
209
207
a .structFieldsCacheMu .RUnlock ()
0 commit comments