Skip to content

Commit 875b36c

Browse files
committed
refactor(markerscope): remove support for float/number types
Signed-off-by: nayuta-ai <nayuta723@gmail.com>
1 parent 7db9920 commit 875b36c

File tree

10 files changed

+39
-69
lines changed

10 files changed

+39
-69
lines changed

pkg/analysis/markerscope/analyzer.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ func init() {
4949
}
5050

5151
type analyzer struct {
52-
markerRules map[string]MarkerScopeRule
53-
policy MarkerScopePolicy
54-
allowDangerousTypes bool
52+
markerRules map[string]MarkerScopeRule
53+
policy MarkerScopePolicy
5554
}
5655

5756
// newAnalyzer creates a new analyzer.
@@ -78,9 +77,8 @@ func newAnalyzer(cfg *MarkerScopeConfig) *analysis.Analyzer {
7877
maps.Copy(rules, customRules) // Add custom markers
7978

8079
a := &analyzer{
81-
markerRules: rules,
82-
policy: cfg.Policy,
83-
allowDangerousTypes: cfg.AllowDangerousTypes,
80+
markerRules: rules,
81+
policy: cfg.Policy,
8482
}
8583

8684
// Register all markers (both default and custom) with the markers helper
@@ -122,7 +120,6 @@ func defaultConfig(cfg *MarkerScopeConfig) {
122120
if cfg.Policy == "" {
123121
cfg.Policy = MarkerScopePolicyWarn
124122
}
125-
// allowDangerousTypes defaults to false (zero value)
126123
// overrideMarkers and customMarkers default to empty (zero value)
127124
}
128125

@@ -211,7 +208,7 @@ func (a *analyzer) checkSingleTypeMarkers(pass *analysis.Pass, typeSpec *ast.Typ
211208
}
212209

213210
// Check type constraints if present
214-
a.checkTypeConstraintViolation(pass, typeSpec, marker, rule, a.allowDangerousTypes)
211+
a.checkTypeConstraintViolation(pass, typeSpec, marker, rule)
215212
}
216213
}
217214

@@ -242,7 +239,7 @@ func (a *analyzer) reportFieldScopeViolation(pass *analysis.Pass, field *ast.Fie
242239

243240
// checkFieldTypeConstraintViolation checks and reports type constraint violations for field markers.
244241
func (a *analyzer) checkFieldTypeConstraintViolation(pass *analysis.Pass, field *ast.Field, marker markershelper.Marker, rule MarkerScopeRule) {
245-
if err := a.validateFieldTypeConstraint(pass, field, rule, a.allowDangerousTypes); err != nil {
242+
if err := a.validateFieldTypeConstraint(pass, field, rule); err != nil {
246243
var fixes []analysis.SuggestedFix
247244

248245
if a.policy == MarkerScopePolicySuggestFix {
@@ -301,8 +298,8 @@ func (a *analyzer) reportTypeScopeViolation(pass *analysis.Pass, typeSpec *ast.T
301298
}
302299

303300
// checkTypeConstraintViolation checks and reports type constraint violations.
304-
func (a *analyzer) checkTypeConstraintViolation(pass *analysis.Pass, typeSpec *ast.TypeSpec, marker markershelper.Marker, rule MarkerScopeRule, allowDangerousTypes bool) {
305-
if err := a.validateTypeSpecTypeConstraint(pass, typeSpec, rule.TypeConstraint, allowDangerousTypes); err != nil {
301+
func (a *analyzer) checkTypeConstraintViolation(pass *analysis.Pass, typeSpec *ast.TypeSpec, marker markershelper.Marker, rule MarkerScopeRule) {
302+
if err := a.validateTypeSpecTypeConstraint(pass, typeSpec, rule.TypeConstraint); err != nil {
306303
var fixes []analysis.SuggestedFix
307304

308305
if a.policy == MarkerScopePolicySuggestFix {
@@ -339,14 +336,14 @@ func (a *analyzer) checkTypeConstraintViolation(pass *analysis.Pass, typeSpec *a
339336
}
340337

341338
// validateFieldTypeConstraint validates that a field's type matches the type constraint.
342-
func (a *analyzer) validateFieldTypeConstraint(pass *analysis.Pass, field *ast.Field, rule MarkerScopeRule, allowDangerousTypes bool) error {
339+
func (a *analyzer) validateFieldTypeConstraint(pass *analysis.Pass, field *ast.Field, rule MarkerScopeRule) error {
343340
// Get the type of the field
344341
tv, ok := pass.TypesInfo.Types[field.Type]
345342
if !ok {
346343
return nil // Skip if we can't determine the type
347344
}
348345

349-
if err := validateTypeAgainstConstraint(tv.Type, rule.TypeConstraint, allowDangerousTypes); err != nil {
346+
if err := validateTypeAgainstConstraint(tv.Type, rule.TypeConstraint); err != nil {
350347
return err
351348
}
352349

@@ -361,7 +358,7 @@ func (a *analyzer) validateFieldTypeConstraint(pass *analysis.Pass, field *ast.F
361358
}
362359

363360
// validateTypeSpecTypeConstraint validates that a type spec's type matches the type constraint.
364-
func (a *analyzer) validateTypeSpecTypeConstraint(pass *analysis.Pass, typeSpec *ast.TypeSpec, tc *TypeConstraint, allowDangerousTypes bool) error {
361+
func (a *analyzer) validateTypeSpecTypeConstraint(pass *analysis.Pass, typeSpec *ast.TypeSpec, tc *TypeConstraint) error {
365362
// Get the type of the type spec
366363
obj := pass.TypesInfo.Defs[typeSpec.Name]
367364
if obj == nil {
@@ -373,21 +370,14 @@ func (a *analyzer) validateTypeSpecTypeConstraint(pass *analysis.Pass, typeSpec
373370
return nil
374371
}
375372

376-
return validateTypeAgainstConstraint(typeName.Type(), tc, allowDangerousTypes)
373+
return validateTypeAgainstConstraint(typeName.Type(), tc)
377374
}
378375

379376
// validateTypeAgainstConstraint validates that a Go type satisfies the type constraint.
380-
func validateTypeAgainstConstraint(t types.Type, tc *TypeConstraint, allowDangerousTypes bool) error {
377+
func validateTypeAgainstConstraint(t types.Type, tc *TypeConstraint) error {
381378
// Get the schema type from the Go type
382379
schemaType := getSchemaType(t)
383380

384-
// Check if dangerous types are disallowed
385-
if !allowDangerousTypes && schemaType == SchemaTypeNumber {
386-
// Get the underlying type for better error messages
387-
underlyingType := getUnderlyingType(t)
388-
return &DengerousTypeError{Type: underlyingType.String()}
389-
}
390-
391381
if tc == nil {
392382
return nil
393383
}
@@ -403,7 +393,7 @@ func validateTypeAgainstConstraint(t types.Type, tc *TypeConstraint, allowDanger
403393
if tc.ElementConstraint != nil && schemaType == SchemaTypeArray {
404394
elemType := getElementType(t)
405395
if elemType != nil {
406-
if err := validateTypeAgainstConstraint(elemType, tc.ElementConstraint, allowDangerousTypes); err != nil {
396+
if err := validateTypeAgainstConstraint(elemType, tc.ElementConstraint); err != nil {
407397
return &InvalidElementConstraintError{Err: err}
408398
}
409399
}

pkg/analysis/markerscope/analyzer_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func TestAnalyzerWithDefaultConfig(t *testing.T) {
2626
testdata := analysistest.TestData()
2727
// Test with nil config - should use all defaults:
2828
// - Policy: Warn
29-
// - AllowDangerousTypes: false
3029
// - OverrideMarkers: empty (use built-in defaults)
3130
// - CustomMarkers: empty
3231
analyzer, err := markerscope.Initializer().Init(&markerscope.MarkerScopeConfig{})

pkg/analysis/markerscope/config.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ type MarkerScopeConfig struct {
108108
// allowedSchemaTypes: ["string"]
109109
CustomMarkers []MarkerScopeRule `json:"customMarkers,omitempty"`
110110

111-
// AllowDangerousTypes specifies if dangerous types are allowed.
112-
// If true, dangerous types are allowed.
113-
// If false, dangerous types are not allowed.
114-
AllowDangerousTypes bool `json:"allowDangerousTypes,omitempty"`
115-
116111
// Policy determines whether to suggest fixes or just warn.
117112
Policy MarkerScopePolicy `json:"policy,omitempty"`
118113
}
@@ -196,35 +191,35 @@ func addNumericMarkers(rules map[string]MarkerScopeRule) {
196191
Scope: AnyScope,
197192
StrictTypeConstraint: true,
198193
TypeConstraint: &TypeConstraint{
199-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
194+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
200195
},
201196
},
202197
markers.KubebuilderMaximumMarker: {
203198
Scope: AnyScope,
204199
StrictTypeConstraint: true,
205200
TypeConstraint: &TypeConstraint{
206-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
201+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
207202
},
208203
},
209204
markers.KubebuilderExclusiveMaximumMarker: {
210205
Scope: AnyScope,
211206
StrictTypeConstraint: true,
212207
TypeConstraint: &TypeConstraint{
213-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
208+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
214209
},
215210
},
216211
markers.KubebuilderExclusiveMinimumMarker: {
217212
Scope: AnyScope,
218213
StrictTypeConstraint: true,
219214
TypeConstraint: &TypeConstraint{
220-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
215+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
221216
},
222217
},
223218
markers.KubebuilderMultipleOfMarker: {
224219
Scope: AnyScope,
225220
StrictTypeConstraint: true,
226221
TypeConstraint: &TypeConstraint{
227-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
222+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
228223
},
229224
},
230225
}
@@ -392,7 +387,7 @@ func addArrayItemsNumericMarkers(rules map[string]MarkerScopeRule) {
392387
TypeConstraint: &TypeConstraint{
393388
AllowedSchemaTypes: []SchemaType{SchemaTypeArray},
394389
ElementConstraint: &TypeConstraint{
395-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
390+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
396391
},
397392
},
398393
},
@@ -402,7 +397,7 @@ func addArrayItemsNumericMarkers(rules map[string]MarkerScopeRule) {
402397
TypeConstraint: &TypeConstraint{
403398
AllowedSchemaTypes: []SchemaType{SchemaTypeArray},
404399
ElementConstraint: &TypeConstraint{
405-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
400+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
406401
},
407402
},
408403
},
@@ -412,7 +407,7 @@ func addArrayItemsNumericMarkers(rules map[string]MarkerScopeRule) {
412407
TypeConstraint: &TypeConstraint{
413408
AllowedSchemaTypes: []SchemaType{SchemaTypeArray},
414409
ElementConstraint: &TypeConstraint{
415-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
410+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
416411
},
417412
},
418413
},
@@ -422,7 +417,7 @@ func addArrayItemsNumericMarkers(rules map[string]MarkerScopeRule) {
422417
TypeConstraint: &TypeConstraint{
423418
AllowedSchemaTypes: []SchemaType{SchemaTypeArray},
424419
ElementConstraint: &TypeConstraint{
425-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
420+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
426421
},
427422
},
428423
},
@@ -432,7 +427,7 @@ func addArrayItemsNumericMarkers(rules map[string]MarkerScopeRule) {
432427
TypeConstraint: &TypeConstraint{
433428
AllowedSchemaTypes: []SchemaType{SchemaTypeArray},
434429
ElementConstraint: &TypeConstraint{
435-
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger, SchemaTypeNumber},
430+
AllowedSchemaTypes: []SchemaType{SchemaTypeInteger},
436431
},
437432
},
438433
},

pkg/analysis/markerscope/errors.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ func (e *MarkerShouldBeOnTypeDefinitionError) Error() string {
6969
return fmt.Sprintf("marker should be declared on the type definition of %s instead of the field", e.TypeName)
7070
}
7171

72-
// DengerousTypeError represents an error when a dangerous type is used.
73-
type DengerousTypeError struct {
74-
Type string
75-
}
76-
77-
func (e *DengerousTypeError) Error() string {
78-
return fmt.Sprintf("type %s is dangerous and not allowed (set allowDangerousTypes to true to permit)", e.Type)
79-
}
80-
8172
// TypeNotAllowedError represents an error when a type is not allowed.
8273
type TypeNotAllowedError struct {
8374
Type SchemaType

pkg/analysis/markerscope/initializer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func validateTypeConstraint(tc *TypeConstraint) error {
157157

158158
func isValidSchemaType(st SchemaType) bool {
159159
switch st {
160-
case SchemaTypeInteger, SchemaTypeNumber, SchemaTypeString, SchemaTypeBoolean, SchemaTypeArray, SchemaTypeObject:
160+
case SchemaTypeInteger, SchemaTypeString, SchemaTypeBoolean, SchemaTypeArray, SchemaTypeObject:
161161
return true
162162
default:
163163
return false

pkg/analysis/markerscope/initializer_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ var _ = Describe("markerscope initializer", func() {
166166
TypeConstraint: &markerscope.TypeConstraint{
167167
AllowedSchemaTypes: []markerscope.SchemaType{
168168
markerscope.SchemaTypeInteger,
169-
markerscope.SchemaTypeNumber,
170169
},
171170
},
172171
},

pkg/analysis/markerscope/schema.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ type SchemaType string
2323
const (
2424
// SchemaTypeInteger represents integer types (int, int32, int64, uint, etc.)
2525
SchemaTypeInteger SchemaType = "integer"
26-
// SchemaTypeNumber represents floating-point types (float32, float64).
27-
SchemaTypeNumber SchemaType = "number"
2826
// SchemaTypeString represents string types.
2927
SchemaTypeString SchemaType = "string"
3028
// SchemaTypeBoolean represents boolean types.
@@ -74,11 +72,9 @@ func getBasicTypeSchema(bt *types.Basic) SchemaType {
7472
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
7573
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
7674
return SchemaTypeInteger
77-
case types.Float32, types.Float64:
78-
return SchemaTypeNumber
7975
case types.String:
8076
return SchemaTypeString
81-
case types.Invalid, types.Uintptr, types.Complex64, types.Complex128,
77+
case types.Float32, types.Float64, types.Invalid, types.Uintptr, types.Complex64, types.Complex128,
8278
types.UnsafePointer, types.UntypedBool, types.UntypedInt, types.UntypedRune,
8379
types.UntypedFloat, types.UntypedComplex, types.UntypedString, types.UntypedNil:
8480
// These types are not supported in OpenAPI schemas

pkg/analysis/markerscope/testdata/src/a/items.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type ObjectArrayTypeNoMarker []map[string]string
4747
type GeneralArrayTypeNoMarker []string
4848

4949
// Invalid: items:Maximum on string array type
50-
// +kubebuilder:validation:items:Maximum=100 // want `marker "kubebuilder:validation:items:Maximum": array element: type string is not allowed \(expected one of: \[integer number\]\)`
50+
// +kubebuilder:validation:items:Maximum=100 // want `marker "kubebuilder:validation:items:Maximum": array element: type string is not allowed \(expected one of: \[integer\]\)`
5151
type InvalidItemsMaximumOnStringArrayType []string
5252

5353
// Invalid: items:Pattern on int array type
@@ -128,7 +128,7 @@ type ArrayItemsMarkersFieldTest struct {
128128
InvalidItemsFormatOnGeneralArrayType GeneralArrayTypeNoMarker `json:"invalidItemsFormatOnGeneralArrayType"`
129129

130130
// Invalid: items:Maximum on string array (element type mismatch)
131-
// +kubebuilder:validation:items:Maximum=100 // want `marker "kubebuilder:validation:items:Maximum": array element: type string is not allowed \(expected one of: \[integer number\]\)`
131+
// +kubebuilder:validation:items:Maximum=100 // want `marker "kubebuilder:validation:items:Maximum": array element: type string is not allowed \(expected one of: \[integer\]\)`
132132
InvalidItemsMaximumOnStringArray []string `json:"invalidItemsMaximumOnStringArray"`
133133

134134
// Invalid: items:Pattern on int array (element type mismatch)

pkg/analysis/markerscope/testdata/src/a/numeric.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ type MaximumType int64
3030
// +kubebuilder:validation:MultipleOf=3
3131
type MultipleOfType int32
3232

33-
// Valid: Float type with numeric markers
34-
// +kubebuilder:validation:Minimum=0.0 // want `marker "kubebuilder:validation:Minimum": type float64 is dangerous and not allowed \(set allowDangerousTypes to true to permit\)`
35-
// +kubebuilder:validation:Maximum=1.0 // want `marker "kubebuilder:validation:Maximum": type float64 is dangerous and not allowed \(set allowDangerousTypes to true to permit\)`
33+
// Invalid: Float type with numeric markers (float types are not supported)
34+
// +kubebuilder:validation:Minimum=0.0 // want `marker "kubebuilder:validation:Minimum": type is not allowed \(expected one of: \[integer\]\)`
35+
// +kubebuilder:validation:Maximum=1.0 // want `marker "kubebuilder:validation:Maximum": type is not allowed \(expected one of: \[integer\]\)`
3636
type FloatType float64
3737

3838
// Invalid: Minimum marker on string type
39-
// +kubebuilder:validation:Minimum=0 // want `marker "kubebuilder:validation:Minimum": type string is not allowed \(expected one of: \[integer number\]\)`
39+
// +kubebuilder:validation:Minimum=0 // want `marker "kubebuilder:validation:Minimum": type string is not allowed \(expected one of: \[integer\]\)`
4040
type InvalidMinimumOnStringType string
4141

4242
// Invalid: Maximum marker on boolean type
43-
// +kubebuilder:validation:Maximum=100 // want `marker "kubebuilder:validation:Maximum": type boolean is not allowed \(expected one of: \[integer number\]\)`
43+
// +kubebuilder:validation:Maximum=100 // want `marker "kubebuilder:validation:Maximum": type boolean is not allowed \(expected one of: \[integer\]\)`
4444
type InvalidMaximumOnBoolType bool
4545

4646
type NumericMarkersFieldTest struct {
@@ -107,20 +107,20 @@ type NumericMarkersFieldTest struct {
107107
InvalidMultipleOfOnNumericType NumericType `json:"invalidMultipleOfOnNumericType"`
108108

109109
// Invalid: Minimum marker on string field
110-
// +kubebuilder:validation:Minimum=0 // want `marker "kubebuilder:validation:Minimum": type string is not allowed \(expected one of: \[integer number\]\)`
110+
// +kubebuilder:validation:Minimum=0 // want `marker "kubebuilder:validation:Minimum": type string is not allowed \(expected one of: \[integer\]\)`
111111
InvalidMinimumOnString string `json:"invalidMinimumOnString"`
112112

113113
// Invalid: Maximum marker on boolean field
114-
// +kubebuilder:validation:Maximum=100 // want `marker "kubebuilder:validation:Maximum": type boolean is not allowed \(expected one of: \[integer number\]\)`
114+
// +kubebuilder:validation:Maximum=100 // want `marker "kubebuilder:validation:Maximum": type boolean is not allowed \(expected one of: \[integer\]\)`
115115
InvalidMaximumOnBool bool `json:"invalidMaximumOnBool"`
116116

117117
// Invalid: MultipleOf marker on array field
118-
// +kubebuilder:validation:MultipleOf=5 // want `marker "kubebuilder:validation:MultipleOf": type array is not allowed \(expected one of: \[integer number\]\)`
118+
// +kubebuilder:validation:MultipleOf=5 // want `marker "kubebuilder:validation:MultipleOf": type array is not allowed \(expected one of: \[integer\]\)`
119119
InvalidMultipleOfOnArray []int32 `json:"invalidMultipleOfOnArray"`
120120

121121
// Invalid: Using invalid named type
122-
// +kubebuilder:validation:Minimum=50 // want `marker "kubebuilder:validation:Minimum": type string is not allowed \(expected one of: \[integer number\]\)`
123-
// +kubebuilder:validation:Maximum=100 // want `marker "kubebuilder:validation:Maximum": type string is not allowed \(expected one of: \[integer number\]\)`
122+
// +kubebuilder:validation:Minimum=50 // want `marker "kubebuilder:validation:Minimum": type string is not allowed \(expected one of: \[integer\]\)`
123+
// +kubebuilder:validation:Maximum=100 // want `marker "kubebuilder:validation:Maximum": type string is not allowed \(expected one of: \[integer\]\)`
124124
InvalidMinimumOnStringTyped InvalidMinimumOnStringType `json:"invalidMinimumOnStringTyped"`
125125

126126
// Invalid: Using invalid named type

pkg/analysis/markerscope/testdata/src/a/numeric.go.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type MaximumType int64
3535
// +kubebuilder:validation:MultipleOf=3
3636
type MultipleOfType int32
3737

38-
// Valid: Float type with numeric markers
38+
// Invalid: Float type with numeric markers (float types are not supported)
3939
type FloatType float64
4040

4141
// Invalid: Minimum marker on string type

0 commit comments

Comments
 (0)