@@ -241,15 +241,20 @@ kubebuilder types):
241
241
242
242
- ` // +unionDiscriminator ` before a field means that this field is the
243
243
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
247
248
field is a member of a union. The ` <memberName> ` is the name of the field that will be set as the discriminator value.
248
249
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 ` .
253
258
254
259
#### Discriminator Values
255
260
@@ -290,8 +295,9 @@ type UnionType string
290
295
291
296
const (
292
297
FieldA UnionType = "FieldA"
293
- FieldB = "FieldC "
298
+ FieldB = "FieldB "
294
299
FieldC = "FieldC"
300
+ FieldD = "FieldD"
295
301
FieldNone = ""
296
302
)
297
303
@@ -321,12 +327,22 @@ type TopLevelUnion struct {
321
327
Union `json:",inline"`
322
328
}
323
329
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
+
324
341
// This will generate one union, with two fields and a discriminator.
325
342
type Union struct {
326
343
// +unionDiscriminator
327
- // +unionAllowEmpty
328
344
// +required
329
- UnionType string `json:"unionType"`
345
+ UnionType UnionType `json:"unionType"`
330
346
331
347
// +unionMember
332
348
// +optional
@@ -336,34 +352,54 @@ type Union struct {
336
352
FieldB int `json:"fieldB"`
337
353
}
338
354
355
+ // +enum
356
+ type Union2Type string
357
+
358
+ const (
359
+ Alpha Union2Type = "ALPHA"
360
+ Beta = "BETA"
361
+ )
362
+
339
363
// This will generate one union that can be embedded because the members explicitly define their discriminator.
340
364
// Also, the unionMember markers here demonstrate how to customize the names used for
341
365
each field in the discriminator.
342
366
type Union2 struct {
343
367
// +unionDiscriminator
344
368
// +required
345
- Type string `json:"type"`
346
- // +unionMember=ALPHA,discriminatedBy=type
369
+ Type2 Union2Type `json:"type"`
370
+ // +unionMember=ALPHA,
371
+ // +unionDiscriminatedBy=Type2
347
372
// +optional
348
373
Alpha int `json:"alpha"`
349
- // +unionMember=BETA,discriminatedBy=type
374
+ // +unionMember=BETA
375
+ // +unionDiscriminatedBy=Type2
350
376
// +optional
351
377
Beta int `json:"beta"`
352
378
}
353
379
380
+ // +enum
381
+ type FieldType string
382
+
383
+ const (
384
+ Field1 FieldType = "Field1"
385
+ Field2 = "Field2"
386
+ FieldNone = "None"
387
+ )
388
+
354
389
// This has 3 embedded unions:
355
390
// One for the fields that are directly embedded, one for Union, and one for Union2.
356
391
type InlinedUnion struct {
357
392
Name string `json:"name"`
358
393
359
394
// +unionDiscriminator
360
- // +unionAllowEmpty
361
395
// +required
362
- FieldType string `json:"fieldType"`
363
- // +unionMember,discriminatedBy=fieldType
396
+ FieldType FieldType `json:"fieldType"`
397
+ // +unionMember
398
+ // +unionDiscriminatedBy=FieldType
364
399
// +optional
365
400
Field1 *int `json:"field1,omitempty"`
366
- // +unionMember,discriminatedBy=fieldType
401
+ // +unionMember
402
+ // +unionDiscriminatedBy=FieldType
367
403
// +optional
368
404
Field2 *int `json:"field2,omitempty"`
369
405
0 commit comments