Skip to content

Commit 158c03d

Browse files
committed
add FieldName API
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 2c83ed3 commit 158c03d

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

pkg/analysis/helpers/inspector/analyzer_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2727
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
28+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2829
)
2930

3031
func TestInspector(t *testing.T) {
@@ -49,14 +50,7 @@ func run(pass *analysis.Pass) (any, error) {
4950
}
5051

5152
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers) {
52-
var fieldName string
53-
if len(field.Names) > 0 {
54-
fieldName = field.Names[0].Name
55-
} else if ident, ok := field.Type.(*ast.Ident); ok {
56-
fieldName = ident.Name
57-
}
58-
59-
pass.Reportf(field.Pos(), "field: %v", fieldName)
53+
pass.Reportf(field.Pos(), "field: %v", utils.FieldName(field))
6054

6155
if jsonTagInfo.Name != "" {
6256
pass.Reportf(field.Pos(), "json tag: %v", jsonTagInfo.Name)

pkg/analysis/jsontags/analyzer.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2525
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
27+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2728
"sigs.k8s.io/kube-api-linter/pkg/config"
2829

2930
"golang.org/x/tools/go/analysis"
@@ -75,15 +76,13 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
7576
}
7677

7778
func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.FieldTagInfo) {
78-
var prefix string
79+
prefix := "embedded field %s"
7980
if len(field.Names) > 0 && field.Names[0] != nil {
80-
prefix = fmt.Sprintf("field %s", field.Names[0].Name)
81-
} else if ident, ok := field.Type.(*ast.Ident); ok {
82-
prefix = fmt.Sprintf("embedded field %s", ident.Name)
81+
prefix = "field %s"
8382
}
8483

8584
if tagInfo.Missing {
86-
pass.Reportf(field.Pos(), "%s is missing json tag", prefix)
85+
pass.Reportf(field.Pos(), "%s is missing json tag", fmt.Sprintf(prefix, utils.FieldName(field)))
8786
return
8887
}
8988

@@ -92,13 +91,13 @@ func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, tagInfo ext
9291
}
9392

9493
if tagInfo.Name == "" {
95-
pass.Reportf(field.Pos(), "%s has empty json tag", prefix)
94+
pass.Reportf(field.Pos(), "%s has empty json tag", fmt.Sprintf(prefix, utils.FieldName(field)))
9695
return
9796
}
9897

9998
matched := a.jsonTagRegex.Match([]byte(tagInfo.Name))
10099
if !matched {
101-
pass.Reportf(field.Pos(), "%s json tag does not match pattern %q: %s", prefix, a.jsonTagRegex.String(), tagInfo.Name)
100+
pass.Reportf(field.Pos(), "%s json tag does not match pattern %q: %s", fmt.Sprintf(prefix, utils.FieldName(field)), a.jsonTagRegex.String(), tagInfo.Name)
102101
}
103102
}
104103

pkg/analysis/utils/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ func LookupTypeSpec(pass *analysis.Pass, ident *ast.Ident) (*ast.TypeSpec, bool)
7777
return nil, false
7878
}
7979

80+
// FieldName returns the name of the field. If the field has a name, it returns that name.
81+
// If the field is embedded and it can be converted to an identifier, it returns the name of the identifier.
82+
func FieldName(field *ast.Field) string {
83+
if len(field.Names) > 0 {
84+
return field.Names[0].Name
85+
}
86+
87+
ident, ok := field.Type.(*ast.Ident)
88+
if !ok {
89+
return ""
90+
}
91+
92+
return ident.Name
93+
}
94+
8095
func getFilesForType(pass *analysis.Pass, ident *ast.Ident) (*token.File, *ast.File) {
8196
namedType, ok := pass.TypesInfo.TypeOf(ident).(*types.Named)
8297
if !ok {

0 commit comments

Comments
 (0)