-
Notifications
You must be signed in to change notification settings - Fork 585
Update unions guidance to confrom with latest serialization guidance #2060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -346,35 +346,37 @@ type MyPlatformConfig struct { | |||||
| // should choose a sensible default on their behalf. | ||||||
| // +unionDiscriminator | ||||||
| // +kubebuilder:validation:Enum:="AWS";"Azure";"GCP" | ||||||
| // +kubebuilder:validation:Required | ||||||
| PlatformType string `json:"platformType"` | ||||||
| // +required | ||||||
| PlatformType string `json:"platformType,omitempty"` | ||||||
|
|
||||||
| // aws is the AWS configuration. | ||||||
| // All structures within the union must be optional and pointers. | ||||||
| // All structures within the union must be optional and have the omitzero tag. | ||||||
| // +optional. | ||||||
| AWS *MyAWSConfig `json:"aws,omitempty"` | ||||||
| AWS MyAWSConfig `json:"aws,omitzero"` | ||||||
|
|
||||||
| // azure is the Azure configuration. | ||||||
| // All structures within the union must be optional and pointers. | ||||||
| // All structures within the union must be optional and have the omitzero tag. | ||||||
| // +optional. | ||||||
| Azure *MyAzureConfig `json:"azure,omitempty"` | ||||||
| Azure MyAzureConfig `json:"azure,omitzero"` | ||||||
|
|
||||||
| // gcp is the GCP configuration. | ||||||
| // All structures within the union must be optional and pointers. | ||||||
| // All structures within the union must be optional and have the omitzero tag. | ||||||
| // +optional. | ||||||
| GCP *MyGCPConfig `json:"gcp,omitempty"` | ||||||
| GCP MyGCPConfig `json:"gcp,omitzero"` | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| The discriminator here allows the consumer to determine which of the configuration structures they should be consuming, AWS, Azure or GCP. | ||||||
|
|
||||||
| Important to note: | ||||||
| * All structs within the union **MUST** be pointers | ||||||
| * All structs within the union **MUST** have the omitzero tag | ||||||
| * All structs within the union **MUST** be optional | ||||||
| * All structs within the union **MUST** not allow the zero value (i.e. avoid allowing `{}` for structs with required fields) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit: the current wording could be read as "for structs that have required fields, don't allow |
||||||
| * The discriminant should be required | ||||||
| * The discriminant **MUST** be a string (or string alias) type | ||||||
| * Discriminant values should be PascalCase and should be equivalent to the camelCase field name (json tag) of one member of the union | ||||||
| * Empty union members (discriminant values without a paired union member) are also permitted | ||||||
| * Optional union members (discriminant values with an optional union member) are also permitted (use `+unionMember,optional` to mark the union member as optional) | ||||||
|
|
||||||
| #### Using union types | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth adding dumb simple guidance (somewhere not necessarily here), like:
This may be obvious to others, but it wasn't obvious to me why we need omitempty for required fields.
Also it seems like there are many other examples in this document where a field is required but not
omitempty, we should probably clean those up. For example:This should be
omitemptyright? There are at least two or three more like this. If scope is creeping we can follow up, but LMK if my understanding is correct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, we would push for that field to have
omitemptyand validations that don't allow the value to be the empty string. The exception to this would be if the empty string is a valid value.I agree that we should probably do a sweeping update of the document to update all the examples, but I'm also okay with incremental updates if we'd like to keep this tightly scoped.