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
// The validation is fully complete, and the zero value is not valid, so we don't need a pointer.
189
-
a.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s is optional and does not allow the zero value. The field does not need to be a pointer.")
// The field is not omitempty, and the zero value is valid, the field does not need to be a pointer.
197
-
a.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s is optional, without omitempty and allows the zero value. The field does not need to be a pointer.")
198
-
case!hasValidZeroValue:
199
-
// The zero value would not be accepted, so the field needs to have omitempty.
200
-
// Force the omitempty policy to suggest a fix. We can only get to this function when the policy is configured to Ignore.
201
-
// Since we absolutely have to add the omitempty tag, we can report it as a suggestion.
202
-
reportShouldAddOmitEmpty(pass, field, OptionalFieldsOmitEmptyPolicySuggestFix, fieldName, "field %s is optional and does not allow the zero value. It must have the omitempty tag.", jsonTags)
203
-
// Once it has the omitempty tag, it will also need to be a pointer in some cases.
204
-
// Now handle it as if it had the omitempty already.
205
-
// We already handle the omitempty tag above, so force the `hasOmitEmpty` to true.
a.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s is optional and does not have a valid zero value. The field does not need to be a pointer.")
reportShouldRemovePointer(pass, field, OptionalFieldsPointerPolicySuggestFix, fieldName, "field %s is optional but the underlying type does not need to be a pointer. The pointer should be removed.")
234
-
caseOptionalFieldsPointerPolicyWarn:
235
-
pass.Reportf(field.Pos(), "field %s is optional but the underlying type does not need to be a pointer. The pointer should be removed.", fieldName)
236
-
}
237
-
}
238
-
239
-
return
240
-
}
241
-
242
-
ifisPointer {
243
-
return
244
-
}
245
-
246
-
switcha.pointerPolicy {
247
-
caseOptionalFieldsPointerPolicySuggestFix:
248
-
reportShouldAddPointer(pass, field, OptionalFieldsPointerPolicySuggestFix, fieldName, "field %s is optional and should be a pointer")
249
-
caseOptionalFieldsPointerPolicyWarn:
250
-
pass.Reportf(field.Pos(), "field %s is optional and should be a pointer", fieldName)
// Currently, add omitzero tags to only struct fields.
275
-
reportShouldAddOmitZero(pass, field, a.omitZeroPolicy, fieldName, "field %s is optional and does not allow the zero value. It must have the omitzero tag.", jsonTags)
pass.Reportf(field.Pos(), "field %s is optional and has a valid zero value (%s), but the validation is not complete (e.g. %s). The field should be a pointer to allow the zero value to be set. If the zero value is not a valid use case, complete the validation and remove the pointer.", fieldName, zeroValue, validationHint)
140
+
// isFieldOptional checks if a field has an optional marker.
Copy file name to clipboardExpand all lines: pkg/analysis/optionalfields/testdata/src/a/a.go
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -19,36 +19,36 @@ type A struct {
19
19
20
20
// string is a string field.
21
21
// +optional
22
-
Stringstring`json:"string,omitempty"`// want "field String is optional and should be a pointer"
22
+
Stringstring`json:"string,omitempty"`// want "field String should be a pointer."
23
23
24
24
// NonOmittedString is a string field without omitempty
25
25
// +optional
26
-
NonOmittedStringstring`json:"nonOmittedString"`// want "field NonOmittedString is optional and should be a pointer" "field NonOmittedString is optional and should have the omitempty tag"
26
+
NonOmittedStringstring`json:"nonOmittedString"`// want "field NonOmittedString should be a pointer." "field NonOmittedString should have the omitempty tag."
27
27
28
28
// int is an int field.
29
29
// +optional
30
-
Intint`json:"int,omitempty"`// want "field Int is optional and should be a pointer"
30
+
Intint`json:"int,omitempty"`// want "field Int should be a pointer."
31
31
32
32
// nonOmittedInt is an int field without omitempty
33
33
// +optional
34
-
NonOmittedIntint`json:"nonOmittedInt"`// want "field NonOmittedInt is optional and should be a pointer" "field NonOmittedInt is optional and should have the omitempty tag"
34
+
NonOmittedIntint`json:"nonOmittedInt"`// want "field NonOmittedInt should be a pointer." "field NonOmittedInt should have the omitempty tag."
35
35
36
36
// struct is a struct field.
37
37
// +optional
38
-
StructB`json:"struct,omitempty"`// want "field Struct is optional and should be a pointer"
38
+
StructB`json:"struct,omitempty"`// want "field Struct should be a pointer."
39
39
40
40
// nonOmittedStruct is a struct field without omitempty.
41
41
// +optional
42
-
NonOmittedStructB`json:"nonOmittedStruct"`// want "field NonOmittedStruct is optional and should be a pointer" "field NonOmittedStruct is optional and should have the omitempty tag"
42
+
NonOmittedStructB`json:"nonOmittedStruct"`// want "field NonOmittedStruct should be a pointer." "field NonOmittedStruct should have the omitempty tag."
43
43
44
44
// structWithMinProperties is a struct field with a minimum number of properties.
45
45
// +kubebuilder:validation:MinProperties=1
46
46
// +optional
47
-
StructWithMinPropertiesB`json:"structWithMinProperties,omitempty"`// want "field StructWithMinProperties is optional and should be a pointer"
47
+
StructWithMinPropertiesB`json:"structWithMinProperties,omitempty"`// want "field StructWithMinProperties should be a pointer."
48
48
49
49
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
50
50
// +optional
51
-
StructWithMinPropertiesOnStructD`json:"structWithMinPropertiesOnStruct,omitempty"`// want "field StructWithMinPropertiesOnStruct is optional and should be a pointer"
51
+
StructWithMinPropertiesOnStructD`json:"structWithMinPropertiesOnStruct,omitempty"`// want "field StructWithMinPropertiesOnStruct should be a pointer."
52
52
53
53
// slice is a slice field.
54
54
// +optional
@@ -60,15 +60,15 @@ type A struct {
60
60
61
61
// PointerSlice is a pointer slice field.
62
62
// +optional
63
-
PointerSlice*[]string`json:"pointerSlice,omitempty"`// want "field PointerSlice is optional but the underlying type does not need to be a pointer. The pointer should be removed."
63
+
PointerSlice*[]string`json:"pointerSlice,omitempty"`// want "field PointerSlice underlying type does not need to be a pointer. The pointer should be removed."
64
64
65
65
// PointerMap is a pointer map field.
66
66
// +optional
67
-
PointerMap*map[string]string`json:"pointerMap,omitempty"`// want "field PointerMap is optional but the underlying type does not need to be a pointer. The pointer should be removed."
67
+
PointerMap*map[string]string`json:"pointerMap,omitempty"`// want "field PointerMap underlying type does not need to be a pointer. The pointer should be removed."
68
68
69
69
// PointerPointerString is a double pointer string field.
70
70
// +optional
71
-
DoublePointerString**string`json:"doublePointerString,omitempty"`// want "field DoublePointerString is optional but the underlying type does not need to be a pointer. The pointer should be removed."
71
+
DoublePointerString**string`json:"doublePointerString,omitempty"`// want "field DoublePointerString underlying type does not need to be a pointer. The pointer should be removed."
72
72
73
73
// PointerStringAlias is a pointer string alias field.
74
74
// +optional
@@ -93,16 +93,16 @@ type A struct {
93
93
94
94
// PointerSliceAlias is a pointer slice alias field.
95
95
// +optional
96
-
PointerSliceAlias*SliceAlias`json:"pointerSliceAlias,omitempty"`// want "field PointerSliceAlias is optional but the underlying type does not need to be a pointer. The pointer should be removed."
96
+
PointerSliceAlias*SliceAlias`json:"pointerSliceAlias,omitempty"`// want "field PointerSliceAlias underlying type does not need to be a pointer. The pointer should be removed."
97
97
98
98
// PointerMapAlias is a pointer map alias field.
99
99
// +optional
100
-
PointerMapAlias*MapAlias`json:"pointerMapAlias,omitempty"`// want "field PointerMapAlias is optional but the underlying type does not need to be a pointer. The pointer should be removed."
100
+
PointerMapAlias*MapAlias`json:"pointerMapAlias,omitempty"`// want "field PointerMapAlias underlying type does not need to be a pointer. The pointer should be removed."
101
101
102
102
// StringAliasWithEnum is a string alias field with enum validation.
103
103
// With the "Always" pointer preference, optional fields should be pointers regardless of zero value validity.
104
104
// +optional
105
-
StringAliasWithEnumStringAliasWithEnum`json:"stringAliasWithEnum,omitempty"`// want "field StringAliasWithEnum is optional and should be a pointer"
105
+
StringAliasWithEnumStringAliasWithEnum`json:"stringAliasWithEnum,omitempty"`// want "field StringAliasWithEnum should be a pointer."
106
106
107
107
// StringAliasWithEnumPointer is a pointer string alias field with enum validation.
108
108
// This is correctly a pointer since the zero value is not valid.
@@ -111,7 +111,7 @@ type A struct {
111
111
112
112
// StringAliasWithEnumNoOmitEmpty is a string alias field with enum validation and no omitempty.
113
113
// +optional
114
-
StringAliasWithEnumNoOmitEmpty*StringAliasWithEnum`json:"stringAliasWithEnumNoOmitEmpty"`// want "field StringAliasWithEnumNoOmitEmpty is optional and should have the omitempty tag"
114
+
StringAliasWithEnumNoOmitEmpty*StringAliasWithEnum`json:"stringAliasWithEnumNoOmitEmpty"`// want "field StringAliasWithEnumNoOmitEmpty should have the omitempty tag."
0 commit comments