@@ -20,11 +20,13 @@ import (
2020 "go/ast"
2121
2222 "golang.org/x/tools/go/analysis"
23+
2324 kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2425 "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2526 "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2627 markershelper "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
2728 "sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
29+ "sigs.k8s.io/kube-api-linter/pkg/markers"
2830)
2931
3032const name = "arrayofstruct"
@@ -38,6 +40,10 @@ var Analyzer = &analysis.Analyzer{
3840 Requires : []* analysis.Analyzer {inspector .Analyzer },
3941}
4042
43+ func init () {
44+ markershelper .DefaultRegistry ().Register (markers .KubebuilderExactlyOneOf )
45+ }
46+
4147func run (pass * analysis.Pass ) (any , error ) {
4248 inspect , ok := pass .ResultOf [inspector .Analyzer ].(inspector.Inspector )
4349 if ! ok {
@@ -74,6 +80,13 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelp
7480 return
7581 }
7682
83+ // Check if the struct has union markers that satisfy the required constraint
84+ if hasExactlyOneOfMarker (structType , markersAccess ) {
85+ // ExactlyOneOf marker enforces that exactly one field is set,
86+ // so we don't need to report an error
87+ return
88+ }
89+
7790 // Check if at least one field in the struct has a required marker
7891 if hasRequiredField (structType , markersAccess ) {
7992 return
@@ -197,3 +210,16 @@ func hasRequiredField(structType *ast.StructType, markersAccess markershelper.Ma
197210
198211 return false
199212}
213+
214+ // hasExactlyOneOfMarker checks if the struct has an ExactlyOneOf marker,
215+ // which satisfies the required field constraint by ensuring exactly one field is set.
216+ func hasExactlyOneOfMarker (structType * ast.StructType , markersAccess markershelper.Markers ) bool {
217+ if structType == nil {
218+ return false
219+ }
220+
221+ // Use StructMarkers to get the set of markers on the struct
222+ markerSet := markersAccess .StructMarkers (structType )
223+
224+ return markerSet .Has (markers .KubebuilderExactlyOneOf )
225+ }
0 commit comments