cmd/cue/cmd: also make field optional if it is a pointer
This seems to result in somewhat better schema
and is arguably more correct.
Note that there is no one correct way.
Change-Id: I81a540a9cb979368ec8786e7343fc31b2aee6d93
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4204
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go
index 4476983..9945f90 100644
--- a/cmd/cue/cmd/get_go.go
+++ b/cmd/cue/cmd/get_go.go
@@ -1037,6 +1037,9 @@
if e.isOptional(tag) {
kind = cuetoken.OPTION
}
+ if _, ok := f.Type().(*types.Pointer); ok {
+ kind = cuetoken.OPTION
+ }
cueType := e.printField(name, kind, f.Type(), docs[i], count > 0)
// Add field tag to convert back to Go.
diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
index a9de907..b56e20b 100644
--- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
+++ b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
@@ -16,12 +16,12 @@
String: string
Inline
- NoInline: NoInline
- CustomJSON: CustomJSON
- CustomYAML: null | CustomYAML @go(,*CustomYAML)
- AnyJSON: _ @go(,json.Marshaler)
- AnyText: string @go(,encoding.TextMarshaler)
- bar?: int @go(Bar)
+ NoInline: NoInline
+ CustomJSON: CustomJSON
+ CustomYAML?: null | CustomYAML @go(,*CustomYAML)
+ AnyJSON: _ @go(,json.Marshaler)
+ AnyText: string @go(,encoding.TextMarshaler)
+ bar?: int @go(Bar)
// Time is mapped to CUE's internal type.
Time: time.Time
@@ -29,12 +29,12 @@
Map: {[string]: null | CustomJSON} @go(,map[string]*CustomJSON)
Slice1: [...int] @go(,[]int)
Slice2: [...] @go(,[]interface{})
- Slice3: null | [...] @go(,*[]json.Unmarshaler)
- Array1: 5 * [int] @go(,[5]int)
- Array2: 5 * [_] @go(,[5]interface{})
- Array3: null | 5*[_] @go(,*[5]json.Marshaler)
- Intf: Interface @protobuf(2,varint,name=intf)
- Intf2: _ @go(,interface{})
+ Slice3?: null | [...] @go(,*[]json.Unmarshaler)
+ Array1: 5 * [int] @go(,[5]int)
+ Array2: 5 * [_] @go(,[5]interface{})
+ Array3?: null | 5*[_] @go(,*[5]json.Marshaler)
+ Intf: Interface @protobuf(2,varint,name=intf)
+ Intf2: _ @go(,interface{})
Intf3: {
Interface: Interface
} @go(,struct{Interface})
diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue
index 3ba6fb0..41dc9b9 100644
--- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue
+++ b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue
@@ -13,10 +13,10 @@
Barzer :: {
a: int @go(A) @protobuf(2,varint,)
T: t.Time
- B: null | int @go(,*big.Int)
+ B?: null | int @go(,*big.Int)
C: int @go(,big.Int)
F: string @go(,big.Float) @xml(,attr)
- G: null | string @go(,*big.Float)
+ G?: null | string @go(,*big.Float)
S: string
"x-y": bool @go(XY)
Err: _ @go(,error)