cue/ast: add NewBool helper
Change-Id: Iccd88259a08d00e5da589b962a69e675e39a6234
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4460
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/ast/ast.go b/cue/ast/ast.go
index b631456..7d91328 100644
--- a/cue/ast/ast.go
+++ b/cue/ast/ast.go
@@ -423,6 +423,20 @@
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
diff --git a/cue/ast/ident_test.go b/cue/ast/ident_test.go
index a32f451..71ead25 100644
--- a/cue/ast/ident_test.go
+++ b/cue/ast/ident_test.go
@@ -58,7 +58,7 @@
isIdent: false,
err: true,
}, {
- in: &ast.BasicLit{Kind: token.TRUE, Value: "true"},
+ in: ast.NewBool(true),
out: "true",
isIdent: true,
}, {