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
Allow pointer-to-slice/map with explicit MinItems=0/MinProperties=0
This change fixes the linter incorrectly suggesting removal of pointers
for slice and map fields that have explicit MinItems=0 or MinProperties=0
markers.
Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
s.handleFieldShouldBePointer(pass, field, fieldName, isPointer, underlying, fmt.Sprintf("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.", zeroValue, validationHint))
148
+
s.handleFieldShouldBePointer(pass, field, fieldName, isPointer, underlying, markersAccess, fmt.Sprintf("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.", zeroValue, validationHint))
149
149
casehasValidZeroValue, isStruct:
150
150
// The field validation infers that the zero value is valid, the field needs to be a pointer.
151
151
// Structs with omitempty should always be pointers, else they won't actually be omitted.
s.handleFieldShouldBePointer(pass, field, fieldName, isPointer, underlying, fmt.Sprintf("has a valid zero value (%s) and should be a pointer.", zeroValue))
154
+
s.handleFieldShouldBePointer(pass, field, fieldName, isPointer, underlying, markersAccess, fmt.Sprintf("has a valid zero value (%s) and should be a pointer.", zeroValue))
// The validation is fully complete, and the zero value is not valid, so we don't need a pointer.
157
-
s.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s does not allow the zero value. The field does not need to be a pointer.")
157
+
s.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, underlying, markersAccess, "field %s does not allow the zero value. The field does not need to be a pointer.")
158
158
}
159
159
160
160
// In this case, we should always add the omitempty if it isn't present.
// The field is not omitempty, and the zero value is valid, the field does not need to be a pointer.
168
-
s.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s does not have omitempty and allows the zero value. The field does not need to be a pointer.")
168
+
s.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, underlying, markersAccess, "field %s does not have omitempty and allows the zero value. The field does not need to be a pointer.")
169
169
case!hasValidZeroValue:
170
170
// The zero value would not be accepted, so the field needs to have omitempty.
171
171
// Force the omitempty policy to suggest a fix. We can only get to this function when the policy is configured to Ignore.
@@ -174,17 +174,17 @@ func (s *serializationCheck) checkFieldPropertiesWithoutOmitEmpty(pass *analysis
174
174
// Once it has the omitempty tag, it will also need to be a pointer in some cases.
175
175
// Now handle it as if it had the omitempty already.
176
176
// We already handle the omitempty tag above, so force the `hasOmitEmpty` to true.
s.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s does not allow the zero value. The field does not need to be a pointer.")
187
+
s.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, underlying, markersAccess, "field %s does not allow the zero value. The field does not need to be a pointer.")
reportShouldRemovePointer(pass, field, PointersPolicySuggestFix, fieldName, "field %s underlying type does not need to be a pointer. The pointer should be removed.", fieldName)
@@ -231,10 +237,33 @@ func (s *serializationCheck) handleFieldShouldBePointer(pass *analysis.Pass, fie
PtrArrayWithZeroMinItemsNoOmitEmpty*[]string`json:"ptrArrayWithZeroMinItemsNoOmitEmpty"`// want "field PtrArrayWithZeroMinItemsNoOmitEmpty should have the omitempty tag."
MapPtrWithZeroMinPropertiesNoOmitEmpty*map[string]string`json:"mapPtrWithZeroMinPropertiesNoOmitEmpty"`// want "field MapPtrWithZeroMinPropertiesNoOmitEmpty should have the omitempty tag."
17
+
}
18
+
19
+
// Test that pointers ARE still flagged when MinItems/MinProperties is NOT zero
PtrArrayWithNonZeroMinItems*[]string`json:"ptrArrayWithNonZeroMinItems,omitempty"`// want "field PtrArrayWithNonZeroMinItems underlying type does not need to be a pointer. The pointer should be removed."
23
+
24
+
// No MinItems validation
25
+
PtrArrayWithoutMinItems*[]string`json:"ptrArrayWithoutMinItems,omitempty"`// want "field PtrArrayWithoutMinItems underlying type does not need to be a pointer. The pointer should be removed."
26
+
}
27
+
28
+
typeTestPointerToMapWithNonZeroMinAlwaysstruct {
29
+
// +kubebuilder:validation:MinProperties=1
30
+
MapPtrWithNonZeroMinProperties*map[string]string`json:"mapPtrWithNonZeroMinProperties,omitempty"`// want "field MapPtrWithNonZeroMinProperties underlying type does not need to be a pointer. The pointer should be removed."
31
+
32
+
// No MinProperties validation
33
+
MapPtrWithoutMinProperties*map[string]string`json:"mapPtrWithoutMinProperties,omitempty"`// want "field MapPtrWithoutMinProperties underlying type does not need to be a pointer. The pointer should be removed."
PtrArrayWithZeroMinItemsNoOmitEmpty *[]string `json:"ptrArrayWithZeroMinItemsNoOmitEmpty,omitempty"` // want "field PtrArrayWithZeroMinItemsNoOmitEmpty should have the omitempty tag."
MapPtrWithZeroMinPropertiesNoOmitEmpty *map[string]string `json:"mapPtrWithZeroMinPropertiesNoOmitEmpty,omitempty"` // want "field MapPtrWithZeroMinPropertiesNoOmitEmpty should have the omitempty tag."
17
+
}
18
+
19
+
// Test that pointers ARE still flagged when MinItems/MinProperties is NOT zero
20
+
type TestPointerToSliceWithNonZeroMinAlways struct {
21
+
// +kubebuilder:validation:MinItems=1
22
+
PtrArrayWithNonZeroMinItems []string `json:"ptrArrayWithNonZeroMinItems,omitempty"` // want "field PtrArrayWithNonZeroMinItems underlying type does not need to be a pointer. The pointer should be removed."
23
+
24
+
// No MinItems validation
25
+
PtrArrayWithoutMinItems []string `json:"ptrArrayWithoutMinItems,omitempty"` // want "field PtrArrayWithoutMinItems underlying type does not need to be a pointer. The pointer should be removed."
26
+
}
27
+
28
+
type TestPointerToMapWithNonZeroMinAlways struct {
29
+
// +kubebuilder:validation:MinProperties=1
30
+
MapPtrWithNonZeroMinProperties map[string]string `json:"mapPtrWithNonZeroMinProperties,omitempty"` // want "field MapPtrWithNonZeroMinProperties underlying type does not need to be a pointer. The pointer should be removed."
31
+
32
+
// No MinProperties validation
33
+
MapPtrWithoutMinProperties map[string]string `json:"mapPtrWithoutMinProperties,omitempty"` // want "field MapPtrWithoutMinProperties underlying type does not need to be a pointer. The pointer should be removed."
PtrArrayWithZeroMinItemsNoOmitEmpty*[]string`json:"ptrArrayWithZeroMinItemsNoOmitEmpty"`// want "field PtrArrayWithZeroMinItemsNoOmitEmpty should have the omitempty tag."
MapPtrWithZeroMinPropertiesNoOmitEmpty*map[string]string`json:"mapPtrWithZeroMinPropertiesNoOmitEmpty"`// want "field MapPtrWithZeroMinPropertiesNoOmitEmpty should have the omitempty tag."
17
+
}
18
+
19
+
// Test that pointers ARE still flagged when MinItems/MinProperties is NOT zero
20
+
typeTestPointerToSliceWithNonZeroMinstruct {
21
+
// +kubebuilder:validation:MinItems=1
22
+
PtrArrayWithNonZeroMinItems*[]string`json:"ptrArrayWithNonZeroMinItems,omitempty"`// want "field PtrArrayWithNonZeroMinItems does not allow the zero value. The field does not need to be a pointer."
23
+
24
+
// No MinItems validation
25
+
PtrArrayWithoutMinItems*[]string`json:"ptrArrayWithoutMinItems,omitempty"`// want "field PtrArrayWithoutMinItems underlying type does not need to be a pointer. The pointer should be removed."
26
+
}
27
+
28
+
typeTestPointerToMapWithNonZeroMinstruct {
29
+
// +kubebuilder:validation:MinProperties=1
30
+
MapPtrWithNonZeroMinProperties*map[string]string`json:"mapPtrWithNonZeroMinProperties,omitempty"`// want "field MapPtrWithNonZeroMinProperties does not allow the zero value. The field does not need to be a pointer."
31
+
32
+
// No MinProperties validation
33
+
MapPtrWithoutMinProperties*map[string]string`json:"mapPtrWithoutMinProperties,omitempty"`// want "field MapPtrWithoutMinProperties underlying type does not need to be a pointer. The pointer should be removed."
0 commit comments