Skip to content

Commit c2c6cc1

Browse files
committed
Add documentation for optionalfields linter
1 parent 7f2244e commit c2c6cc1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,42 @@ lintersConfig:
279279

280280
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.
281281

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+
282318
## OptionalOrRequired
283319

284320
The `optionalorrequired` linter checks that all fields in the API types are either optional or required, and are marked explicitly as such.

pkg/analysis/optionalfields/doc.go

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

1717
/*
18-
*/
18+
optionalfields is a linter to check that fields that are marked as optional are marshalled properly depending on the configured policies.
19+
20+
By default, all optional fields should be pointers and have omitempty tags. The exception to this would be arrays and maps where the empty value can be omitted without the need for a pointer.
21+
22+
However, where the zero value for a field is not a valid value (e.g. the empty string, or 0), the field does not need to be a pointer as the zero value could never be admitted.
23+
In this case, the field may not need to be a pointer, and, with the WhenRequired preference, the linter will point out where the fields do not need to be pointers.
24+
25+
Structs are also inspected to determine if they require a pointer.
26+
If a struct has any required fields, or a minimum numebr of properties, then fields leveraging the struct should be pointers.
27+
28+
Optional structs do not always need to be pointers, but may be marshalled as `{}` because the JSON marshaller in Go cannot determine whether a struct is empty or not.
29+
*/
1930
package optionalfields

0 commit comments

Comments
 (0)