@@ -83,7 +83,7 @@ func Generate(ctx *cue.Context, insts ...*build.Instance) error {
83
83
84
84
g .emitDocs (goName , val .Doc ())
85
85
g .appendf ("type %s " , goName )
86
- if err := g .emitType (val ); err != nil {
86
+ if err := g .emitType (val , false ); err != nil {
87
87
return err
88
88
}
89
89
g .appendf ("\n \n " )
@@ -180,7 +180,7 @@ func (g *generator) addDef(path cue.Path) {
180
180
// emitType generates a CUE value as a Go type.
181
181
// When possible, the Go type is emitted in the form of a reference.
182
182
// Otherwise, an inline Go type expression is used.
183
- func (g * generator ) emitType (val cue.Value ) error {
183
+ func (g * generator ) emitType (val cue.Value , optional bool ) error {
184
184
goAttr := val .Attribute ("go" )
185
185
// We prefer the form @go(Name,type=pkg.Baz) as it is explicit and extensible,
186
186
// but we are also backwards compatible with @go(Name,pkg.Baz) as emitted by `cue get go`.
@@ -201,6 +201,18 @@ func (g *generator) emitType(val cue.Value) error {
201
201
g .appendf ("%s" , attrType )
202
202
return nil
203
203
}
204
+ // TODO: should we ensure that optional fields are always nilable in Go?
205
+ // On one hand this allows telling int64(0) apart from a missing field,
206
+ // but on the other, it's often unnecessary and leads to clumsy types.
207
+ // Perhaps add a @go() attribute parameter to require nullability.
208
+ //
209
+ // For now, only structs are always pointers when optional.
210
+ // This is necessary to allow recursive Go types such as linked lists.
211
+ // Pointers to structs are still OK in terms of UX, given that
212
+ // one can do X.PtrY.Z without needing to do (*X.PtrY).Z.
213
+ if optional && cue .Dereference (val ).IncompleteKind () == cue .StructKind {
214
+ g .appendf ("*" )
215
+ }
204
216
// TODO: support nullable types, such as `null | #SomeReference` and
205
217
// `null | {foo: int}`.
206
218
if g .emitTypeReference (val ) {
@@ -210,7 +222,7 @@ func (g *generator) emitType(val cue.Value) error {
210
222
case cue .StructKind :
211
223
if elem := val .LookupPath (cue .MakePath (cue .AnyString )); elem .Err () == nil {
212
224
g .appendf ("map[string]" )
213
- if err := g .emitType (elem ); err != nil {
225
+ if err := g .emitType (elem , false ); err != nil {
214
226
return err
215
227
}
216
228
break
@@ -242,19 +254,7 @@ func (g *generator) emitType(val cue.Value) error {
242
254
}
243
255
244
256
g .appendf ("%s " , goName )
245
- // TODO: should we ensure that optional fields are always nilable in Go?
246
- // On one hand this allows telling int64(0) apart from a missing field,
247
- // but on the other, it's often unnecessary and leads to clumsy types.
248
- // Perhaps add a @go() attribute parameter to require nullability.
249
- //
250
- // For now, only structs are always pointers when optional.
251
- // This is necessary to allow recursive Go types such as linked lists.
252
- // Pointers to structs are still OK in terms of UX, given that
253
- // one can do X.PtrY.Z without needing to do (*X.PtrY).Z.
254
- if optional && cue .Dereference (val ).IncompleteKind () == cue .StructKind {
255
- g .appendf ("*" )
256
- }
257
- if err := g .emitType (val ); err != nil {
257
+ if err := g .emitType (val , optional ); err != nil {
258
258
return err
259
259
}
260
260
// TODO: should we generate cuego tags like `cue:"expr"`?
@@ -275,7 +275,7 @@ func (g *generator) emitType(val cue.Value) error {
275
275
if ! elem .Exists () {
276
276
// TODO: perhaps mention the original type.
277
277
g .appendf ("any /* CUE closed list */" )
278
- } else if err := g .emitType (elem ); err != nil {
278
+ } else if err := g .emitType (elem , false ); err != nil {
279
279
return err
280
280
}
281
281
0 commit comments