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: README.md
+36Lines changed: 36 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -279,6 +279,42 @@ lintersConfig:
279
279
280
280
The `nophase` linter checks that the fields in the API types don't contain a 'Phase', or any field which contains 'Phase' as a substring, e.g MachinePhase.
281
281
282
+
## OptionalFields
283
+
284
+
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.
285
+
286
+
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.
287
+
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.
288
+
289
+
In certain use cases, it can be desirable to not omit optional fields from the serialized form of the object.
290
+
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.
291
+
292
+
### Configuration
293
+
294
+
```yaml
295
+
lintersConfig:
296
+
optionalFields:
297
+
pointers:
298
+
preference: Always | WhenRequired # Whether to always require pointers, or only when required. Defaults to `Always`.
299
+
policy: SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
300
+
omitempty:
301
+
policy: SuggestFix | Warn | Ignore # The policy for omitempty in optional fields. Defaults to `SuggestFix`.
302
+
```
303
+
304
+
### Fixes
305
+
306
+
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.
307
+
It will suggest to add the pointer to the field, and update the `json` tag to include the `omitempty` value.
308
+
309
+
If you prefer not to suggest fixes for pointers in optional fields, you can change the `pointers.policy` to `Warn`.
310
+
311
+
If you prefer not to suggest fixes for `omitempty` in optional fields, you can change the `omitempty.policy` to `Warn` or `Ignore`.
312
+
313
+
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.
314
+
When the field zero value is not a valid value for the field, the linter will suggest to remove the pointer from the field.
315
+
316
+
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.
317
+
282
318
## OptionalOrRequired
283
319
284
320
The `optionalorrequired` linter checks that all fields in the API types are either optional or required, and are marked explicitly as such.
0 commit comments