Skip to content

Commit c5dd963

Browse files
committed
Switch lintersconfig to be untyped map
1 parent e02743c commit c5dd963

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1243
-760
lines changed

docs/linters.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ The `jsontags` linter checks the tag name against the regex `"^[a-z][a-z0-9]*(?:
113113

114114
```yaml
115115
lintersConfig:
116-
jsonTags:
116+
jsontags:
117117
jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # Provide a custom regex, which the json tag must match.
118118
```
119119

@@ -178,7 +178,7 @@ In this case, the `omitempty` policy can be set to `Ignore`, and the linter will
178178

179179
```yaml
180180
lintersConfig:
181-
optionalFields:
181+
optionalfields:
182182
pointers:
183183
preference: Always | WhenRequired # Whether to always require pointers, or only when required. Defaults to `Always`.
184184
policy: SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
@@ -216,7 +216,7 @@ The `optionalorrequired` linter also checks for the presence of optional or requ
216216

217217
```yaml
218218
lintersConfig:
219-
optionalOrRequired:
219+
optionalorrequired:
220220
preferredOptionalMarker: optional | kubebuilder:validation:Optional # The preferred optional marker to use, fixes will suggest to use this marker. Defaults to `optional`.
221221
preferredRequiredMarker: required | kubebuilder:validation:Required # The preferred required marker to use, fixes will suggest to use this marker. Defaults to `required`.
222222
```
@@ -236,7 +236,7 @@ and not having an `omitempty` value in their `json` tag.
236236

237237
```yaml
238238
lintersConfig:
239-
requiredFields:
239+
requiredfields:
240240
pointerPolicy: Warn | SuggestFix # The policy for pointers in required fields. Defaults to `SuggestFix`.
241241
```
242242

@@ -326,7 +326,7 @@ Because this linter has no way of determining which marker definition was intend
326326
It can configured to include a set of custom markers in the analysis by setting:
327327
```yaml
328328
lintersConfig:
329-
uniqueMarkers:
329+
uniquemarkers:
330330
customMarkers:
331331
- identifier: custom:SomeCustomMarker
332332
attributes:

pkg/analysis/commentstart/initializer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package commentstart
1818
import (
1919
"golang.org/x/tools/go/analysis"
2020
"sigs.k8s.io/kube-api-linter/pkg/analysis/initializer"
21-
"sigs.k8s.io/kube-api-linter/pkg/config"
2221
)
2322

2423
// Initializer returns the AnalyzerInitializer for this
@@ -32,6 +31,6 @@ func Initializer() initializer.AnalyzerInitializer {
3231
}
3332

3433
// Init returns the intialized Analyzer.
35-
func initAnalyzer(cfg config.LintersConfig) (*analysis.Analyzer, error) {
34+
func initAnalyzer(_ any) (*analysis.Analyzer, error) {
3635
return Analyzer, nil
3736
}

pkg/analysis/conditions/analyzer.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2929
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
3030
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
31-
"sigs.k8s.io/kube-api-linter/pkg/config"
3231
)
3332

3433
const (
@@ -63,14 +62,14 @@ func init() {
6362
}
6463

6564
type analyzer struct {
66-
isFirstField config.ConditionsFirstField
67-
useProtobuf config.ConditionsUseProtobuf
68-
usePatchStrategy config.ConditionsUsePatchStrategy
65+
isFirstField ConditionsFirstField
66+
useProtobuf ConditionsUseProtobuf
67+
usePatchStrategy ConditionsUsePatchStrategy
6968
}
7069

7170
// 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)
7473

7574
a := &analyzer{
7675
isFirstField: cfg.IsFirstField,
@@ -136,12 +135,12 @@ func (a *analyzer) checkField(pass *analysis.Pass, index int, field *ast.Field,
136135
checkFieldMarkers(pass, field, fieldMarkers, a.usePatchStrategy)
137136
a.checkFieldTags(pass, index, field)
138137

139-
if a.isFirstField == config.ConditionsFirstFieldWarn && index != 0 {
138+
if a.isFirstField == ConditionsFirstFieldWarn && index != 0 {
140139
pass.Reportf(field.Pos(), "Conditions field must be the first field in the struct")
141140
}
142141
}
143142

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) {
145144
missingMarkers := []string{}
146145
additionalMarkers := []markers.Marker{}
147146

@@ -170,22 +169,22 @@ func checkFieldMarkers(pass *analysis.Pass, field *ast.Field, fieldMarkers marke
170169
}
171170
}
172171

173-
func checkPatchStrategyMarkers(fieldMarkers markers.MarkerSet, usePatchStrategy config.ConditionsUsePatchStrategy) ([]string, []markers.Marker) {
172+
func checkPatchStrategyMarkers(fieldMarkers markers.MarkerSet, usePatchStrategy ConditionsUsePatchStrategy) ([]string, []markers.Marker) {
174173
missingMarkers := []string{}
175174
additionalMarkers := []markers.Marker{}
176175

177176
switch usePatchStrategy {
178-
case config.ConditionsUsePatchStrategySuggestFix, config.ConditionsUsePatchStrategyWarn:
177+
case ConditionsUsePatchStrategySuggestFix, ConditionsUsePatchStrategyWarn:
179178
if !fieldMarkers.HasWithValue(patchStrategyMerge) {
180179
missingMarkers = append(missingMarkers, patchStrategyMerge)
181180
}
182181

183182
if !fieldMarkers.HasWithValue(patchMergeKeyType) {
184183
missingMarkers = append(missingMarkers, patchMergeKeyType)
185184
}
186-
case config.ConditionsUsePatchStrategyIgnore:
185+
case ConditionsUsePatchStrategyIgnore:
187186
// If it's there, we don't care.
188-
case config.ConditionsUsePatchStrategyForbid:
187+
case ConditionsUsePatchStrategyForbid:
189188
if fieldMarkers.HasWithValue(patchStrategyMerge) {
190189
additionalMarkers = append(additionalMarkers, fieldMarkers[patchStrategy]...)
191190
}
@@ -200,11 +199,11 @@ func checkPatchStrategyMarkers(fieldMarkers markers.MarkerSet, usePatchStrategy
200199
return missingMarkers, additionalMarkers
201200
}
202201

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) {
204203
suggestedFixes := []analysis.SuggestedFix{}
205204

206205
// 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 {
208207
switch marker {
209208
case patchStrategyMerge, patchMergeKeyType:
210209
return false
@@ -324,14 +323,14 @@ func (a *analyzer) checkFieldTags(pass *analysis.Pass, index int, field *ast.Fie
324323
}
325324
}
326325

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 {
328327
expectedTag := fmt.Sprintf("`%s", expectedJSONTag)
329328

330-
if usePatchStrategy == config.ConditionsUsePatchStrategySuggestFix || usePatchStrategy == config.ConditionsUsePatchStrategyWarn {
329+
if usePatchStrategy == ConditionsUsePatchStrategySuggestFix || usePatchStrategy == ConditionsUsePatchStrategyWarn {
331330
expectedTag += fmt.Sprintf(" %s", expectedPatchTag)
332331
}
333332

334-
if useProtobuf == config.ConditionsUseProtobufSuggestFix || useProtobuf == config.ConditionsUseProtobufWarn {
333+
if useProtobuf == ConditionsUseProtobufSuggestFix || useProtobuf == ConditionsUseProtobufWarn {
335334
expectedTag += fmt.Sprintf(" %s", getExpectedProtobufTag(isFirstField, index))
336335
}
337336

@@ -340,46 +339,46 @@ func getExpectedTag(usePatchStrategy config.ConditionsUsePatchStrategy, useProto
340339
return expectedTag
341340
}
342341

343-
func getExpectedProtobufTag(isFirstField config.ConditionsFirstField, index int) string {
342+
func getExpectedProtobufTag(isFirstField ConditionsFirstField, index int) string {
344343
i := 1
345-
if isFirstField == config.ConditionsFirstFieldIgnore {
344+
if isFirstField == ConditionsFirstFieldIgnore {
346345
i = index + 1
347346
}
348347

349348
return fmt.Sprintf(expectedProtobufTag, i)
350349
}
351350

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) {
353352
patchTagCorrect, patchShouldSuggestFix := patchStrategyTagIsAsExpected(tag, usePatchStrategy)
354353
protoTagCorrect, protoShouldSuggestFix := protobufTagIsAsExpected(tag, useProtobuf, isFirstField, index)
355354

356355
return patchTagCorrect && protoTagCorrect, patchShouldSuggestFix || protoShouldSuggestFix
357356
}
358357

359-
func patchStrategyTagIsAsExpected(tag string, usePatchStrategy config.ConditionsUsePatchStrategy) (bool, bool) {
358+
func patchStrategyTagIsAsExpected(tag string, usePatchStrategy ConditionsUsePatchStrategy) (bool, bool) {
360359
switch usePatchStrategy {
361-
case config.ConditionsUsePatchStrategySuggestFix:
360+
case ConditionsUsePatchStrategySuggestFix:
362361
return strings.Contains(tag, expectedPatchTag), true
363-
case config.ConditionsUsePatchStrategyWarn:
362+
case ConditionsUsePatchStrategyWarn:
364363
return strings.Contains(tag, expectedPatchTag), false
365-
case config.ConditionsUsePatchStrategyIgnore:
364+
case ConditionsUsePatchStrategyIgnore:
366365
return true, false
367-
case config.ConditionsUsePatchStrategyForbid:
366+
case ConditionsUsePatchStrategyForbid:
368367
return !strings.Contains(tag, expectedPatchTag), true
369368
default:
370369
panic("unexpected usePatchStrategy value")
371370
}
372371
}
373372

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) {
375374
switch useProtobuf {
376-
case config.ConditionsUseProtobufSuggestFix:
375+
case ConditionsUseProtobufSuggestFix:
377376
return strings.Contains(tag, getExpectedProtobufTag(isFirstField, index)), true
378-
case config.ConditionsUseProtobufWarn:
377+
case ConditionsUseProtobufWarn:
379378
return strings.Contains(tag, getExpectedProtobufTag(isFirstField, index)), false
380-
case config.ConditionsUseProtobufIgnore:
379+
case ConditionsUseProtobufIgnore:
381380
return true, false
382-
case config.ConditionsUseProtobufForbid:
381+
case ConditionsUseProtobufForbid:
383382
return !strings.Contains(tag, getExpectedProtobufTag(isFirstField, index)), true
384383
default:
385384
panic("unexpected useProtobuf value")
@@ -429,16 +428,16 @@ func isSliceMetaV1Condition(field *ast.Field) bool {
429428
return true
430429
}
431430

432-
func defaultConfig(cfg *config.ConditionsConfig) {
431+
func defaultConfig(cfg *ConditionsConfig) {
433432
if cfg.IsFirstField == "" {
434-
cfg.IsFirstField = config.ConditionsFirstFieldWarn
433+
cfg.IsFirstField = ConditionsFirstFieldWarn
435434
}
436435

437436
if cfg.UseProtobuf == "" {
438-
cfg.UseProtobuf = config.ConditionsUseProtobufSuggestFix
437+
cfg.UseProtobuf = ConditionsUseProtobufSuggestFix
439438
}
440439

441440
if cfg.UsePatchStrategy == "" {
442-
cfg.UsePatchStrategy = config.ConditionsUsePatchStrategySuggestFix
441+
cfg.UsePatchStrategy = ConditionsUsePatchStrategySuggestFix
443442
}
444443
}

pkg/analysis/conditions/analyzer_test.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import (
2020

2121
"golang.org/x/tools/go/analysis/analysistest"
2222
"sigs.k8s.io/kube-api-linter/pkg/analysis/conditions"
23-
"sigs.k8s.io/kube-api-linter/pkg/config"
2423
)
2524

2625
func TestDefaultConfiguration(t *testing.T) {
2726
testdata := analysistest.TestData()
2827

29-
a, err := conditions.Initializer().Init(config.LintersConfig{})
28+
a, err := conditions.Initializer().Init(&conditions.ConditionsConfig{})
3029
if err != nil {
3130
t.Fatal(err)
3231
}
@@ -37,10 +36,8 @@ func TestDefaultConfiguration(t *testing.T) {
3736
func TestNotFieldFirst(t *testing.T) {
3837
testdata := analysistest.TestData()
3938

40-
a, err := conditions.Initializer().Init(config.LintersConfig{
41-
Conditions: config.ConditionsConfig{
42-
IsFirstField: config.ConditionsFirstFieldIgnore,
43-
},
39+
a, err := conditions.Initializer().Init(&conditions.ConditionsConfig{
40+
IsFirstField: conditions.ConditionsFirstFieldIgnore,
4441
})
4542
if err != nil {
4643
t.Fatal(err)
@@ -52,10 +49,8 @@ func TestNotFieldFirst(t *testing.T) {
5249
func TestIgnoreProtobuf(t *testing.T) {
5350
testdata := analysistest.TestData()
5451

55-
a, err := conditions.Initializer().Init(config.LintersConfig{
56-
Conditions: config.ConditionsConfig{
57-
UseProtobuf: config.ConditionsUseProtobufIgnore,
58-
},
52+
a, err := conditions.Initializer().Init(&conditions.ConditionsConfig{
53+
UseProtobuf: conditions.ConditionsUseProtobufIgnore,
5954
})
6055
if err != nil {
6156
t.Fatal(err)
@@ -67,10 +62,8 @@ func TestIgnoreProtobuf(t *testing.T) {
6762
func TestForbidProtobuf(t *testing.T) {
6863
testdata := analysistest.TestData()
6964

70-
a, err := conditions.Initializer().Init(config.LintersConfig{
71-
Conditions: config.ConditionsConfig{
72-
UseProtobuf: config.ConditionsUseProtobufForbid,
73-
},
65+
a, err := conditions.Initializer().Init(&conditions.ConditionsConfig{
66+
UseProtobuf: conditions.ConditionsUseProtobufForbid,
7467
})
7568
if err != nil {
7669
t.Fatal(err)
@@ -82,10 +75,8 @@ func TestForbidProtobuf(t *testing.T) {
8275
func TestIgnorePatchStrategy(t *testing.T) {
8376
testdata := analysistest.TestData()
8477

85-
a, err := conditions.Initializer().Init(config.LintersConfig{
86-
Conditions: config.ConditionsConfig{
87-
UsePatchStrategy: config.ConditionsUsePatchStrategyIgnore,
88-
},
78+
a, err := conditions.Initializer().Init(&conditions.ConditionsConfig{
79+
UsePatchStrategy: conditions.ConditionsUsePatchStrategyIgnore,
8980
})
9081
if err != nil {
9182
t.Fatal(err)
@@ -97,10 +88,8 @@ func TestIgnorePatchStrategy(t *testing.T) {
9788
func TestForbidPatchStrategy(t *testing.T) {
9889
testdata := analysistest.TestData()
9990

100-
a, err := conditions.Initializer().Init(config.LintersConfig{
101-
Conditions: config.ConditionsConfig{
102-
UsePatchStrategy: config.ConditionsUsePatchStrategyForbid,
103-
},
91+
a, err := conditions.Initializer().Init(&conditions.ConditionsConfig{
92+
UsePatchStrategy: conditions.ConditionsUsePatchStrategyForbid,
10493
})
10594
if err != nil {
10695
t.Fatal(err)

0 commit comments

Comments
 (0)