Skip to content

Commit defc384

Browse files
committed
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 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214379 Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent f2e0b7e commit defc384

File tree

1 file changed

+73
-10
lines changed

1 file changed

+73
-10
lines changed

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

Lines changed: 73 additions & 10 deletions
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:127:2
191+
./root/root.cue:136: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:127:2
194+
./root/root.cue:136: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):
@@ -251,6 +251,7 @@ import (
251251
"slices"
252252

253253
"foo.test/bar/root"
254+
imported "foo.test/bar/imported/subinst"
254255
)
255256

256257
type Tests struct {
@@ -267,8 +268,9 @@ func main() {
267268
var _ root.AttrChangedNameEmbed
268269
var _ = root.AttrType(constant.Kind(0))
269270

270-
// It's easier to test the fields with a root value.
271+
// It's easier to test the fields with zero values.
271272
var zeroRoot root.Root
273+
var zeroRootImports root.RootImports
272274

273275
var _ = zeroRoot.Embedded1
274276
var _ = zeroRoot.Embedded2
@@ -282,12 +284,27 @@ func main() {
282284
zeroRoot.DiscriminatorField = make(map[string]any)
283285

284286
// Optional fields which use the optional attribute flag.
287+
285288
zeroRoot.Fields.OptionalBasic = 5
286289
zeroRoot.Fields.OptionalList = make([]int64, 0)
287290
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)
291+
292+
zeroRoot.Fields.OptionalTopAttrNillable = (*any)(nil)
293+
zeroRoot.Fields.OptionalNullAttrNillable = (**struct{})(nil)
294+
zeroRoot.Fields.OptionalBasicAttrNillable = (*int64)(nil)
295+
zeroRoot.Fields.OptionalListAttrNillable = (*[]int64)(nil)
296+
297+
zeroRoot.Fields.OptionalInlineMapAttrNillable = (*map[string]int64)(nil)
298+
zeroRoot.Fields.OptionalLocalMapAttrNillable = (*root.LocalMap)(nil)
299+
zeroRoot.Fields.OptionalInlineNestedAttrNillable = (*struct{F *[]string `json:"f,omitempty"`})(nil)
300+
zeroRoot.Fields.OptionalLocalNestedAttrNillable = (*root.LocalNested)(nil)
301+
302+
// #localNested is not affected by one of the references using optional=nullable.
303+
var _ = root.LocalNested{F: []string{}}
304+
305+
zeroRootImports.OptionalRemoteMapAttrNillable = (*imported.RemoteMap)(nil)
306+
zeroRootImports.OptionalRemoteNestedAttrNillable = (*imported.RemoteNested)(nil)
307+
291308
zeroRoot.Fields.OptionalStruct = root.EmptyStruct{}
292309
zeroRoot.Fields.OptionalStructAttrType = root.EmptyStruct{}
293310
zeroRoot.Fields.OptionalStructAttrZero = root.EmptyStruct{}
@@ -389,9 +406,18 @@ _#overridenNeverGenerate: string
389406
optionalList?: [...int]
390407
optionalMap?: [string]: int
391408

409+
optionalTopAttrNillable?: _ @go(,optional=nillable)
410+
optionalNullAttrNillable?: null @go(,optional=nillable)
392411
optionalBasicAttrNillable?: int @go(,optional=nillable)
393412
optionalListAttrNillable?: [...int] @go(,optional=nillable)
394-
optionalMapAttrNillable?: {[string]: int} @go(,optional=nillable)
413+
414+
// Ensure that "is nillable" is worked out correctly for inline types, referenced definitions,
415+
// and nested types where only part of the type is not nillable, but not the top level.
416+
417+
optionalInlineMapAttrNillable?: {[string]: int} @go(,optional=nillable)
418+
optionalLocalMapAttrNillable?: #localMap @go(,optional=nillable)
419+
optionalInlineNestedAttrNillable?: {f?: [...string]} @go(,optional=nillable)
420+
optionalLocalNestedAttrNillable?: #localNested @go(,optional=nillable)
395421

396422
optionalStruct?: #emptyStruct
397423
optionalStructAttrType?: #emptyStruct @go(,type=EmptyStruct)
@@ -498,6 +524,9 @@ _#hiddenStruct: {
498524
}
499525
}
500526

527+
#localMap: [string]: int
528+
#localNested: f?: [...string]
529+
501530
// Hidden definitions are only generated if referenced; this one is not.
502531
_#unusedHiddenStruct: neverGenerate?: int
503532

@@ -580,7 +609,7 @@ import (
580609
"foo.test/bar/imported/unused"
581610
)
582611

583-
#remoteStructs: {
612+
#rootImports: {
584613
inst?: imported.#instanceStruct
585614

586615
lowerRegular?: imported.lowerRegular
@@ -590,6 +619,9 @@ import (
590619

591620
multiOne?: multipkg_one.#One
592621
multiTwo?: multipkg_two.#Two
622+
623+
optionalRemoteMapAttrNillable?: imported.#remoteMap @go(,optional=nillable)
624+
optionalRemoteNestedAttrNillable?: imported.#remoteNested @go(,optional=nillable)
593625
}
594626

595627
_unusedImport: unused.#UnusedNeverGenerate
@@ -606,7 +638,7 @@ import (
606638
"time"
607639
)
608640

609-
type RemoteStructs struct {
641+
type RootImports struct {
610642
Inst imported.InstanceStruct `json:"inst,omitempty"`
611643

612644
LowerRegular int64 `json:"lowerRegular,omitempty"`
@@ -620,6 +652,10 @@ type RemoteStructs struct {
620652
MultiOne multipkg.One `json:"multiOne,omitempty"`
621653

622654
MultiTwo multipkg.Two `json:"multiTwo,omitempty"`
655+
656+
OptionalRemoteMapAttrNillable *imported.RemoteMap `json:"optionalRemoteMapAttrNillable,omitempty"`
657+
658+
OptionalRemoteNestedAttrNillable *imported.RemoteNested `json:"optionalRemoteNestedAttrNillable,omitempty"`
623659
}
624660

625661
type Types struct {
@@ -703,11 +739,23 @@ type Root struct {
703739

704740
OptionalMap map[string]int64 `json:"optionalMap,omitempty"`
705741

742+
OptionalTopAttrNillable *any/* CUE top */ `json:"optionalTopAttrNillable,omitempty"`
743+
744+
OptionalNullAttrNillable **struct{}/* CUE null */ `json:"optionalNullAttrNillable,omitempty"`
745+
706746
OptionalBasicAttrNillable *int64 `json:"optionalBasicAttrNillable,omitempty"`
707747

708748
OptionalListAttrNillable *[]int64 `json:"optionalListAttrNillable,omitempty"`
709749

710-
OptionalMapAttrNillable *map[string]int64 `json:"optionalMapAttrNillable,omitempty"`
750+
OptionalInlineMapAttrNillable *map[string]int64 `json:"optionalInlineMapAttrNillable,omitempty"`
751+
752+
OptionalLocalMapAttrNillable *LocalMap `json:"optionalLocalMapAttrNillable,omitempty"`
753+
754+
OptionalInlineNestedAttrNillable *struct {
755+
F *[]string `json:"f,omitempty"`
756+
} `json:"optionalInlineNestedAttrNillable,omitempty"`
757+
758+
OptionalLocalNestedAttrNillable *LocalNested `json:"optionalLocalNestedAttrNillable,omitempty"`
711759

712760
OptionalStruct EmptyStruct `json:"optionalStruct,omitempty"`
713761

@@ -804,6 +852,12 @@ type Root struct {
804852

805853
}
806854

855+
type LocalMap map[string]int64
856+
857+
type LocalNested struct {
858+
F []string `json:"f,omitempty"`
859+
}
860+
807861
type Root_innerStruct struct {
808862
InnerStructField int64 `json:"innerStructField,omitempty"`
809863
}
@@ -861,6 +915,9 @@ lowerRegular: int
861915
UpperRegular: int
862916
#lowerDef: int
863917
#UpperDef: int
918+
919+
#remoteMap: [string]: int
920+
#remoteNested: f?: [...string]
864921
-- imported/subinst/cue_types_imported_gen.go.want --
865922
// Code generated by "cue exp gengotypes"; DO NOT EDIT.
866923

@@ -879,6 +936,12 @@ type InstanceStruct struct {
879936
type LowerDef int64
880937

881938
type UpperDef int64
939+
940+
type RemoteMap map[string]int64
941+
942+
type RemoteNested struct {
943+
F []string `json:"f,omitempty"`
944+
}
882945
-- imported/indirect/indirect.cue --
883946
package indirect
884947

0 commit comments

Comments
 (0)