Skip to content

Commit 7d36636

Browse files
committed
Check alias types in optionalfields
1 parent 10de586 commit 7d36636

File tree

5 files changed

+197
-4
lines changed

5 files changed

+197
-4
lines changed

pkg/analysis/optionalfields/analyzer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,18 @@ func (a *analyzer) checkFieldPointersPreferenceWhenRequired(pass *analysis.Pass,
239239
a.checkFieldPointersPreferenceWhenRequiredBool(pass, field, fieldName, isStarExpr, jsonTags)
240240
case "float32", "float64":
241241
a.checkFieldPointersPreferenceWhenRequiredFloat(pass, field, fieldName, isStarExpr, markersAccess, jsonTags)
242-
default:
243-
panic(fmt.Sprintf("unknown type: %s", ident.Name))
244242
}
245243
}
246244

247245
func (a *analyzer) checkFieldPointersPreferenceWhenRequiredIdentObj(pass *analysis.Pass, field *ast.Field, fieldName string, isStarExpr bool, decl *ast.TypeSpec, markersAccess markers.Markers, jsonTags extractjsontags.FieldTagInfo) {
248246
switch t := decl.Type.(type) {
249247
case *ast.StructType:
250248
a.checkFieldPointersPreferenceWhenRequiredStructType(pass, field, fieldName, isStarExpr, t, markersAccess, jsonTags)
251-
default:
252-
panic(fmt.Sprintf("unknown type: %T", decl.Type))
249+
case *ast.Ident:
250+
// The field is using a type alias.
251+
a.checkFieldPointersPreferenceWhenRequired(pass, field, fieldName, isStarExpr, t, markersAccess, jsonTags)
252+
case *ast.ArrayType, *ast.MapType:
253+
a.checkFieldPointersPointerTypes(pass, field, fieldName, isStarExpr, markersAccess, jsonTags)
253254
}
254255
}
255256

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ type A struct {
6969
// PointerPointerString is a double pointer string field.
7070
// +optional
7171
DoublePointerString **string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is a pointer type and should not be a pointer"
72+
73+
// PointerStringAlias is a pointer string alias field.
74+
// +optional
75+
PointerStringAlias *StringAlias `json:"pointerStringAlias,omitempty"`
76+
77+
// PointerIntAlias is a pointer int alias field.
78+
// +optional
79+
PointerIntAlias *IntAlias `json:"pointerIntAlias,omitempty"`
80+
81+
// PointerFloatAlias is a pointer float alias field.
82+
// +optional
83+
PointerFloatAlias *FloatAlias `json:"pointerFloatAlias,omitempty"`
84+
85+
// PointerBoolAlias is a pointer bool alias field.
86+
// +optional
87+
PointerBoolAlias *BoolAlias `json:"pointerBoolAlias,omitempty"`
88+
89+
// PointerSliceAlias is a pointer slice alias field.
90+
// +optional
91+
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"`
92+
93+
// PointerMapAlias is a pointer map alias field.
94+
// +optional
95+
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"`
7296
}
7397

7498
type B struct {
@@ -88,3 +112,15 @@ type D struct {
88112
// +optional
89113
StringWithMinLength1 *string `json:"stringWithMinLength1,omitempty"`
90114
}
115+
116+
type StringAlias string
117+
118+
type IntAlias int
119+
120+
type FloatAlias float64
121+
122+
type BoolAlias bool
123+
124+
type SliceAlias []string
125+
126+
type MapAlias map[string]string

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ type A struct {
6969
// PointerPointerString is a double pointer string field.
7070
// +optional
7171
DoublePointerString *string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is a pointer type and should not be a pointer"
72+
73+
// PointerStringAlias is a pointer string alias field.
74+
// +optional
75+
PointerStringAlias *StringAlias `json:"pointerStringAlias,omitempty"`
76+
77+
// PointerIntAlias is a pointer int alias field.
78+
// +optional
79+
PointerIntAlias *IntAlias `json:"pointerIntAlias,omitempty"`
80+
81+
// PointerFloatAlias is a pointer float alias field.
82+
// +optional
83+
PointerFloatAlias *FloatAlias `json:"pointerFloatAlias,omitempty"`
84+
85+
// PointerBoolAlias is a pointer bool alias field.
86+
// +optional
87+
PointerBoolAlias *BoolAlias `json:"pointerBoolAlias,omitempty"`
88+
89+
// PointerSliceAlias is a pointer slice alias field.
90+
// +optional
91+
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"`
92+
93+
// PointerMapAlias is a pointer map alias field.
94+
// +optional
95+
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"`
7296
}
7397

7498
type B struct {
@@ -88,3 +112,15 @@ type D struct {
88112
// +optional
89113
StringWithMinLength1 *string `json:"stringWithMinLength1,omitempty"`
90114
}
115+
116+
type StringAlias string
117+
118+
type IntAlias int
119+
120+
type FloatAlias float64
121+
122+
type BoolAlias bool
123+
124+
type SliceAlias []string
125+
126+
type MapAlias map[string]string

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,54 @@ type A struct {
214214
// PointerPointerString is a double pointer string field.
215215
// +optional
216216
DoublePointerString **string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is a pointer type and should not be a pointer"
217+
218+
// PointerStringAlias is a pointer string alias field.
219+
// +optional
220+
PointerStringAlias *StringAlias `json:"pointerStringAlias,omitempty"` // want "field PointerStringAlias is an optional string and does not have a minimum length. Where the difference between omitted and the empty string is significant, set the minmum length to 0"
221+
222+
// PointerIntAlias is a pointer int alias field.
223+
// +optional
224+
PointerIntAlias *IntAlias `json:"pointerIntAlias,omitempty"` // want "field PointerIntAlias is an optional integer and does not have a minimum/maximum value. Where the difference between omitted and 0 is significant, set the minimum/maximum value to a range including 0"
225+
226+
// PointerFloatAlias is a pointer float alias field.
227+
// +optional
228+
PointerFloatAlias *FloatAlias `json:"pointerFloatAlias,omitempty"` // want "field PointerFloatAlias is an optional float and does not have a minimum/maximum value. Where the difference between omitted and 0 is significant, set the minimum/maximum value to a range including 0"
229+
230+
// PointerBoolAlias is a pointer bool alias field.
231+
// +optional
232+
PointerBoolAlias *BoolAlias `json:"pointerBoolAlias,omitempty"`
233+
234+
// PointerSliceAlias is a pointer slice alias field.
235+
// +optional
236+
PointerSliceAlias *SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field PointerSliceAlias is a pointer type and should not be a pointer"
237+
238+
// PointerMapAlias is a pointer map alias field.
239+
// +optional
240+
PointerMapAlias *MapAlias `json:"pointerMapAlias,omitempty"` // want "field PointerMapAlias is a pointer type and should not be a pointer"
241+
242+
// StringAlias is a string alias field.
243+
// +optional
244+
StringAlias StringAlias `json:"stringAlias,omitempty"` // want "field StringAlias is an optional string and does not have a minimum length. Either set a minimum length or make StringAlias a pointer where the difference between omitted and the empty string is significant"
245+
246+
// IntAlias is an int alias field.
247+
// +optional
248+
IntAlias IntAlias `json:"intAlias,omitempty"` // want "field IntAlias is an optional integer and does not have a minimum/maximum value. Either set a minimum/maximum value or make IntAlias a pointer where the difference between omitted and 0 is significant"
249+
250+
// FloatAlias is a float alias field.
251+
// +optional
252+
FloatAlias FloatAlias `json:"floatAlias,omitempty"` // want "field FloatAlias is an optional float and does not have a minimum/maximum value. Either set a minimum/maximum value or make FloatAlias a pointer where the difference between omitted and 0 is significant"
253+
254+
// BoolAlias is a bool alias field.
255+
// +optional
256+
BoolAlias BoolAlias `json:"boolAlias,omitempty"` // want "field BoolAlias is an optional boolean and should be a pointer"
257+
258+
// SliceAlias is a slice alias field.
259+
// +optional
260+
SliceAlias SliceAlias `json:"sliceAlias,omitempty"`
261+
262+
// MapAlias is a map alias field.
263+
// +optional
264+
MapAlias MapAlias `json:"mapAlias,omitempty"`
217265
}
218266

219267
type B struct {
@@ -240,3 +288,15 @@ type D struct {
240288
// +optional
241289
StringWithMinLength1 string `json:"stringWithMinLength1,omitempty"`
242290
}
291+
292+
type StringAlias string
293+
294+
type IntAlias int
295+
296+
type FloatAlias float64
297+
298+
type BoolAlias bool
299+
300+
type SliceAlias []string
301+
302+
type MapAlias map[string]string

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,54 @@ type A struct {
215215
// PointerPointerString is a double pointer string field.
216216
// +optional
217217
DoublePointerString *string `json:"doublePointerString,omitempty"` // want "field DoublePointerString is a pointer type and should not be a pointer"
218+
219+
// PointerStringAlias is a pointer string alias field.
220+
// +optional
221+
PointerStringAlias *StringAlias `json:"pointerStringAlias,omitempty"` // want "field PointerStringAlias is an optional string and does not have a minimum length. Where the difference between omitted and the empty string is significant, set the minmum length to 0"
222+
223+
// PointerIntAlias is a pointer int alias field.
224+
// +optional
225+
PointerIntAlias *IntAlias `json:"pointerIntAlias,omitempty"` // want "field PointerIntAlias is an optional integer and does not have a minimum/maximum value. Where the difference between omitted and 0 is significant, set the minimum/maximum value to a range including 0"
226+
227+
// PointerFloatAlias is a pointer float alias field.
228+
// +optional
229+
PointerFloatAlias *FloatAlias `json:"pointerFloatAlias,omitempty"` // want "field PointerFloatAlias is an optional float and does not have a minimum/maximum value. Where the difference between omitted and 0 is significant, set the minimum/maximum value to a range including 0"
230+
231+
// PointerBoolAlias is a pointer bool alias field.
232+
// +optional
233+
PointerBoolAlias *BoolAlias `json:"pointerBoolAlias,omitempty"`
234+
235+
// PointerSliceAlias is a pointer slice alias field.
236+
// +optional
237+
PointerSliceAlias SliceAlias `json:"pointerSliceAlias,omitempty"` // want "field PointerSliceAlias is a pointer type and should not be a pointer"
238+
239+
// PointerMapAlias is a pointer map alias field.
240+
// +optional
241+
PointerMapAlias MapAlias `json:"pointerMapAlias,omitempty"` // want "field PointerMapAlias is a pointer type and should not be a pointer"
242+
243+
// StringAlias is a string alias field.
244+
// +optional
245+
StringAlias StringAlias `json:"stringAlias,omitempty"` // want "field StringAlias is an optional string and does not have a minimum length. Either set a minimum length or make StringAlias a pointer where the difference between omitted and the empty string is significant"
246+
247+
// IntAlias is an int alias field.
248+
// +optional
249+
IntAlias IntAlias `json:"intAlias,omitempty"` // want "field IntAlias is an optional integer and does not have a minimum/maximum value. Either set a minimum/maximum value or make IntAlias a pointer where the difference between omitted and 0 is significant"
250+
251+
// FloatAlias is a float alias field.
252+
// +optional
253+
FloatAlias FloatAlias `json:"floatAlias,omitempty"` // want "field FloatAlias is an optional float and does not have a minimum/maximum value. Either set a minimum/maximum value or make FloatAlias a pointer where the difference between omitted and 0 is significant"
254+
255+
// BoolAlias is a bool alias field.
256+
// +optional
257+
BoolAlias *BoolAlias `json:"boolAlias,omitempty"` // want "field BoolAlias is an optional boolean and should be a pointer"
258+
259+
// SliceAlias is a slice alias field.
260+
// +optional
261+
SliceAlias SliceAlias `json:"sliceAlias,omitempty"`
262+
263+
// MapAlias is a map alias field.
264+
// +optional
265+
MapAlias MapAlias `json:"mapAlias,omitempty"`
218266
}
219267

220268
type B struct {
@@ -241,3 +289,15 @@ type D struct {
241289
// +optional
242290
StringWithMinLength1 string `json:"stringWithMinLength1,omitempty"`
243291
}
292+
293+
type StringAlias string
294+
295+
type IntAlias int
296+
297+
type FloatAlias float64
298+
299+
type BoolAlias bool
300+
301+
type SliceAlias []string
302+
303+
type MapAlias map[string]string

0 commit comments

Comments
 (0)