Skip to content

Commit 7da001c

Browse files
committed
remove multiple unions per object possibility
1 parent 5e85c60 commit 7da001c

File tree

1 file changed

+5
-62
lines changed
  • keps/sig-api-machinery/1027-api-unions

1 file changed

+5
-62
lines changed

keps/sig-api-machinery/1027-api-unions/README.md

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,6 @@ kubebuilder types):
260260
is not present in the `unionMember` tag, then the object will fail validation
261261
if the discriminator selects the field but it is nil. A field can be marked as
262262
optional without specifying memberName via `// +unionMember,optional`.
263-
- `// +unionDiscriminatedBy=<discriminatorName>` before a member field identifies which
264-
discriminator (and thus which union) the member field belongs to. Optional
265-
unless there are multiple unions/discriminators in a single struct. If used,
266-
it must be the go (i.e. `CamelCase`) representation of the field name tagged
267-
with `unionDiscriminator`.
268263

269264
#### Discriminator Values
270265

@@ -352,7 +347,7 @@ type Union struct {
352347
// +unionMember
353348
// +optional
354349
FieldA int `json:"fieldA"`
355-
// +unionMember
350+
// +unionMember,optional
356351
// +optional
357352
FieldB int `json:"fieldB"`
358353
}
@@ -365,54 +360,20 @@ const (
365360
Beta = "BETA"
366361
)
367362
368-
// This will generate one union that can be embedded because the members explicitly define their discriminator.
369-
// Also, the unionMember markers here demonstrate how to customize the names used for
370-
each field in the discriminator.
363+
// This generates a union where the unionMember markers demonstrate how to
364+
customize the names used for each field in the discriminator.
371365
type Union2 struct {
372366
// +unionDiscriminator
373367
// +required
374368
Type2 Union2Type `json:"type"`
375-
// +unionMember=ALPHA,
376-
// +unionDiscriminatedBy=Type2
369+
// +unionMember=ALPHA
377370
// +optional
378371
Alpha int `json:"alpha"`
379-
// +unionMember=BETA
380-
// +unionDiscriminatedBy=Type2
372+
// +unionMember=BETA,optional
381373
// +optional
382374
Beta int `json:"beta"`
383375
}
384376
385-
// +enum
386-
type FieldType string
387-
388-
const (
389-
Field1 FieldType = "Field1"
390-
Field2 = "Field2"
391-
FieldNone = "None"
392-
)
393-
394-
// This has 3 embedded unions:
395-
// One for the fields that are directly embedded, one for Union, and one for Union2.
396-
type InlinedUnion struct {
397-
Name string `json:"name"`
398-
399-
// +unionDiscriminator
400-
// +required
401-
FieldType FieldType `json:"fieldType"`
402-
// +unionMember
403-
// +unionDiscriminatedBy=FieldType
404-
// +optional
405-
Field1 *int `json:"field1,omitempty"`
406-
// +unionMember
407-
// +unionDiscriminatedBy=FieldType
408-
// +optional
409-
Field2 *int `json:"field2,omitempty"`
410-
411-
// Union does not label its members, so it
412-
cannot be inlined
413-
union Union `json:"union"`
414-
Union2 `json:",inline"`
415-
}
416377
```
417378

418379
### OpenAPI
@@ -441,12 +402,6 @@ const (
441402
FieldD Union1Type = "FieldD"
442403
FieldNone Union1Type = ""
443404
)
444-
const (
445-
Alpha Union2Type = "ALPHA"
446-
Beta Union2Type = "BETA"
447-
Gamma Union2Type = "GAMMA"
448-
Null Union2Type = "NULL"
449-
)
450405
451406
// This will generate one union, with two fields and a discriminator.
452407
type Union struct {
@@ -463,18 +418,6 @@ type Union struct {
463418
// +optional
464419
FieldB int `json:"fieldB"`
465420
466-
// +unionDiscriminator
467-
// +required
468-
Union2 Union2Type `json:"union2"`
469-
470-
// +unionMember=ALPHA,
471-
// +unionDiscriminatedBy=Union2
472-
// +optional
473-
Alpha int `json:"alpha"`
474-
// +unionMember=BETA,optional
475-
// +unionDiscriminatedBy=Union2
476-
// +optional
477-
Beta int `json:"beta"`
478421
}
479422
```
480423

0 commit comments

Comments
 (0)