Skip to content

Commit

Permalink
cue/ast: introduce NewNull()
Browse files Browse the repository at this point in the history
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 <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 13, 2020
1 parent 519db58 commit 197f4c8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ func (e *extractor) makeType(expr types.Type) (result cueast.Expr) {
switch x := expr.(type) {
case *types.Pointer:
return &cueast.BinaryExpr{
X: e.ident("null", false),
X: cueast.NewNull(),
Op: cuetoken.OR,
Y: e.makeType(x.Elem()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ import "strings"
// environment variables. The variable is prefixed with INPUT_
// and converted to upper case.
with?: #env & {
args?: string, entrypoint?: string
...
args?: string, entrypoint?: string, ...
}

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

{[!~"^(in|ex)clude$" & !~"^()$"]: [...#configuration] & [_, ...]}
Expand Down
6 changes: 6 additions & 0 deletions cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@ func NewString(str string) *BasicLit {
return &BasicLit{Kind: token.STRING, ValuePos: token.NoPos, Value: str}
}

// NewNull creates a new BasicLit configured to be a null value.
// Useful for ASTs generated by code other than the CUE parser.
func NewNull() *BasicLit {
return &BasicLit{Kind: token.NULL, Value: "null"}
}

// NewLit creates a new BasicLit with from a token type and string without
// position.
// Useful for ASTs generated by code other than the CUE parser.
Expand Down
2 changes: 1 addition & 1 deletion cue/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func (p *exporter) expr(v value) ast.Expr {
}

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

case *boolLit:
return ast.NewBool(x.b)
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (s *state) finalize() (e ast.Expr) {
switch k {
case cue.NullKind:
// TODO: handle OpenAPI restrictions.
add(ast.NewIdent("null"))
add(ast.NewNull())
case cue.BoolKind:
add(ast.NewIdent("bool"))
case cue.StringKind:
Expand Down
2 changes: 1 addition & 1 deletion encoding/protobuf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (p *protoConverter) mapBuiltinPackage(pos scanner.Position, file string, re
}, nil)

p.setBuiltin("google.protobuf.NullValue", func() ast.Expr {
return ast.NewLit(token.NULL, "null")
return ast.NewNull()
}, nil)

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

0 comments on commit 197f4c8

Please sign in to comment.