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 struct field need not be pointer if it does not have a valid zero value.
223
+
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 omitzero, and the zero value is valid, the field does not need to be a pointer.
236
+
a.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s is optional, without omitzero and allows the zero value. The field does not need to be a pointer.")
237
+
case!hasValidZeroValue&&isStruct:
238
+
// The zero value would not be accepted, so the struct field needs to have omitzero.
239
+
// Force the omitzero policy to suggest a fix. We can only get to this function when the omitzero policy is configured to Ignore.
240
+
// Since we absolutely have to add the omitzero tag, we can report it as a suggestion.
241
+
reportShouldAddOmitZero(pass, field, OptionalFieldsOmitZeroPolicySuggestFix, fieldName, "field %s is struct and optional and does not allow the zero value. It must have the omitzero tag.", jsonTags)
242
+
243
+
// Once it has the omitzero tag, it will also need to be a pointer in some cases.
244
+
// Now handle it as if it had the omitzero already.
245
+
// We already handle the omitzero tag above, so force the `hasOmitZero` to true.
StructWithOnlyOmitZeroTagB`json:"structWithOnlyOmitZeroTag,omitzero"`// want "field StructWithOnlyOmitZeroTag is optional and should have the omitempty tag"
183
183
184
184
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
Copy file name to clipboardExpand all lines: pkg/analysis/optionalfields/testdata/src/b/a.go.golden
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -179,7 +179,7 @@ type A struct {
179
179
// structWithOnlyOmitZeroTag is a struct field with a minimum number of properties and has only omitzero tag.
180
180
// +kubebuilder:validation:MinProperties=1
181
181
// +optional
182
-
StructWithOnlyOmitZeroTag B `json:"structWithOnlyOmitZeroTag,omitzero"`
182
+
StructWithOnlyOmitZeroTag B `json:"structWithOnlyOmitZeroTag,omitempty,omitzero"` // want "field StructWithOnlyOmitZeroTag is optional and should have the omitempty tag"
183
183
184
184
// structWithMinPropertiesOnStruct is a struct field with a minimum number of properties on the struct.
0 commit comments