Skip to content

Commit f4d71c5

Browse files
committed
fix documents based on review comments
1 parent 13d00d0 commit f4d71c5

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

docs/linters.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,13 @@ The `optionalorrequired` linter can automatically fix fields that are using the
288288
It will also remove the secondary marker where both the preferred and secondary marker are present on a field.
289289

290290
## RequiredFields
291-
292-
The `requiredfields` linter checks that all fields marked as required adhere to having `omitempty` tags and being pointers where the zero value is an acceptable value.
291+
The `requiredfields` linter checks that all fields marked as required adhere to having `omitempty` or `omitzero` values in their `json` tags.
293292
Currently `omitzero` is handled only for fields with struct type.
294293

295-
If you prefer to avoid pointers where possible, the linter can be configured to determine, based on the validation constraints for the field, whether the field should be a pointer or not.
296-
For example:
297-
- a required 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.
298-
- a required struct having omitzero json tag with validation constraints that make the zero value invalid 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.
294+
Required fields should have omitempty tags to prevent "mess" in the encoded object.
295+
Fields are not typically pointers.
296+
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.
297+
However, if the zero value is valid, the field should be a pointer to differentiate between an unset state and a valid zero value.
299298

300299
In certain use cases, it can be desirable to not omit required fields from the serialized form of the object.
301300
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.
@@ -315,8 +314,12 @@ lintersConfig:
315314
316315
### Fixes
317316
318-
The `requiredfields` linter can automatically fix fields that are marked as required, that are either not pointers when they should be or do not have the `omitempty` or `omitzero` value in their `json` tag.
319-
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.
317+
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`.
318+
319+
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.
320+
321+
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.
322+
For a struct field with an invalid zero value, the linter will suggest to add `omitzero` to the JSON tag.
320323

321324
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointers.policy` to `Warn`.
322325

pkg/analysis/requiredfields/doc.go

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

1717
/*
18-
requiredfields is a linter to check that fields that are marked as required are marshalled properly depending on the configured policies.
18+
requiredfields is a linter to check that fields that are marked as required are marshalled properly.
19+
The linter will check for fields that are marked as required using the +required marker, or the +kubebuilder:validation:Required marker.
1920
20-
By default, all required fields should have omitempty tags and be pointers where the zero value is an acceptable value. The exception to this would be where the zero value for a field is not a valid value, in which case the field does not need to be a pointer.
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.
2123
22-
However, where the zero value for a field is a valid value (e.g. the empty string, or 0), the field should be a pointer to distinguish between unset and zero value states.
23-
24-
Required fields should always have omitempty tags to prevent "mess" in the encoded object, regardless of whether they are pointers.
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.
2527
*/
2628
package requiredfields

0 commit comments

Comments
 (0)