this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: fix non-null map values being detected as nullable

Instead of forwarding the type attributes of the map, we now
check the value type for attributes.

Change-Id: I8745c7581002ae3fd9d3116d3a86cf804897980a
Signed-off-by: Tim Windelschmidt <tim@monogon.tech>
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1227302
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

authored by

Tim Windelschmidt and committed by
Daniel Martí
50371eba eecd7f7d

+21 -10
+15 -9
cmd/cue/cmd/get_go.go
··· 1284 1284 1285 1285 f := &cueast.Field{ 1286 1286 Label: cueast.NewList(e.ident("string", false)), 1287 - Value: e.makeType(typ.Elem(), kind, nullable), 1287 + Value: e.makeType(typ.Elem(), kind, e.fieldAttributesFromType(typ.Elem())), 1288 1288 } 1289 1289 cueast.SetRelPos(f, cuetoken.Blank) 1290 1290 return &cueast.StructLit{ ··· 1506 1506 hasFlag(tag, "yaml", "inline", 1) 1507 1507 } 1508 1508 1509 + func (e *extractor) fieldAttributesFromType(f types.Type) (attrs fieldAttributes) { 1510 + switch f.(type) { 1511 + case *types.Pointer: 1512 + attrs |= optional 1513 + 1514 + // In k8s semantics a pointer doesn't count as nullable. 1515 + if !e.k8sSemantic { 1516 + attrs |= nullable 1517 + } 1518 + } 1519 + return attrs 1520 + } 1521 + 1509 1522 func (e *extractor) detectFieldAttributes(f *types.Var, doc *ast.CommentGroup, tag string) (fieldAttributes, error) { 1510 1523 var attrs fieldAttributes 1511 1524 // See k8s docs https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md ··· 1528 1541 return attrs, nil 1529 1542 } 1530 1543 1531 - if _, ok := f.Type().(*types.Pointer); ok { 1532 - attrs |= optional 1533 - 1534 - // In k8s semantics a pointer doesn't count as nullable. 1535 - if !e.k8sSemantic { 1536 - attrs |= nullable 1537 - } 1538 - } 1544 + attrs |= e.fieldAttributesFromType(f.Type()) 1539 1545 1540 1546 // Go 1.24 added the "omitzero" option to encoding/json, an improvement over "omitempty". 1541 1547 // Note that, as of mid 2025, YAML libraries don't seem to have picked up "omitzero" yet.
+6 -1
cmd/cue/cmd/testdata/script/get_go_types.txtar
··· 86 86 87 87 // Note: Go encodings of protobuf tags are lossy. So this is a best-effort 88 88 // thing. 89 - Map map[string]*CustomJSON `protobuf:"bytes,1,name=intf" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` 89 + Map map[string]*CustomJSON `protobuf:"bytes,1,name=intf" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` 90 + MapString map[string]string 91 + 90 92 Slice1 []int 91 93 Slice2 []interface{} 92 94 Slice3 *[]json.Unmarshaler 95 + Slice4 []*int 93 96 Array1 [5]int 94 97 Array2 [5]interface{} 95 98 Array3 *[5]json.Marshaler ··· 335 338 // Note: Go encodings of protobuf tags are lossy. So this is a best-effort 336 339 // thing. 337 340 Map: {[string]: null | #CustomJSON} @go(,map[string]*CustomJSON) @protobuf(1,map[bytes]bytes,name=intf) 341 + MapString: {[string]: string} @go(,map[string]string) 338 342 Slice1: [...int] @go(,[]int) 339 343 Slice2: [...] @go(,[]interface{}) 340 344 Slice3?: null | [...] @go(,*[]json.Unmarshaler) 345 + Slice4: [...int] @go(,[]*int) 341 346 Array1: 5 * [int] @go(,[5]int) 342 347 Array2: 5 * [_] @go(,[5]interface{}) 343 348 Array3?: null | 5*[_] @go(,*[5]json.Marshaler)