Skip to content

Commit 7b1857d

Browse files
committed
Use StructMarkers() to check struct-level markers.
1 parent df6a56e commit 7b1857d

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

pkg/analysis/arrayofstruct/analyzer.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package arrayofstruct
1818
import (
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
}

pkg/analysis/arrayofstruct/testdata/src/a/a.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ type ValidWithExactlyOneOf struct {
156156
Items []ValidExactlyOneOfItem
157157
}
158158

159+
// +kubebuilder:validation:ExactlyOneOf=FieldA;FieldB
159160
type ValidExactlyOneOfItem struct {
160-
// +kubebuilder:validation:ExactlyOneOf=FieldA;FieldB
161-
// No required fields, but the marker enforces exactly one is set
162161
FieldA *string `json:"fieldA,omitempty"`
163162
FieldB *string `json:"fieldB,omitempty"`
164163
}

0 commit comments

Comments
 (0)