@@ -28,7 +28,6 @@ import (
28
28
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
29
29
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
30
30
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
31
- "sigs.k8s.io/kube-api-linter/pkg/config"
32
31
)
33
32
34
33
const (
@@ -63,14 +62,14 @@ func init() {
63
62
}
64
63
65
64
type analyzer struct {
66
- isFirstField config. ConditionsFirstField
67
- useProtobuf config. ConditionsUseProtobuf
68
- usePatchStrategy config. ConditionsUsePatchStrategy
65
+ isFirstField ConditionsFirstField
66
+ useProtobuf ConditionsUseProtobuf
67
+ usePatchStrategy ConditionsUsePatchStrategy
69
68
}
70
69
71
70
// newAnalyzer creates a new analyzer.
72
- func newAnalyzer (cfg config. ConditionsConfig ) * analysis.Analyzer {
73
- defaultConfig (& cfg )
71
+ func newAnalyzer (cfg * ConditionsConfig ) * analysis.Analyzer {
72
+ defaultConfig (cfg )
74
73
75
74
a := & analyzer {
76
75
isFirstField : cfg .IsFirstField ,
@@ -136,12 +135,12 @@ func (a *analyzer) checkField(pass *analysis.Pass, index int, field *ast.Field,
136
135
checkFieldMarkers (pass , field , fieldMarkers , a .usePatchStrategy )
137
136
a .checkFieldTags (pass , index , field )
138
137
139
- if a .isFirstField == config . ConditionsFirstFieldWarn && index != 0 {
138
+ if a .isFirstField == ConditionsFirstFieldWarn && index != 0 {
140
139
pass .Reportf (field .Pos (), "Conditions field must be the first field in the struct" )
141
140
}
142
141
}
143
142
144
- func checkFieldMarkers (pass * analysis.Pass , field * ast.Field , fieldMarkers markers.MarkerSet , usePatchStrategy config. ConditionsUsePatchStrategy ) {
143
+ func checkFieldMarkers (pass * analysis.Pass , field * ast.Field , fieldMarkers markers.MarkerSet , usePatchStrategy ConditionsUsePatchStrategy ) {
145
144
missingMarkers := []string {}
146
145
additionalMarkers := []markers.Marker {}
147
146
@@ -170,22 +169,22 @@ func checkFieldMarkers(pass *analysis.Pass, field *ast.Field, fieldMarkers marke
170
169
}
171
170
}
172
171
173
- func checkPatchStrategyMarkers (fieldMarkers markers.MarkerSet , usePatchStrategy config. ConditionsUsePatchStrategy ) ([]string , []markers.Marker ) {
172
+ func checkPatchStrategyMarkers (fieldMarkers markers.MarkerSet , usePatchStrategy ConditionsUsePatchStrategy ) ([]string , []markers.Marker ) {
174
173
missingMarkers := []string {}
175
174
additionalMarkers := []markers.Marker {}
176
175
177
176
switch usePatchStrategy {
178
- case config . ConditionsUsePatchStrategySuggestFix , config . ConditionsUsePatchStrategyWarn :
177
+ case ConditionsUsePatchStrategySuggestFix , ConditionsUsePatchStrategyWarn :
179
178
if ! fieldMarkers .HasWithValue (patchStrategyMerge ) {
180
179
missingMarkers = append (missingMarkers , patchStrategyMerge )
181
180
}
182
181
183
182
if ! fieldMarkers .HasWithValue (patchMergeKeyType ) {
184
183
missingMarkers = append (missingMarkers , patchMergeKeyType )
185
184
}
186
- case config . ConditionsUsePatchStrategyIgnore :
185
+ case ConditionsUsePatchStrategyIgnore :
187
186
// If it's there, we don't care.
188
- case config . ConditionsUsePatchStrategyForbid :
187
+ case ConditionsUsePatchStrategyForbid :
189
188
if fieldMarkers .HasWithValue (patchStrategyMerge ) {
190
189
additionalMarkers = append (additionalMarkers , fieldMarkers [patchStrategy ]... )
191
190
}
@@ -200,11 +199,11 @@ func checkPatchStrategyMarkers(fieldMarkers markers.MarkerSet, usePatchStrategy
200
199
return missingMarkers , additionalMarkers
201
200
}
202
201
203
- func reportMissingMarkers (pass * analysis.Pass , field * ast.Field , missingMarkers []string , usePatchStrategy config. ConditionsUsePatchStrategy ) {
202
+ func reportMissingMarkers (pass * analysis.Pass , field * ast.Field , missingMarkers []string , usePatchStrategy ConditionsUsePatchStrategy ) {
204
203
suggestedFixes := []analysis.SuggestedFix {}
205
204
206
205
// If patch strategy is warn, and the only markers in the list are patchStrategy and patchMergeKeyType, we don't need to suggest a fix.
207
- if usePatchStrategy != config . ConditionsUsePatchStrategyWarn || slices .ContainsFunc [[]string , string ](missingMarkers , func (marker string ) bool {
206
+ if usePatchStrategy != ConditionsUsePatchStrategyWarn || slices .ContainsFunc [[]string , string ](missingMarkers , func (marker string ) bool {
208
207
switch marker {
209
208
case patchStrategyMerge , patchMergeKeyType :
210
209
return false
@@ -324,14 +323,14 @@ func (a *analyzer) checkFieldTags(pass *analysis.Pass, index int, field *ast.Fie
324
323
}
325
324
}
326
325
327
- func getExpectedTag (usePatchStrategy config. ConditionsUsePatchStrategy , useProtobuf config. ConditionsUseProtobuf , isFirstField config. ConditionsFirstField , index int ) string {
326
+ func getExpectedTag (usePatchStrategy ConditionsUsePatchStrategy , useProtobuf ConditionsUseProtobuf , isFirstField ConditionsFirstField , index int ) string {
328
327
expectedTag := fmt .Sprintf ("`%s" , expectedJSONTag )
329
328
330
- if usePatchStrategy == config . ConditionsUsePatchStrategySuggestFix || usePatchStrategy == config . ConditionsUsePatchStrategyWarn {
329
+ if usePatchStrategy == ConditionsUsePatchStrategySuggestFix || usePatchStrategy == ConditionsUsePatchStrategyWarn {
331
330
expectedTag += fmt .Sprintf (" %s" , expectedPatchTag )
332
331
}
333
332
334
- if useProtobuf == config . ConditionsUseProtobufSuggestFix || useProtobuf == config . ConditionsUseProtobufWarn {
333
+ if useProtobuf == ConditionsUseProtobufSuggestFix || useProtobuf == ConditionsUseProtobufWarn {
335
334
expectedTag += fmt .Sprintf (" %s" , getExpectedProtobufTag (isFirstField , index ))
336
335
}
337
336
@@ -340,46 +339,46 @@ func getExpectedTag(usePatchStrategy config.ConditionsUsePatchStrategy, useProto
340
339
return expectedTag
341
340
}
342
341
343
- func getExpectedProtobufTag (isFirstField config. ConditionsFirstField , index int ) string {
342
+ func getExpectedProtobufTag (isFirstField ConditionsFirstField , index int ) string {
344
343
i := 1
345
- if isFirstField == config . ConditionsFirstFieldIgnore {
344
+ if isFirstField == ConditionsFirstFieldIgnore {
346
345
i = index + 1
347
346
}
348
347
349
348
return fmt .Sprintf (expectedProtobufTag , i )
350
349
}
351
350
352
- func tagIsAsExpected (tag string , usePatchStrategy config. ConditionsUsePatchStrategy , useProtobuf config. ConditionsUseProtobuf , isFirstField config. ConditionsFirstField , index int ) (bool , bool ) {
351
+ func tagIsAsExpected (tag string , usePatchStrategy ConditionsUsePatchStrategy , useProtobuf ConditionsUseProtobuf , isFirstField ConditionsFirstField , index int ) (bool , bool ) {
353
352
patchTagCorrect , patchShouldSuggestFix := patchStrategyTagIsAsExpected (tag , usePatchStrategy )
354
353
protoTagCorrect , protoShouldSuggestFix := protobufTagIsAsExpected (tag , useProtobuf , isFirstField , index )
355
354
356
355
return patchTagCorrect && protoTagCorrect , patchShouldSuggestFix || protoShouldSuggestFix
357
356
}
358
357
359
- func patchStrategyTagIsAsExpected (tag string , usePatchStrategy config. ConditionsUsePatchStrategy ) (bool , bool ) {
358
+ func patchStrategyTagIsAsExpected (tag string , usePatchStrategy ConditionsUsePatchStrategy ) (bool , bool ) {
360
359
switch usePatchStrategy {
361
- case config . ConditionsUsePatchStrategySuggestFix :
360
+ case ConditionsUsePatchStrategySuggestFix :
362
361
return strings .Contains (tag , expectedPatchTag ), true
363
- case config . ConditionsUsePatchStrategyWarn :
362
+ case ConditionsUsePatchStrategyWarn :
364
363
return strings .Contains (tag , expectedPatchTag ), false
365
- case config . ConditionsUsePatchStrategyIgnore :
364
+ case ConditionsUsePatchStrategyIgnore :
366
365
return true , false
367
- case config . ConditionsUsePatchStrategyForbid :
366
+ case ConditionsUsePatchStrategyForbid :
368
367
return ! strings .Contains (tag , expectedPatchTag ), true
369
368
default :
370
369
panic ("unexpected usePatchStrategy value" )
371
370
}
372
371
}
373
372
374
- func protobufTagIsAsExpected (tag string , useProtobuf config. ConditionsUseProtobuf , isFirstField config. ConditionsFirstField , index int ) (bool , bool ) {
373
+ func protobufTagIsAsExpected (tag string , useProtobuf ConditionsUseProtobuf , isFirstField ConditionsFirstField , index int ) (bool , bool ) {
375
374
switch useProtobuf {
376
- case config . ConditionsUseProtobufSuggestFix :
375
+ case ConditionsUseProtobufSuggestFix :
377
376
return strings .Contains (tag , getExpectedProtobufTag (isFirstField , index )), true
378
- case config . ConditionsUseProtobufWarn :
377
+ case ConditionsUseProtobufWarn :
379
378
return strings .Contains (tag , getExpectedProtobufTag (isFirstField , index )), false
380
- case config . ConditionsUseProtobufIgnore :
379
+ case ConditionsUseProtobufIgnore :
381
380
return true , false
382
- case config . ConditionsUseProtobufForbid :
381
+ case ConditionsUseProtobufForbid :
383
382
return ! strings .Contains (tag , getExpectedProtobufTag (isFirstField , index )), true
384
383
default :
385
384
panic ("unexpected useProtobuf value" )
@@ -429,16 +428,16 @@ func isSliceMetaV1Condition(field *ast.Field) bool {
429
428
return true
430
429
}
431
430
432
- func defaultConfig (cfg * config. ConditionsConfig ) {
431
+ func defaultConfig (cfg * ConditionsConfig ) {
433
432
if cfg .IsFirstField == "" {
434
- cfg .IsFirstField = config . ConditionsFirstFieldWarn
433
+ cfg .IsFirstField = ConditionsFirstFieldWarn
435
434
}
436
435
437
436
if cfg .UseProtobuf == "" {
438
- cfg .UseProtobuf = config . ConditionsUseProtobufSuggestFix
437
+ cfg .UseProtobuf = ConditionsUseProtobufSuggestFix
439
438
}
440
439
441
440
if cfg .UsePatchStrategy == "" {
442
- cfg .UsePatchStrategy = config . ConditionsUsePatchStrategySuggestFix
441
+ cfg .UsePatchStrategy = ConditionsUsePatchStrategySuggestFix
443
442
}
444
443
}
0 commit comments