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
Copy file name to clipboardExpand all lines: docs/linters.md
+11-4Lines changed: 11 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -180,10 +180,13 @@ The `nophase` linter checks that the fields in the API types don't contain a 'Ph
180
180
181
181
## OptionalFields
182
182
183
-
The `optionalfields` linter checks that all fields marked as optional adhere to being pointers and having the `omitempty` value in their `json` tag where appropriate.
183
+
The `optionalfields` linter checks that all fields marked as optional adhere to being pointers and having either the `omitempty` or `omitzero` value in their `json` tag where appropriate.
184
+
Currently `omitzero` is handled only for fields with struct type.
184
185
185
186
If you prefer to avoid pointers where possible, the linter can be configured with the `WhenRequired` preference to determine, based on the serialization and valid values for the field, whether the field should be a pointer or not.
186
-
For example, an optional string with a non-zero minimum length does not need to be a pointer, as the zero value is not valid, and it is safe for the Go marshaller to omit the empty value.
187
+
For example
188
+
- an optional string with a non-zero minimum length does not need to be a pointer, as the zero value is not valid, and it is safe for the Go marshaller to omit the empty value.
189
+
- an optional struct having omitzero json tag with a non-zero minimum properties does not need to be a pointer, as the zero value is not valid, and it is safe for the Go marshaller to omit the empty value.
187
190
188
191
In certain use cases, it can be desirable to not omit optional fields from the serialized form of the object.
189
192
In this case, the `omitempty` policy can be set to `Ignore`, and the linter will ensure that the zero value of the object is an acceptable value for the field.
@@ -198,19 +201,23 @@ lintersConfig:
198
201
policy: SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
199
202
omitempty:
200
203
policy: SuggestFix | Warn | Ignore # The policy for omitempty in optional fields. Defaults to `SuggestFix`.
204
+
omitzero:
205
+
policy: SuggestFix | Warn | Forbid # The policy for omitzero in optional fields. Defaults to `SuggestFix`.
201
206
```
202
207
203
208
### Fixes
204
209
205
-
The `optionalfields` linter can automatically fix fields that are marked as optional, that are either not pointers or do not have the `omitempty` value in their `json` tag.
206
-
It will suggest to add the pointer to the field, and update the `json` tag to include the `omitempty` value.
210
+
The `optionalfields` linter can automatically fix fields that are marked as optional, that are either not pointers or do not have the `omitempty` or `omitzero` value in their `json` tag.
211
+
It will suggest to add the pointer to the field, and update the `json` tag to include the `omitempty` value or, for struct fields specifically, it will suggest to remove the pointer to the field, and update the `json` tag to include the `omitzero` value.
207
212
208
213
If you prefer not to suggest fixes for pointers in optional fields, you can change the `pointers.policy` to `Warn`.
209
214
210
215
If you prefer not to suggest fixes for `omitempty` in optional fields, you can change the `omitempty.policy` to `Warn` or `Ignore`.
216
+
If you prefer not to suggest fixes for `omitzero` in optional fields, you can change the `omitzero.policy` to `Warn` and also not to consider `omitzero` policy at all, it can be set to `Forbid`.
211
217
212
218
When the `pointers.preference` is set to `WhenRequired`, the linter will suggest to add the pointer to the field only when the field zero value is a valid value for the field.
213
219
When the field zero value is not a valid value for the field, the linter will suggest to remove the pointer from the field.
220
+
When the field zero value is not a valid value for the field of type struct, the linter will suggest to add `omitzero` json tag and to remove the pointer from the field.
214
221
215
222
When the `pointers.preference` is set to `Always`, the linter will always suggest to add the pointer to the field, regardless of the validity of the zero value of the field.
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.")
// 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)
// structWithValidOmitZero is a struct field with a minimum number of properties on the struct so not a valid zero value.
293
+
// +optional
294
+
StructWithValidOmitZero*D`json:"structWithValidOmitZero,omitempty,omitzero"`// want "field StructWithValidOmitZero has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed."
295
+
296
+
// structWithOnlyOmitZero is a struct field with a minimum number of properties on the struct so not a valid zero value.
297
+
// +optional
298
+
StructWithOnlyOmitZero*D`json:"structWithOnlyOmitZero,omitzero"`// want "field StructWithOnlyOmitZero has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed." "field StructWithOnlyOmitZero is optional and should have the omitempty tag"
299
+
300
+
// structWithValidOmitZeroWithoutPointer is a struct field with a minimum number of properties on the struct so not a valid zero value.
301
+
// +optional
302
+
StructWithValidOmitZeroWithoutPointerD`json:"structWithValidOmitZeroWithoutPointer,omitempty,omitzero"`// want "field StructWithValidOmitZeroWithoutPointer has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed." "field StructWithValidOmitZeroWithoutPointer is optional and should be a pointer"
303
+
304
+
// structWithOnlyOmitZeroWithoutPointer is a struct field with a minimum number of properties on the struct so not a valid zero value.
305
+
// +optional
306
+
StructWithOnlyOmitZeroWithoutPointerD`json:"structWithOnlyOmitZeroWithoutPointer,omitzero"`// want "field StructWithOnlyOmitZeroWithoutPointer has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed." "field StructWithOnlyOmitZeroWithoutPointer is optional and should have the omitempty tag" "field StructWithOnlyOmitZeroWithoutPointer is optional and should be a pointer"
// structWithValidOmitZero is a struct field with a minimum number of properties on the struct so not a valid zero value.
293
+
// +optional
294
+
StructWithValidOmitZero *D `json:"structWithValidOmitZero,omitempty"` // want "field StructWithValidOmitZero has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed."
295
+
296
+
// structWithOnlyOmitZero is a struct field with a minimum number of properties on the struct so not a valid zero value.
297
+
// +optional
298
+
StructWithOnlyOmitZero *D `json:"structWithOnlyOmitZero,omitempty"` // want "field StructWithOnlyOmitZero has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed." "field StructWithOnlyOmitZero is optional and should have the omitempty tag"
299
+
300
+
// structWithValidOmitZeroWithoutPointer is a struct field with a minimum number of properties on the struct so not a valid zero value.
301
+
// +optional
302
+
StructWithValidOmitZeroWithoutPointer *D `json:"structWithValidOmitZeroWithoutPointer,omitempty"` // want "field StructWithValidOmitZeroWithoutPointer has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed." "field StructWithValidOmitZeroWithoutPointer is optional and should be a pointer"
303
+
304
+
// structWithOnlyOmitZeroWithoutPointer is a struct field with a minimum number of properties on the struct so not a valid zero value.
305
+
// +optional
306
+
StructWithOnlyOmitZeroWithoutPointer *D `json:"structWithOnlyOmitZeroWithoutPointer,omitempty"` // want "field StructWithOnlyOmitZeroWithoutPointer has the omitzero tag, but by policy is not allowed. The omitzero tag should be removed." "field StructWithOnlyOmitZeroWithoutPointer is optional and should have the omitempty tag" "field StructWithOnlyOmitZeroWithoutPointer is optional and should be a pointer"
0 commit comments