Skip to content

Commit c5e2e26

Browse files
reorganize code to mimic project conventions
1 parent 8912482 commit c5e2e26

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

pkg/analysis/numericbounds/analyzer.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ func run(pass *analysis.Pass) (any, error) {
6363
}
6464

6565
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelper.Markers) {
66-
if field == nil || len(field.Names) == 0 {
66+
fieldName := utils.FieldName(field)
67+
if fieldName == "" {
6768
return
6869
}
6970

@@ -81,7 +82,6 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelp
8182
return
8283
}
8384

84-
fieldName := utils.FieldName(field)
8585
fieldMarkers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field)
8686

8787
// Determine which markers to look for based on whether the field is a slice
@@ -125,6 +125,31 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelp
125125
checkJavaScriptSafeBounds(pass, field, fieldName, ident.Name, minimum, maximum)
126126
}
127127

128+
// getNumericTypeIdent returns the identifier for int32 or int64 types.
129+
// It handles type aliases by looking up the underlying type.
130+
// Note: This function expects pointers and slices to already be unwrapped.
131+
func getNumericTypeIdent(pass *analysis.Pass, expr ast.Expr) *ast.Ident {
132+
ident, ok := expr.(*ast.Ident)
133+
if !ok {
134+
return nil
135+
}
136+
137+
// Check if it's a basic int32 or int64 type
138+
if ident.Name == "int32" || ident.Name == "int64" {
139+
return ident
140+
}
141+
142+
// Check if it's a type alias to int32 or int64
143+
if !utils.IsBasicType(pass, ident) {
144+
typeSpec, ok := utils.LookupTypeSpec(pass, ident)
145+
if ok {
146+
return getNumericTypeIdent(pass, typeSpec.Type)
147+
}
148+
}
149+
150+
return nil
151+
}
152+
128153
// unwrapType unwraps pointers and slices to get the underlying type.
129154
// Returns the unwrapped type and a boolean indicating if it's a slice.
130155
func unwrapType(expr ast.Expr) (ast.Expr, bool) {
@@ -158,19 +183,6 @@ func getMarkerNames(isSlice bool) (minMarker, maxMarker string) {
158183
return markers.KubebuilderMinimumMarker, markers.KubebuilderMaximumMarker
159184
}
160185

161-
// checkJavaScriptSafeBounds checks if int64 bounds are within JavaScript safe integer range.
162-
func checkJavaScriptSafeBounds(pass *analysis.Pass, field *ast.Field, fieldName, typeName string, minimum, maximum float64) {
163-
if typeName != "int64" {
164-
return
165-
}
166-
167-
if minimum < minSafeInt || maximum > maxSafeInt {
168-
pass.Reportf(field.Pos(),
169-
"field %s of type int64 has bounds [%d, %d] that exceed safe integer range [%d, %d]. Consider using a string type to avoid precision loss in JavaScript clients",
170-
fieldName, int64(minimum), int64(maximum), minSafeInt, maxSafeInt)
171-
}
172-
}
173-
174186
// getMarkerNumericValue extracts the numeric value from the first instance of the marker with the given name.
175187
func getMarkerNumericValue(markerSet markershelper.MarkerSet, markerName string) (float64, error) {
176188
markerList := markerSet.Get(markerName)
@@ -193,27 +205,15 @@ func getMarkerNumericValue(markerSet markershelper.MarkerSet, markerName string)
193205
return value, nil
194206
}
195207

196-
// getNumericTypeIdent returns the identifier for int32 or int64 types.
197-
// It handles type aliases by looking up the underlying type.
198-
// Note: This function expects pointers and slices to already be unwrapped.
199-
func getNumericTypeIdent(pass *analysis.Pass, expr ast.Expr) *ast.Ident {
200-
ident, ok := expr.(*ast.Ident)
201-
if !ok {
202-
return nil
203-
}
204-
205-
// Check if it's a basic int32 or int64 type
206-
if ident.Name == "int32" || ident.Name == "int64" {
207-
return ident
208+
// checkJavaScriptSafeBounds checks if int64 bounds are within JavaScript safe integer range.
209+
func checkJavaScriptSafeBounds(pass *analysis.Pass, field *ast.Field, fieldName, typeName string, minimum, maximum float64) {
210+
if typeName != "int64" {
211+
return
208212
}
209213

210-
// Check if it's a type alias to int32 or int64
211-
if !utils.IsBasicType(pass, ident) {
212-
typeSpec, ok := utils.LookupTypeSpec(pass, ident)
213-
if ok {
214-
return getNumericTypeIdent(pass, typeSpec.Type)
215-
}
214+
if minimum < minSafeInt || maximum > maxSafeInt {
215+
pass.Reportf(field.Pos(),
216+
"field %s of type int64 has bounds [%d, %d] that exceed safe integer range [%d, %d]. Consider using a string type to avoid precision loss in JavaScript clients",
217+
fieldName, int64(minimum), int64(maximum), minSafeInt, maxSafeInt)
216218
}
217-
218-
return nil
219219
}

0 commit comments

Comments
 (0)