Skip to content

Commit 197f4c8

Browse files
committed
cue/ast: introduce NewNull()
This seems like a trivial helper, but defining a null value properly is somewhat unintuitive and cumbersome. This fixes a bug in the go converter and jsonschema as well. Change-Id: I14d2bda3b74f846dbfa7cd19c791c263dc87e7a8 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6305 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 519db58 commit 197f4c8

File tree

6 files changed

+12
-7
lines changed

6 files changed

+12
-7
lines changed

cmd/cue/cmd/get_go.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
942942
switch x := expr.(type) {
943943
case *types.Pointer:
944944
return &cueast.BinaryExpr{
945-
X: e.ident("null", false),
945+
X: cueast.NewNull(),
946946
Op: cuetoken.OR,
947947
Y: e.makeType(x.Elem()),
948948
}

cue.mod/pkg/github.com/SchemaStore/schemastore/src/schemas/json/github-workflow.cue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ import "strings"
424424
// environment variables. The variable is prefixed with INPUT_
425425
// and converted to upper case.
426426
with?: #env & {
427-
args?: string, entrypoint?: string
428-
...
427+
args?: string, entrypoint?: string, ...
429428
}
430429

431430
// Prevents a job from failing when a step fails. Set to true to
@@ -466,7 +465,7 @@ import "strings"
466465
// https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions.
467466
matrix: {
468467
{[=~"^(in|ex)clude$" & !~"^()$"]: [...{
469-
[string]: #configuration
468+
[string]: #configuration
470469
}] & [_, ...]}
471470

472471
{[!~"^(in|ex)clude$" & !~"^()$"]: [...#configuration] & [_, ...]}

cue/ast/ast.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,12 @@ func NewString(str string) *BasicLit {
435435
return &BasicLit{Kind: token.STRING, ValuePos: token.NoPos, Value: str}
436436
}
437437

438+
// NewNull creates a new BasicLit configured to be a null value.
439+
// Useful for ASTs generated by code other than the CUE parser.
440+
func NewNull() *BasicLit {
441+
return &BasicLit{Kind: token.NULL, Value: "null"}
442+
}
443+
438444
// NewLit creates a new BasicLit with from a token type and string without
439445
// position.
440446
// Useful for ASTs generated by code other than the CUE parser.

cue/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ func (p *exporter) expr(v value) ast.Expr {
613613
}
614614

615615
case *nullLit:
616-
return &ast.BasicLit{Kind: token.NULL, Value: "null"}
616+
return ast.NewNull()
617617

618618
case *boolLit:
619619
return ast.NewBool(x.b)

encoding/jsonschema/decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func (s *state) finalize() (e ast.Expr) {
314314
switch k {
315315
case cue.NullKind:
316316
// TODO: handle OpenAPI restrictions.
317-
add(ast.NewIdent("null"))
317+
add(ast.NewNull())
318318
case cue.BoolKind:
319319
add(ast.NewIdent("bool"))
320320
case cue.StringKind:

encoding/protobuf/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (p *protoConverter) mapBuiltinPackage(pos scanner.Position, file string, re
9999
}, nil)
100100

101101
p.setBuiltin("google.protobuf.NullValue", func() ast.Expr {
102-
return ast.NewLit(token.NULL, "null")
102+
return ast.NewNull()
103103
}, nil)
104104

105105
p.setBuiltin("google.protobuf.ListValue", func() ast.Expr {

0 commit comments

Comments
 (0)