Skip to content

Commit 81d935c

Browse files
authored
Merge pull request #142 from nayuta-ai/fix-requiredfields-doc
fix documentation about requiredfields
2 parents ef33eac + 4fea898 commit 81d935c

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

docs/linters.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,43 @@ It will also remove the secondary marker where both the preferred and secondary
427427

428428
## RequiredFields
429429

430-
The `requiredfields` linter checks that fields that are marked as required, follow the convention of not being pointers,
431-
and not having an `omitempty` value in their `json` tag.
430+
The `requiredfields` linter checks that all fields marked as required adhere to having `omitempty` or `omitzero` values in their `json` tags.
431+
Currently `omitzero` is handled only for fields with struct type.
432+
433+
Required fields should have omitempty tags to prevent "mess" in the encoded object.
434+
Fields are not typically pointers.
435+
A field doesn't need to be a pointer if its zero value is not a valid value, as this zero value could never be accepted.
436+
However, if the zero value is valid, the field should be a pointer to differentiate between an unset state and a valid zero value.
437+
438+
In certain use cases, it can be desirable to not omit required fields from the serialized form of the object.
439+
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.
432440

433441
### Configuration
434442

435443
```yaml
436444
lintersConfig:
437445
requiredfields:
438-
pointerPolicy: Warn | SuggestFix # The policy for pointers in required fields. Defaults to `SuggestFix`.
446+
pointers:
447+
policy: SuggestFix | Warn # The policy for pointers in required fields. Defaults to `SuggestFix`.
448+
omitempty:
449+
policy: SuggestFix | Warn | Ignore # The policy for omitempty in required fields. Defaults to `SuggestFix`.
450+
omitzero:
451+
policy: SuggestFix | Warn | Forbid # The policy for omitzero in required fields. Defaults to `SuggestFix`.
439452
```
440453
441454
### Fixes
442455
443-
The `requiredfields` linter can automatically fix fields that are marked as required, but are pointers.
456+
The `requiredfields` linter can automatically fix fields marked as required. It does this by checking if the field should be a pointer and if its `json` tag is set correctly with `omitempty` or `omitzero`.
457+
458+
If a field's zero value is valid, the linter will suggest to fix it to be a pointer type and add `omitempty` to its JSON tag.
459+
460+
If a field's zero value is not valid, the field doesn't need to be a pointer. However, to prevent unnecessary data from being encoded in the JSON, the linter will suggest to add `omitempty` to its JSON tag.
461+
For a struct field with an invalid zero value, the linter will suggest to add `omitzero` to the JSON tag.
444462

445-
It will suggest to remove the pointer from the field, and update the `json` tag to remove the `omitempty` value.
463+
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointers.policy` to `Warn`.
446464

447-
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointerPolicy` to `Warn`.
448-
The linter will then only suggest to remove the `omitempty` value from the `json` tag.
465+
If you prefer not to suggest fixes for `omitempty` in required fields, you can change the `omitempty.policy` to `Warn` or `Ignore`.
466+
If you prefer not to suggest fixes for `omitzero` in required fields, you can change the `omitzero.policy` to `Warn` and also not to consider `omitzero` policy at all, it can be set to `Forbid`.
449467

450468
## SSATags
451469

pkg/analysis/requiredfields/doc.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ limitations under the License.
1515
*/
1616

1717
/*
18-
requiredFields is a linter to check that fields that are marked as required are not pointers, and do not have the omitempty tag.
18+
requiredfields is a linter to check that fields that are marked as required are marshalled properly.
1919
The linter will check for fields that are marked as required using the +required marker, or the +kubebuilder:validation:Required marker.
2020
21-
The linter will suggest to remove the omitempty tag from fields that are marked as required, but have the omitempty tag.
22-
The linter will suggest to remove the pointer type from fields that are marked as required.
21+
Required fields should have omitempty or omitzero tags to prevent "mess" in the encoded object.
22+
omitzero is handled only for fields with struct type.
2323
24-
If you have a large, existing codebase, you may not want to automatically fix all of the pointer issues.
25-
In this case, you can configure the linter not to suggest fixing the pointer issues by setting the `pointerPolicy` option to `Warn`.
24+
Fields are not typically pointers.
25+
A field doesn't need to be a pointer if its zero value is not a valid value, as this zero value could never be accepted.
26+
However, if the zero value is valid, the field should be a pointer to differentiate between an unset state and a valid zero value.
2627
*/
2728
package requiredfields

0 commit comments

Comments
 (0)