Skip to content

Commit 91bc47a

Browse files
committed
check only identical marker
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent e339eae commit 91bc47a

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

pkg/analysis/duplicatemarkers/analyzer.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"go/ast"
2121

2222
"golang.org/x/tools/go/analysis"
23+
"k8s.io/apimachinery/pkg/util/sets"
2324

2425
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
2526
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
@@ -47,37 +48,54 @@ func run(pass *analysis.Pass) (any, error) {
4748
}
4849

4950
inspect.InspectFields(func(field *ast.Field, _ []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) {
50-
if len(field.Names) == 0 {
51-
return
52-
}
5351
checkField(pass, field, markersAccess)
5452
})
5553

54+
inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) {
55+
checkTypeSpec(pass, typeSpec, markersAccess)
56+
})
57+
5658
return nil, nil //nolint:nilnil
5759
}
5860

5961
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers) {
60-
set := markersAccess.FieldMarkers(field)
62+
if field == nil || len(field.Names) == 0 {
63+
return
64+
}
6165

62-
fieldName := field.Names[0].Name
66+
fieldMarkers := markersAccess.FieldMarkers(field)
6367

64-
for _, marker := range set.UnsortedList() {
65-
// TODO: Add check whether the marker is a duuplicate or not.
68+
set := sets.New[string]()
69+
70+
for _, marker := range fieldMarkers.UnsortedList() {
71+
if !set.Has(marker.String()) {
72+
set.Insert(marker.String())
73+
continue
74+
}
6675
pass.Report(analysis.Diagnostic{
6776
Pos: field.Pos(),
68-
Message: fmt.Sprintf("%s has duplicated markers %s", fieldName, marker.String()),
69-
SuggestedFixes: []analysis.SuggestedFix{
70-
{
71-
Message: fmt.Sprintf("should remove `// +%s`", marker.String()),
72-
TextEdits: []analysis.TextEdit{
73-
{
74-
Pos: marker.Pos,
75-
End: marker.End,
76-
NewText: nil,
77-
},
78-
},
79-
},
80-
},
77+
Message: fmt.Sprintf("%s has duplicated markers %s", field.Names[0].Name, marker.String()),
78+
})
79+
}
80+
}
81+
82+
func checkTypeSpec(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess markers.Markers) {
83+
if typeSpec == nil {
84+
return
85+
}
86+
87+
typeMarkers := markersAccess.TypeMarkers(typeSpec)
88+
89+
set := sets.New[string]()
90+
91+
for _, marker := range typeMarkers.UnsortedList() {
92+
if !set.Has(marker.String()) {
93+
set.Insert(marker.String())
94+
continue
95+
}
96+
pass.Report(analysis.Diagnostic{
97+
Pos: typeSpec.Pos(),
98+
Message: fmt.Sprintf("%s has duplicated markers %s", typeSpec.Name.Name, marker.String()),
8199
})
82100
}
83101
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Enum string // want "Enum has duplicated markers kubebuilder:validation:Enu
1010

1111
// +kubebuilder:validation:MaxLength=10
1212
// +kubebuilder:validation:MaxLength=11
13-
type MaxLength int // want "MaxLength has duplicated markers kubebuilder:validation:MaxLength"
13+
type MaxLength int
1414

1515
// +kubebuilder:object:root=true
1616
// +kubebuilder:subresource:status
@@ -43,14 +43,14 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
4343

4444
// +kubebuilder:validation:MaxLength=11
4545
// +kubebuilder:validation:MaxLength=10
46-
DuplicatedMaxlength int `json:"maxlength"` // want "DuplicatedMaxlength has duplicated markers kubebuilder:validation:MaxLength"
46+
DuplicatedMaxlength int `json:"maxlength"`
4747

4848
// +listType=map
4949
// +listMapKey=primaryKey
5050
// +listMapKey=secondaryKey
5151
// +listType=map
5252
// +required
53-
DuplicatedListTypeMap Map `json:"duplicatedListTypeMap"` // want "DuplicatedListTypeMap has duplicated markers listType=map, listMapKey=primaryKey, listMapKey=secondaryKey"
53+
DuplicatedListTypeMap Map `json:"duplicatedListTypeMap"` // want "DuplicatedListTypeMap has duplicated markers listType=map"
5454

5555
// +optional
5656
// +kubebuilder:validation:XValidation:rule="self >= 1 && self <= 3",message="must be 1 to 5"

0 commit comments

Comments
 (0)