Skip to content

Commit 3cc898b

Browse files
committed
Ensure types from other files can be reported
1 parent 350d7ca commit 3cc898b

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

pkg/analysis/optionalfields/analyzer.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2727
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
2828
markershelper "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
29+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2930
"sigs.k8s.io/kube-api-linter/pkg/config"
3031
"sigs.k8s.io/kube-api-linter/pkg/markers"
3132

@@ -231,28 +232,28 @@ func (a *analyzer) checkFieldPointersPreferenceWhenRequired(pass *analysis.Pass,
231232
return
232233
}
233234

234-
if ident.Obj != nil {
235-
// The field is not a simple type, check the object if it is a type spec.
236-
decl, ok := ident.Obj.Decl.(*ast.TypeSpec)
237-
if !ok {
238-
return
239-
}
240-
241-
a.checkFieldPointersPreferenceWhenRequiredIdentObj(pass, field, fieldName, isStarExpr, decl, markersAccess, jsonTags)
242-
243-
return
244-
}
245-
246235
switch ident.Name {
247236
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
248237
a.checkFieldPointersPreferenceWhenRequiredInteger(pass, field, fieldName, isStarExpr, markersAccess, jsonTags)
238+
return
249239
case "string":
250240
a.checkFieldPointersPreferenceWhenRequiredString(pass, field, fieldName, isStarExpr, markersAccess, jsonTags)
241+
return
251242
case "bool":
252243
a.checkFieldPointersPreferenceWhenRequiredBool(pass, field, fieldName, isStarExpr, jsonTags)
244+
return
253245
case "float32", "float64":
254246
a.checkFieldPointersPreferenceWhenRequiredFloat(pass, field, fieldName, isStarExpr, markersAccess, jsonTags)
247+
return
255248
}
249+
250+
// The field is not a simple type in our switch, so try looking up the type spec.
251+
typeSpec, ok := utils.LookupTypeSpec(pass, ident)
252+
if !ok {
253+
return
254+
}
255+
256+
a.checkFieldPointersPreferenceWhenRequiredIdentObj(pass, field, fieldName, isStarExpr, typeSpec, markersAccess, jsonTags)
256257
}
257258

258259
func (a *analyzer) checkFieldPointersPreferenceWhenRequiredIdentObj(pass *analysis.Pass, field *ast.Field, fieldName string, isStarExpr bool, decl *ast.TypeSpec, markersAccess markershelper.Markers, jsonTags extractjsontags.FieldTagInfo) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ type A struct {
7474
// +optional
7575
PointerStringAlias *StringAlias `json:"pointerStringAlias,omitempty"`
7676

77+
// PointerStringAliasFromAnotherFile is a pointer string alias field.
78+
// It proves that we can use types defined in other files.
79+
// +optional
80+
StringAliasFromAnotherFile *StringAliasFromAnotherFile `json:"pointerStringAliasFromAnotherFile,omitempty"`
81+
7782
// PointerIntAlias is a pointer int alias field.
7883
// +optional
7984
PointerIntAlias *IntAlias `json:"pointerIntAlias,omitempty"`

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ type A struct {
7474
// +optional
7575
PointerStringAlias *StringAlias `json:"pointerStringAlias,omitempty"`
7676

77+
// PointerStringAliasFromAnotherFile is a pointer string alias field.
78+
// It proves that we can use types defined in other files.
79+
// +optional
80+
StringAliasFromAnotherFile *StringAliasFromAnotherFile `json:"pointerStringAliasFromAnotherFile,omitempty"`
81+
7782
// PointerIntAlias is a pointer int alias field.
7883
// +optional
7984
PointerIntAlias *IntAlias `json:"pointerIntAlias,omitempty"`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package a
2+
3+
type StringAliasFromAnotherFile string

pkg/analysis/optionalfields/testdata/src/b/a.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ type A struct {
179179
// +optional
180180
StructWithRequiredFields C `json:"structWithRequiredFields,omitempty"` // want "field StructWithRequiredFields is optional, but contains required field\\(s\\) and should be a pointer"
181181

182+
// structWithRequiredFieldsFromAnotherFile is a struct field.
183+
// +optional
184+
StructWithRequiredFieldsFromAnotherFile StructWithRequiredField `json:"structWithRequiredFields,omitempty"` // want "field StructWithRequiredFieldsFromAnotherFile is optional, but contains required field\\(s\\) and should be a pointer"
185+
182186
// pointerStructWithOptionalFields is a pointer struct field.
183187
// +optional
184188
PointerStructWithOptionalFields *B `json:"pointerStructWithOptionalFields,omitempty"` // want "field PointerStructWithOptionalFields is optional, and contains no required field\\(s\\) and does not need to be a pointer"
@@ -187,6 +191,10 @@ type A struct {
187191
// +optional
188192
PointerStructWithRequiredFields *C `json:"pointerStructWithRequiredFields,omitempty"`
189193

194+
// pointerStructWithRequiredFromAnotherFile is a pointer struct field.
195+
// +optional
196+
PointerStructWithRequiredFromAnotherFile *StructWithRequiredField `json:"pointerStructWithRequiredFromAnotherFile,omitempty"`
197+
190198
// bool is a boolean field.
191199
// +optional
192200
Bool bool `json:"bool,omitempty"` // want "field Bool is an optional boolean and should be a pointer"

pkg/analysis/optionalfields/testdata/src/b/a.go.golden

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ type A struct {
180180
// +optional
181181
StructWithRequiredFields *C `json:"structWithRequiredFields,omitempty"` // want "field StructWithRequiredFields is optional, but contains required field\\(s\\) and should be a pointer"
182182

183+
// structWithRequiredFieldsFromAnotherFile is a struct field.
184+
// +optional
185+
StructWithRequiredFieldsFromAnotherFile *StructWithRequiredField `json:"structWithRequiredFields,omitempty"` // want "field StructWithRequiredFieldsFromAnotherFile is optional, but contains required field\\(s\\) and should be a pointer"
186+
183187
// pointerStructWithOptionalFields is a pointer struct field.
184188
// +optional
185189
PointerStructWithOptionalFields B `json:"pointerStructWithOptionalFields,omitempty"` // want "field PointerStructWithOptionalFields is optional, and contains no required field\\(s\\) and does not need to be a pointer"
@@ -188,6 +192,10 @@ type A struct {
188192
// +optional
189193
PointerStructWithRequiredFields *C `json:"pointerStructWithRequiredFields,omitempty"`
190194

195+
// pointerStructWithRequiredFromAnotherFile is a pointer struct field.
196+
// +optional
197+
PointerStructWithRequiredFromAnotherFile *StructWithRequiredField `json:"pointerStructWithRequiredFromAnotherFile,omitempty"`
198+
191199
// bool is a boolean field.
192200
// +optional
193201
Bool *bool `json:"bool,omitempty"` // want "field Bool is an optional boolean and should be a pointer"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package a
2+
3+
type StructWithRequiredField struct {
4+
// tsring is a string field.
5+
// +required
6+
String string `json:"string"`
7+
}

0 commit comments

Comments
 (0)