Skip to content

Commit 6db9259

Browse files
mvdancueckoo
authored andcommitted
cmd/cue: add more gengotypes test cases for optional=nillable
To make sure that fields which normally generate as interfaces, pointers, and references to named types will also work as expected. While here, start using nil conversions for the gotest.go checks, just so that we can consistently validate what each type ends up as while at the same time sanity checking they can all be assigned to nil. For #3760. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I964367b0b945f68bd58b646c5e39dd191a0b40dd Dispatch-Trailer: {"type":"trybot","CL":1214379,"patchset":1,"ref":"refs/changes/79/1214379/1","targetBranch":"master"}
1 parent 2f2a88d commit 6db9259

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

cmd/cue/cmd/testdata/script/exp_gengotypes.txtar

+47-7
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ fail.both."42_LinkedList".types.LinkedList.next: conflicting values "x" and {ite
188188
./root/types.cue:43:14
189189
fail.both.notList: conflicting values [1,2,3] and {embedded2?:int} (mismatched types list and struct):
190190
./cuetest/all.cue:5:24
191-
./root/root.cue:122:2
191+
./root/root.cue:131:2
192192
fail.both.notString: conflicting values "not_a_struct" and {embedded2?:int} (mismatched types string and struct):
193193
./cuetest/all.cue:4:24
194-
./root/root.cue:122:2
194+
./root/root.cue:131:2
195195
fail.cue."11_Int8".types.Int8: invalid value 99999 (out of bound <=127):
196196
./cuetest/all.cue:61:30
197197
fail.cue."12_Int8".types.Int8: invalid value -99999 (out of bound >=-128):
@@ -282,12 +282,21 @@ func main() {
282282
zeroRoot.DiscriminatorField = make(map[string]any)
283283

284284
// Optional fields which use the optional attribute flag.
285+
285286
zeroRoot.Fields.OptionalBasic = 5
286287
zeroRoot.Fields.OptionalList = make([]int64, 0)
287288
zeroRoot.Fields.OptionalMap = make(map[string]int64, 0)
288-
zeroRoot.Fields.OptionalBasicAttrNillable = new(int64)
289-
zeroRoot.Fields.OptionalListAttrNillable = new([]int64)
290-
zeroRoot.Fields.OptionalMapAttrNillable = new(map[string]int64)
289+
290+
zeroRoot.Fields.OptionalTopAttrNillable = (*any)(nil)
291+
zeroRoot.Fields.OptionalNullAttrNillable = (**struct{})(nil)
292+
zeroRoot.Fields.OptionalBasicAttrNillable = (*int64)(nil)
293+
zeroRoot.Fields.OptionalListAttrNillable = (*[]int64)(nil)
294+
295+
zeroRoot.Fields.OptionalInlineMapAttrNillable = (*map[string]int64)(nil)
296+
zeroRoot.Fields.OptionalNamedMapAttrNillable = (*root.NamedMap)(nil)
297+
zeroRoot.Fields.OptionalInlineNestedAttrNillable = (*struct{F *[]string `json:"f,omitempty"`})(nil)
298+
zeroRoot.Fields.OptionalNamedNestedAttrNillable = (*root.NamedNested)(nil)
299+
291300
zeroRoot.Fields.OptionalStruct = root.EmptyStruct{}
292301
zeroRoot.Fields.OptionalStructAttrType = root.EmptyStruct{}
293302
zeroRoot.Fields.OptionalStructAttrZero = root.EmptyStruct{}
@@ -389,9 +398,18 @@ _#overridenNeverGenerate: string
389398
optionalList?: [...int]
390399
optionalMap?: [string]: int
391400

401+
optionalTopAttrNillable?: _ @go(,optional=nillable)
402+
optionalNullAttrNillable?: null @go(,optional=nillable)
392403
optionalBasicAttrNillable?: int @go(,optional=nillable)
393404
optionalListAttrNillable?: [...int] @go(,optional=nillable)
394-
optionalMapAttrNillable?: {[string]: int} @go(,optional=nillable)
405+
406+
// Ensure that "is nillable" is worked out correctly for inline types, referenced definitions,
407+
// and nested types where only part of the type is not nillable, but not the top level.
408+
409+
optionalInlineMapAttrNillable?: {[string]: int} @go(,optional=nillable)
410+
optionalNamedMapAttrNillable?: #namedMap @go(,optional=nillable)
411+
optionalInlineNestedAttrNillable?: {f?: [...string]} @go(,optional=nillable)
412+
optionalNamedNestedAttrNillable?: #namedNested @go(,optional=nillable)
395413

396414
optionalStruct?: #emptyStruct
397415
optionalStructAttrType?: #emptyStruct @go(,type=EmptyStruct)
@@ -493,6 +511,10 @@ _#hiddenStruct: {
493511
}
494512
}
495513

514+
#namedMap: [string]: int
515+
516+
#namedNested: f?: [...string]
517+
496518
// Hidden definitions are only generated if referenced; this one is not.
497519
_#unusedHiddenStruct: neverGenerate?: int
498520

@@ -698,11 +720,23 @@ type Root struct {
698720

699721
OptionalMap map[string]int64 `json:"optionalMap,omitempty"`
700722

723+
OptionalTopAttrNillable *any/* CUE top */ `json:"optionalTopAttrNillable,omitempty"`
724+
725+
OptionalNullAttrNillable **struct{}/* CUE null */ `json:"optionalNullAttrNillable,omitempty"`
726+
701727
OptionalBasicAttrNillable *int64 `json:"optionalBasicAttrNillable,omitempty"`
702728

703729
OptionalListAttrNillable *[]int64 `json:"optionalListAttrNillable,omitempty"`
704730

705-
OptionalMapAttrNillable *map[string]int64 `json:"optionalMapAttrNillable,omitempty"`
731+
OptionalInlineMapAttrNillable *map[string]int64 `json:"optionalInlineMapAttrNillable,omitempty"`
732+
733+
OptionalNamedMapAttrNillable *NamedMap `json:"optionalNamedMapAttrNillable,omitempty"`
734+
735+
OptionalInlineNestedAttrNillable *struct {
736+
F *[]string `json:"f,omitempty"`
737+
} `json:"optionalInlineNestedAttrNillable,omitempty"`
738+
739+
OptionalNamedNestedAttrNillable *NamedNested `json:"optionalNamedNestedAttrNillable,omitempty"`
706740

707741
OptionalStruct EmptyStruct `json:"optionalStruct,omitempty"`
708742

@@ -799,6 +833,12 @@ type Root struct {
799833

800834
}
801835

836+
type NamedMap map[string]int64
837+
838+
type NamedNested struct {
839+
F []string `json:"f,omitempty"`
840+
}
841+
802842
type Root_innerStruct struct {
803843
InnerStructField int64 `json:"innerStructField,omitempty"`
804844
}

0 commit comments

Comments
 (0)