Skip to content

Commit 7fa7d48

Browse files
committed
Address more feedback
1 parent 921f0a0 commit 7fa7d48

File tree

1 file changed

+53
-17
lines changed
  • keps/sig-api-machinery/1027-api-unions

1 file changed

+53
-17
lines changed

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

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,20 @@ kubebuilder types):
241241

242242
- `// +unionDiscriminator` before a field means that this field is the
243243
discriminator for the union. This field MUST be an enum defined as a string (see section on
244-
discriminator values). This field MUST be
245-
required.
246-
- `// +unionMember=<memberName>,discriminatedBy=<discriminatorName>` before a field means that this
244+
discriminator values). This field MUST be required if there is no default
245+
option, omitempty if the default option is the empty string, or optional and
246+
omitempty if a default value is specified with the `// +default` marker.
247+
- `// +unionMember=<memberName> before a field means that this
247248
field is a member of a union. The `<memberName>` is the name of the field that will be set as the discriminator value.
248249
It MUST correspond to one of the valid enum values of the discriminator's enum
249-
type. It defaults to the `CamelCase` representation of the field name if not specified. The `<discriminatorName>` is optional if there
250-
is only union in a struct and required if there are multiple unions per
251-
struct. It should be the `CamelCase` representation of the field tagged with
252-
`unionDiscriminator`.
250+
type. It defaults to the go (i.e `CamelCase`) representation of the field name if not specified.
251+
`<memberName>` should only be set if authors want to customize how the fields
252+
are represented in the discriminator field.
253+
- `// +unionDiscriminatedBy=<discriminatorName>` before a member field identifies which
254+
discriminator (and thus which union) the member field belongs to. Optional
255+
unless there are multiple unions/discriminators in a single struct. If used,
256+
it must be the go (i.e. `CamelCase`) representation of the field name tagged
257+
with `unionDiscriminator`.
253258

254259
#### Discriminator Values
255260

@@ -290,8 +295,9 @@ type UnionType string
290295
291296
const (
292297
FieldA UnionType = "FieldA"
293-
FieldB = "FieldC"
298+
FieldB = "FieldB"
294299
FieldC = "FieldC"
300+
FieldD = "FieldD"
295301
FieldNone = ""
296302
)
297303
@@ -321,12 +327,22 @@ type TopLevelUnion struct {
321327
Union `json:",inline"`
322328
}
323329
330+
// +enum
331+
type UnionType string
332+
333+
const (
334+
FieldA UnionType = "FieldA"
335+
FieldB = "FieldB"
336+
FieldC = "FieldC"
337+
FieldD = "FieldD"
338+
FieldNone = ""
339+
)
340+
324341
// This will generate one union, with two fields and a discriminator.
325342
type Union struct {
326343
// +unionDiscriminator
327-
// +unionAllowEmpty
328344
// +required
329-
UnionType string `json:"unionType"`
345+
UnionType UnionType `json:"unionType"`
330346
331347
// +unionMember
332348
// +optional
@@ -336,34 +352,54 @@ type Union struct {
336352
FieldB int `json:"fieldB"`
337353
}
338354
355+
// +enum
356+
type Union2Type string
357+
358+
const (
359+
Alpha Union2Type = "ALPHA"
360+
Beta = "BETA"
361+
)
362+
339363
// This will generate one union that can be embedded because the members explicitly define their discriminator.
340364
// Also, the unionMember markers here demonstrate how to customize the names used for
341365
each field in the discriminator.
342366
type Union2 struct {
343367
// +unionDiscriminator
344368
// +required
345-
Type string `json:"type"`
346-
// +unionMember=ALPHA,discriminatedBy=type
369+
Type2 Union2Type `json:"type"`
370+
// +unionMember=ALPHA,
371+
// +unionDiscriminatedBy=Type2
347372
// +optional
348373
Alpha int `json:"alpha"`
349-
// +unionMember=BETA,discriminatedBy=type
374+
// +unionMember=BETA
375+
// +unionDiscriminatedBy=Type2
350376
// +optional
351377
Beta int `json:"beta"`
352378
}
353379
380+
// +enum
381+
type FieldType string
382+
383+
const (
384+
Field1 FieldType = "Field1"
385+
Field2 = "Field2"
386+
FieldNone = "None"
387+
)
388+
354389
// This has 3 embedded unions:
355390
// One for the fields that are directly embedded, one for Union, and one for Union2.
356391
type InlinedUnion struct {
357392
Name string `json:"name"`
358393
359394
// +unionDiscriminator
360-
// +unionAllowEmpty
361395
// +required
362-
FieldType string `json:"fieldType"`
363-
// +unionMember,discriminatedBy=fieldType
396+
FieldType FieldType `json:"fieldType"`
397+
// +unionMember
398+
// +unionDiscriminatedBy=FieldType
364399
// +optional
365400
Field1 *int `json:"field1,omitempty"`
366-
// +unionMember,discriminatedBy=fieldType
401+
// +unionMember
402+
// +unionDiscriminatedBy=FieldType
367403
// +optional
368404
Field2 *int `json:"field2,omitempty"`
369405

0 commit comments

Comments
 (0)