Skip to content

Commit f346928

Browse files
committed
combine markers
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 68435a3 commit f346928

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

pkg/analysis/duplicatemarkers/analyzer.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"go/ast"
2121
"go/token"
22+
"go/types"
2223

2324
"golang.org/x/tools/go/analysis"
2425

@@ -63,20 +64,46 @@ func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Mar
6364
return
6465
}
6566

66-
fieldMarkers := markersAccess.FieldMarkers(field)
67+
markerSet := collectMarkers(pass, markersAccess, field)
6768

68-
markerSet := markers.NewMarkerSet()
69+
seen := markers.NewMarkerSet()
6970

70-
for _, marker := range fieldMarkers.UnsortedList() {
71-
if !markerSet.HasWithValue(marker.String()) {
72-
markerSet.Insert(marker)
71+
for _, marker := range markerSet.UnsortedList() {
72+
if !seen.HasWithValue(marker.String()) {
73+
seen.Insert(marker)
7374
continue
7475
}
7576

7677
report(pass, field.Pos(), field.Names[0].Name, marker)
7778
}
7879
}
7980

81+
func collectMarkers(pass *analysis.Pass, markersAccess markers.Markers, field *ast.Field) markers.MarkerSet {
82+
markers := markersAccess.FieldMarkers(field)
83+
84+
if _, ok := pass.TypesInfo.TypeOf(field.Type).(*types.Basic); ok {
85+
return markers
86+
}
87+
88+
ident, ok := field.Type.(*ast.Ident)
89+
if !ok {
90+
return markers
91+
}
92+
93+
typeSpec, ok := ident.Obj.Decl.(*ast.TypeSpec)
94+
if !ok {
95+
return markers
96+
}
97+
98+
typeMarkers := markersAccess.TypeMarkers(typeSpec)
99+
100+
// duplicatemarkers removes duplicate markers without the first line.
101+
// By inserting the field markers after the type markers, it allows the markers to be removed from the field.
102+
typeMarkers.Insert(markers.UnsortedList()...)
103+
104+
return typeMarkers
105+
}
106+
80107
func checkTypeSpec(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess markers.Markers) {
81108
if typeSpec == nil {
82109
return

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
5555
// +kubebuilder:validation:MaxLength=10
5656
DuplicatedMaxLength int `json:"duplicatedMaxLength"` // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
5757

58+
// +kubebuilder:validation:MaxLength=10
59+
DuplicatedMaxLengthIncludingTypeMarker MaxLength `json:"duplicatedMaxLengthIncludingTypeMarker"` // want "DuplicatedMaxLengthIncludingTypeMarker has duplicated markers kubebuilder:validation:MaxLength=10"
60+
5861
// +listType=map
5962
// +listMapKey=primaryKey
6063
// +listMapKey=secondaryKey

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type DuplicateMarkerSpec struct { // want "DuplicateMarkerSpec has duplicated ma
4949
// +kubebuilder:validation:MaxLength=10
5050
DuplicatedMaxLength int `json:"duplicatedMaxLength"` // want "DuplicatedMaxLength has duplicated markers kubebuilder:validation:MaxLength=10"
5151

52+
DuplicatedMaxLengthIncludingTypeMarker MaxLength `json:"duplicatedMaxLengthIncludingTypeMarker"` // want "DuplicatedMaxLengthIncludingTypeMarker has duplicated markers kubebuilder:validation:MaxLength=10"
53+
5254
// +listType=map
5355
// +listMapKey=primaryKey
5456
// +listMapKey=secondaryKey

0 commit comments

Comments
 (0)