Skip to content

Commit 2819026

Browse files
authored
Merge pull request #69 from JoelSpeed/optionalfields
Introduce optionalfields linter
2 parents f86bf7b + 9375560 commit 2819026

File tree

24 files changed

+3215
-0
lines changed

24 files changed

+3215
-0
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.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/onsi/gomega v1.36.3
99
golang.org/x/tools v0.32.0
1010
k8s.io/apimachinery v0.32.3
11+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
1112
)
1213

1314
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5151
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5252
k8s.io/apimachinery v0.32.3 h1:JmDuDarhDmA/Li7j3aPrwhpNBA94Nvk5zLeOge9HH1U=
5353
k8s.io/apimachinery v0.32.3/go.mod h1:GpHVgxoKlTxClKcteaeuF1Ul/lDVb74KpZcxcmLDElE=
54+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro=
55+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=

pkg/analysis/helpers/markers/analyzer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ func (ms MarkerSet) HasWithExpressions(identifier string, expressions map[string
385385
return false
386386
}
387387

388+
// Get returns the markers associated with the given identifier.
389+
// If no markers are found, an empty slice is returned.
390+
// The returned slice may contain multiple markers with the same identifier.
391+
func (ms MarkerSet) Get(identifier string) []Marker {
392+
return ms[identifier]
393+
}
394+
388395
// UnsortedList returns a list of the markers, in no particular order.
389396
func (ms MarkerSet) UnsortedList() []Marker {
390397
markers := []Marker{}

0 commit comments

Comments
 (0)