Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue/ast: add NewBool helper
Browse files Browse the repository at this point in the history
Change-Id: Iccd88259a08d00e5da589b962a69e675e39a6234
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4460
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 19, 2019
1 parent 4d5f5c7 commit e2650e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ func NewString(str string) *BasicLit {
return &BasicLit{Kind: token.STRING, ValuePos: token.NoPos, Value: str}
}

// NewBool creates a new BasicLit with a bool value without position.
// Useful for ASTs generated by code other than the CUE parser.
func NewBool(b bool) *BasicLit {
x := &BasicLit{}
if b {
x.Kind = token.TRUE
x.Value = "true"
} else {
x.Kind = token.FALSE
x.Value = "false"
}
return x
}

// TODO:
// - use CUE-specific quoting (hoist functionality in export)
// - NewBytes
Expand Down
2 changes: 1 addition & 1 deletion cue/ast/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestLabelName(t *testing.T) {
isIdent: false,
err: true,
}, {
in: &ast.BasicLit{Kind: token.TRUE, Value: "true"},
in: ast.NewBool(true),
out: "true",
isIdent: true,
}, {
Expand Down

0 comments on commit e2650e0

Please sign in to comment.