Skip to content

Commit ab9f31e

Browse files
committed
Move validations directly into analyzer packages
1 parent 2627b31 commit ab9f31e

File tree

19 files changed

+385
-197
lines changed

19 files changed

+385
-197
lines changed

pkg/analysis/commentstart/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
return true

pkg/analysis/conditions/initializer.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ limitations under the License.
1616
package conditions
1717

1818
import (
19+
"fmt"
20+
1921
"golang.org/x/tools/go/analysis"
22+
"k8s.io/apimachinery/pkg/util/validation/field"
2023
"sigs.k8s.io/kube-api-linter/pkg/config"
2124
)
2225

@@ -39,6 +42,41 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3942
return newAnalyzer(cfg.Conditions), nil
4043
}
4144

45+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
46+
func (initializer) IsConfigurable() bool {
47+
return true
48+
}
49+
50+
// ValidateConfig implements validation of the conditions linter config.
51+
func (initializer) ValidateConfig(cfg any, fldPath *field.Path) field.ErrorList {
52+
cc, ok := cfg.(config.ConditionsConfig)
53+
if !ok {
54+
return field.ErrorList{field.InternalError(fldPath, fmt.Errorf("incorrect type for passed configuration: %T", cfg))}
55+
}
56+
57+
fieldErrors := field.ErrorList{}
58+
59+
switch cc.IsFirstField {
60+
case "", config.ConditionsFirstFieldWarn, config.ConditionsFirstFieldIgnore:
61+
default:
62+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("isFirstField"), cc.IsFirstField, fmt.Sprintf("invalid value, must be one of %q, %q or omitted", config.ConditionsFirstFieldWarn, config.ConditionsFirstFieldIgnore)))
63+
}
64+
65+
switch cc.UseProtobuf {
66+
case "", config.ConditionsUseProtobufSuggestFix, config.ConditionsUseProtobufWarn, config.ConditionsUseProtobufIgnore, config.ConditionsUseProtobufForbid:
67+
default:
68+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("useProtobuf"), cc.UseProtobuf, fmt.Sprintf("invalid value, must be one of %q, %q, %q, %q or omitted", config.ConditionsUseProtobufSuggestFix, config.ConditionsUseProtobufWarn, config.ConditionsUseProtobufIgnore, config.ConditionsUseProtobufForbid)))
69+
}
70+
71+
switch cc.UsePatchStrategy {
72+
case "", config.ConditionsUsePatchStrategySuggestFix, config.ConditionsUsePatchStrategyWarn, config.ConditionsUsePatchStrategyIgnore, config.ConditionsUsePatchStrategyForbid:
73+
default:
74+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("usePatchStrategy"), cc.UsePatchStrategy, fmt.Sprintf("invalid value, must be one of %q, %q, %q, %q or omitted", config.ConditionsUsePatchStrategySuggestFix, config.ConditionsUsePatchStrategyWarn, config.ConditionsUsePatchStrategyIgnore, config.ConditionsUsePatchStrategyForbid)))
75+
}
76+
77+
return fieldErrors
78+
}
79+
4280
// Default determines whether this Analyzer is on by default, or not.
4381
func (initializer) Default() bool {
4482
return true

pkg/analysis/duplicatemarkers/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
// Duplicated markers are a sign of bad code, and should be avoided.

pkg/analysis/integers/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
return true

pkg/analysis/jsontags/initializer.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ limitations under the License.
1616
package jsontags
1717

1818
import (
19+
"fmt"
20+
"regexp"
21+
1922
"golang.org/x/tools/go/analysis"
23+
"k8s.io/apimachinery/pkg/util/validation/field"
2024
"sigs.k8s.io/kube-api-linter/pkg/config"
2125
)
2226

@@ -39,6 +43,29 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3943
return newAnalyzer(cfg.JSONTags)
4044
}
4145

46+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
47+
func (initializer) IsConfigurable() bool {
48+
return true
49+
}
50+
51+
// validateJSONTagsConfig is used to validate the configuration in the config.JSONTagsConfig struct.
52+
func (initializer) ValidateConfig(cfg any, fldPath *field.Path) field.ErrorList {
53+
jtc, ok := cfg.(config.JSONTagsConfig)
54+
if !ok {
55+
return field.ErrorList{field.InternalError(fldPath, fmt.Errorf("incorrect type for passed configuration: %T", cfg))}
56+
}
57+
58+
fieldErrors := field.ErrorList{}
59+
60+
if jtc.JSONTagRegex != "" {
61+
if _, err := regexp.Compile(jtc.JSONTagRegex); err != nil {
62+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("jsonTagRegex"), jtc.JSONTagRegex, fmt.Sprintf("invalid regex: %v", err)))
63+
}
64+
}
65+
66+
return fieldErrors
67+
}
68+
4269
// Default determines whether this Analyzer is on by default, or not.
4370
func (initializer) Default() bool {
4471
return true

pkg/analysis/maxlength/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
return false // For now, CRD only, and so not on by default.

pkg/analysis/nobools/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
// Bools avoidance in the Kube conventions is not a must.

pkg/analysis/nofloats/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
// Floats avoidance in the Kube conventions is a must for spec fields.

pkg/analysis/nomaps/initializer.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ limitations under the License.
1616
package nomaps
1717

1818
import (
19+
"fmt"
20+
1921
"golang.org/x/tools/go/analysis"
22+
"k8s.io/apimachinery/pkg/util/validation/field"
2023
"sigs.k8s.io/kube-api-linter/pkg/config"
2124
)
2225

@@ -39,6 +42,30 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3942
return newAnalyzer(cfg.NoMaps), nil
4043
}
4144

45+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
46+
func (initializer) IsConfigurable() bool {
47+
return true
48+
}
49+
50+
// ValidateConfig is used to validate the configuration in the config.NoMapsConfig struct.
51+
func (initializer) ValidateConfig(cfg any, fldPath *field.Path) field.ErrorList {
52+
nmc, ok := cfg.(config.NoMapsConfig)
53+
if !ok {
54+
return field.ErrorList{field.InternalError(fldPath, fmt.Errorf("incorrect type for passed configuration: %T", cfg))}
55+
}
56+
57+
fieldErrors := field.ErrorList{}
58+
59+
switch nmc.Policy {
60+
case config.NoMapsEnforce, config.NoMapsAllowStringToStringMaps, config.NoMapsIgnore, "":
61+
// Valid values
62+
default:
63+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("policy"), nmc.Policy, fmt.Sprintf("invalid value, must be one of %q, %q, %q or omitted", config.NoMapsEnforce, config.NoMapsAllowStringToStringMaps, config.NoMapsIgnore)))
64+
}
65+
66+
return fieldErrors
67+
}
68+
4269
// Default determines whether this Analyzer is on by default, or not.
4370
func (initializer) Default() bool {
4471
return true

pkg/analysis/nophase/initializer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
3939
return Analyzer, nil
4040
}
4141

42+
// IsConfigurable determines whether or not the Analyzer provides configuration options.
43+
func (initializer) IsConfigurable() bool {
44+
return false
45+
}
46+
4247
// Default determines whether this Analyzer is on by default, or not.
4348
func (initializer) Default() bool {
4449
return true

0 commit comments

Comments
 (0)