Skip to content

Commit 10de586

Browse files
committed
Add documentation for optionalfields linter
1 parent 896e8b6 commit 10de586

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@ lintersConfig:
261261

262262
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.
263263

264+
## OptionalFields
265+
266+
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.
267+
268+
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.
269+
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.
270+
271+
In certain use cases, it can be desirable to not omit optional fields from the serialized form of the object.
272+
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.
273+
274+
### Configuration
275+
276+
```yaml
277+
lintersConfig:
278+
optionalFields:
279+
pointers:
280+
preference: Always | WhenRequired # Whether to always require pointers, or only when required. Defaults to `Always`.
281+
policy: SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
282+
omitempty:
283+
policy: SuggestFix | Warn | Ignore # The policy for omitempty in optional fields. Defaults to `SuggestFix`.
284+
```
285+
264286
## OptionalOrRequired
265287
266288
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)