You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reportShouldAddOmitEmpty(pass, field, fieldName, "field %s is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added.", jsonTags)
301
+
fallthrough// Check as if it did have omitempty, since it requires the omitempty.
reportShouldAddPointer(pass, field, a.pointerPolicy, fieldName, "field %s is an optional string enum allowing the empty value and should be a pointer")
reportShouldRemovePointer(pass, field, a.pointerPolicy, fieldName, "field %s is an optional string enum not allowing the empty value and should not be a pointer")
355
+
}
356
+
357
+
return
358
+
}
359
+
337
360
if!fieldMarkers.Has(minLengthMarker) {
338
361
ifisStarExpr {
339
362
pass.Reportf(field.Pos(), "field %s 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", fieldName)
@@ -365,6 +388,11 @@ func (a *analyzer) checkFieldPointersPreferenceWhenRequiredString(pass *analysis
reportShouldRemoveAllInstancesOfIntegerMarker(pass, field, markersAccess, minLengthMarker, fieldName, "field %s has a greater than zero minimum length without omitempty. The minimum length should be removed.")
pass.Reportf(field.Pos(), "field %s is an optional string enum not allowing the empty value without omitempty. Either allow the empty string or add omitempty.", fieldName)
394
+
}
395
+
368
396
// When non-omitempty, the string field should not be a pointer.
369
397
// The empty string should be a valid/acceptable value.
Copy file name to clipboardExpand all lines: pkg/analysis/optionalfields/testdata/src/c/a.go
+106-2Lines changed: 106 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,16 @@ type A struct {
129
129
// +optional
130
130
StringWithMinLength0string`json:"stringWithMinLength0,omitempty"`// want "field StringWithMinLength0 has a minimum length of 0. The empty string is a valid value and therefore the field should be a pointer"
131
131
132
+
// EnumString is a string field with an enum.
133
+
// +kubebuilder:validation:Enum=foo;bar;baz
134
+
// +optional
135
+
EnumStringstring`json:"enumString,omitempty"`
136
+
137
+
// EnumStringWithEmptyValue is a string field with an enum, also allowing the empty string.
138
+
// +kubebuilder:validation:Enum=foo;bar;baz;""
139
+
// +optional
140
+
EnumStringWithEmptyValuestring`json:"enumStringWithEmptyValue,omitempty"`// want "field EnumStringWithEmptyValue is an optional string enum allowing the empty value and should be a pointer"
141
+
132
142
// stringWithMinLength0WithoutOmitEmpty with minimum length is a string field without omitempty.
133
143
// +kubebuilder:validation:MinLength=0
134
144
// +optional
@@ -322,7 +332,19 @@ type A struct {
322
332
323
333
// structWithRequiredFieldsWithoutOmitEmpty is a struct field without omitempty.
// structWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty is a struct field with required fields but where the zero values are not valid.
338
+
// +optional
339
+
StructWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmptyC`json:"structWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty"`// want "field StructWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty is optional, but contains required field\\(s\\) and should be a pointer" "field StructWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added."
340
+
341
+
// structWithNonZeroByteArray is a struct field with a non-zero byte array.
342
+
// +optional
343
+
StructWithNonZeroByteArrayF`json:"structWithNonZeroByteArray"`// want "field StructWithNonZeroByteArray is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added."
344
+
345
+
// structWithInvalidEmptyEnum is a struct field with an invalid empty enum.
346
+
// +optional
347
+
StructWithInvalidEmptyEnumG`json:"structWithInvalidEmptyEnum"`// want "field StructWithInvalidEmptyEnum is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added"
326
348
327
349
// pointerStructWithOptionalFields is a pointer struct field.
328
350
// +optional
@@ -338,7 +360,7 @@ type A struct {
338
360
339
361
// pointerStructWithRequiredFieldsWithoutOmitEmpty is a pointer struct field without omitempty.
340
362
// +optional
341
-
PointerStructWithRequiredFieldsWithoutOmitEmpty*C`json:"pointerStructWithRequiredFieldsWithoutOmitEmpty"`// want "field PointerStructWithRequiredFieldsWithoutOmitEmpty is an optional struct without omitempty. It should not be a pointer"
363
+
PointerStructWithRequiredFieldsWithoutOmitEmpty*C`json:"pointerStructWithRequiredFieldsWithoutOmitEmpty"`// want "field PointerStructWithRequiredFieldsWithoutOmitEmpty is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added"
// D is a struct that's optional, but does not have any required fields.
507
+
DD`json:"d"`
508
+
509
+
// ByteArry is a byte array field.
510
+
// +optional
511
+
ByteArray []byte`json:"byteArray"`
512
+
}
513
+
514
+
typeFstruct {
515
+
// nonZeroByteArray is a byte array field that does not allow the zero value.
516
+
// +kubebuilder:validation:MinLength=1
517
+
// +optional
518
+
NonZeroByteArray []byte`json:"nonZeroByteArray"`
519
+
}
520
+
521
+
typeGstruct {
522
+
// enumWithoutOmitEmpty is an enum field without omitempty.
523
+
// It does need to allow the zero value, else the parent zero value is not valid.
524
+
// +optional
525
+
// +kubebuilder:validation:Enum=foo;bar
526
+
EnumWithoutOmitEmptystring`json:"enumWithoutOmitEmpty"`// want "field EnumWithoutOmitEmpty is an optional string enum not allowing the empty value without omitempty. Either allow the empty string or add omitempty."
Copy file name to clipboardExpand all lines: pkg/analysis/optionalfields/testdata/src/c/a.go.golden
+111-7Lines changed: 111 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -98,8 +98,8 @@ type A struct {
98
98
// pointerStruct is a pointer struct field.
99
99
// +optional
100
100
PointerStruct B `json:"pointerStruct,omitempty"` // want "field PointerStruct is optional, and contains no required field\\(s\\) and does not need to be a pointer"
101
-
102
-
// pointerStructWithoutOmitEmpty is a pointer struct field without omitempty.
101
+
102
+
// pointerStructWithoutOmitEmpty is a pointer struct field without omitempty.
103
103
// +optional
104
104
PointerStructWithoutOmitEmpty B `json:"pointerStructWithoutOmitEmpty"` // want "field PointerStructWithoutOmitEmpty is an optional struct without omitempty. It should not be a pointer"
105
105
@@ -125,6 +125,16 @@ type A struct {
125
125
// +optional
126
126
StringWithMinLength0 *string `json:"stringWithMinLength0,omitempty"` // want "field StringWithMinLength0 has a minimum length of 0. The empty string is a valid value and therefore the field should be a pointer"
127
127
128
+
// EnumString is a string field with an enum.
129
+
// +kubebuilder:validation:Enum=foo;bar;baz
130
+
// +optional
131
+
EnumString string `json:"enumString,omitempty"`
132
+
133
+
// EnumStringWithEmptyValue is a string field with an enum, also allowing the empty string.
134
+
// +kubebuilder:validation:Enum=foo;bar;baz;""
135
+
// +optional
136
+
EnumStringWithEmptyValue *string `json:"enumStringWithEmptyValue,omitempty"` // want "field EnumStringWithEmptyValue is an optional string enum allowing the empty value and should be a pointer"
137
+
128
138
// stringWithMinLength0WithoutOmitEmpty with minimum length is a string field without omitempty.
// floatWithInvalidMinimumValue with invalid minimum value is a float field.
284
+
285
+
// floatWithInvalidMinimumValue with invalid minimum value is a float field.
276
286
// +kubebuilder:validation:Minimum=foo
277
287
// +optional
278
288
FloatWithInvalidMinimumValue float64 `json:"floatWithInvalidMinimumValue,omitempty"` // want "field FloatWithInvalidMinimumValue has a minimum value of foo, but it is not a float"
@@ -311,9 +321,21 @@ type A struct {
311
321
// +optional
312
322
StructWithRequiredFields *C `json:"structWithRequiredFields,omitempty"` // want "field StructWithRequiredFields is optional, but contains required field\\(s\\) and should be a pointer"
313
323
314
-
// structWithRequiredFieldsWithoutOmitEmpty is a struct field without omitempty.
324
+
// structWithRequiredFieldsWithoutOmitEmpty is a struct field without omitempty.
325
+
// +optional
326
+
StructWithRequiredFieldsWithoutOmitEmpty E `json:"structWithRequiredFieldsWithoutOmitEmpty"`
327
+
328
+
// structWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty is a struct field with required fields but where the zero values are not valid.
315
329
// +optional
316
-
StructWithRequiredFieldsWithoutOmitEmpty C `json:"structWithRequiredFieldsWithoutOmitEmpty"`
330
+
StructWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty *C `json:"structWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty,omitempty"` // want "field StructWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty is optional, but contains required field\\(s\\) and should be a pointer" "field StructWithRequiredFieldsWithNonZeroAllowedValuesWithoutOmitEmpty is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added."
331
+
332
+
// structWithNonZeroByteArray is a struct field with a non-zero byte array.
333
+
// +optional
334
+
StructWithNonZeroByteArray F `json:"structWithNonZeroByteArray,omitempty"` // want "field StructWithNonZeroByteArray is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added."
335
+
336
+
// structWithInvalidEmptyEnum is a struct field with an invalid empty enum.
337
+
// +optional
338
+
StructWithInvalidEmptyEnum G `json:"structWithInvalidEmptyEnum,omitempty"` // want "field StructWithInvalidEmptyEnum is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added"
317
339
318
340
// pointerStructWithOptionalFields is a pointer struct field.
319
341
// +optional
@@ -329,7 +351,7 @@ type A struct {
329
351
330
352
// pointerStructWithRequiredFieldsWithoutOmitEmpty is a pointer struct field without omitempty.
331
353
// +optional
332
-
PointerStructWithRequiredFieldsWithoutOmitEmpty C `json:"pointerStructWithRequiredFieldsWithoutOmitEmpty"` // want "field PointerStructWithRequiredFieldsWithoutOmitEmpty is an optional struct without omitempty. It should not be a pointer"
354
+
PointerStructWithRequiredFieldsWithoutOmitEmpty *C `json:"pointerStructWithRequiredFieldsWithoutOmitEmpty,omitempty"` // want "field PointerStructWithRequiredFieldsWithoutOmitEmpty is an optional struct without omitempty, but the zero value is not valid. Omitempty must be added"
// D is a struct that's optional, but does not have any required fields.
496
+
D D `json:"d"`
497
+
498
+
// ByteArry is a byte array field.
499
+
// +optional
500
+
ByteArray []byte `json:"byteArray"`
501
+
}
502
+
503
+
type F struct {
504
+
// nonZeroByteArray is a byte array field that does not allow the zero value.
505
+
// +kubebuilder:validation:MinLength=1
506
+
// +optional
507
+
NonZeroByteArray []byte `json:"nonZeroByteArray"`
508
+
}
509
+
510
+
type G struct {
511
+
// enumWithoutOmitEmpty is an enum field without omitempty.
512
+
// It does need to allow the zero value, else the parent zero value is not valid.
513
+
// +optional
514
+
// +kubebuilder:validation:Enum=foo;bar
515
+
EnumWithoutOmitEmpty string `json:"enumWithoutOmitEmpty"` // want "field EnumWithoutOmitEmpty is an optional string enum not allowing the empty value without omitempty. Either allow the empty string or add omitempty."
0 commit comments