@@ -18,8 +18,6 @@ package arrayofstruct
1818import (
1919 "fmt"
2020 "go/ast"
21- "regexp"
22- "slices"
2321
2422 "golang.org/x/tools/go/analysis"
2523
@@ -78,7 +76,7 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelp
7876 }
7977
8078 // Check if the struct has union markers that satisfy the required constraint
81- if hasExactlyOneOfMarker (structType ) {
79+ if hasExactlyOneOfMarker (structType , markersAccess ) {
8280 // ExactlyOneOf marker enforces that exactly one field is set,
8381 // so we don't need to report an error
8482 return
@@ -221,32 +219,13 @@ func hasRequiredField(structType *ast.StructType, markersAccess markershelper.Ma
221219
222220// hasExactlyOneOfMarker checks if the struct has an ExactlyOneOf marker,
223221// which satisfies the required field constraint by ensuring exactly one field is set.
224- func hasExactlyOneOfMarker (structType * ast.StructType ) bool {
225- if structType . Fields == nil {
222+ func hasExactlyOneOfMarker (structType * ast.StructType , markersAccess markershelper. Markers ) bool {
223+ if structType == nil {
226224 return false
227225 }
228226
229- for _ , field := range structType .Fields .List {
230- var markers []string
231-
232- if field .Doc != nil {
233- for _ , comment := range field .Doc .List {
234- markers = append (markers , comment .Text )
235- }
236- }
237- // Check for ExactlyOneOf marker
238- if hasMarkerPattern (markers , "ExactlyOneOf" ) {
239- return true
240- }
241- }
242-
243- return false
244- }
245-
246- // hasMarkerPattern checks if any of the markers match the given pattern.
247- func hasMarkerPattern (markers []string , markerName string ) bool {
248- pattern := fmt .Sprintf (`\+kubebuilder:validation:%s=` , markerName )
249- re := regexp .MustCompile (pattern )
227+ // Use StructMarkers to get the set of markers on the struct
228+ markerSet := markersAccess .StructMarkers (structType )
250229
251- return slices . ContainsFunc ( markers , re . MatchString )
230+ return markerSet . Has ( "kubebuilder:validation:ExactlyOneOf" )
252231}
0 commit comments