summaryrefslogtreecommitdiff
path: root/static_expr.go
diff options
context:
space:
mode:
authorMartin Atkins <mart@degeneration.co.uk>2019-09-09 16:08:19 -0700
committerMartin Atkins <mart@degeneration.co.uk>2019-09-09 16:08:19 -0700
commit6c4344623b6ac528a57f9b80e4622acfab2fde40 (patch)
tree83031084d3ab54abbe40e3fd749e439c8b28e9d1 /static_expr.go
parent0f5ab3bd563c111077917020c0cbc8c211e1bff3 (diff)
Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than having to pick it out from dozens of other package directories.
Diffstat (limited to 'static_expr.go')
-rw-r--r--static_expr.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/static_expr.go b/static_expr.go
new file mode 100644
index 0000000..98ada87
--- /dev/null
+++ b/static_expr.go
@@ -0,0 +1,40 @@
+package hcl
+
+import (
+ "github.com/zclconf/go-cty/cty"
+)
+
+type staticExpr struct {
+ val cty.Value
+ rng Range
+}
+
+// StaticExpr returns an Expression that always evaluates to the given value.
+//
+// This is useful to substitute default values for expressions that are
+// not explicitly given in configuration and thus would otherwise have no
+// Expression to return.
+//
+// Since expressions are expected to have a source range, the caller must
+// provide one. Ideally this should be a real source range, but it can
+// be a synthetic one (with an empty-string filename) if no suitable range
+// is available.
+func StaticExpr(val cty.Value, rng Range) Expression {
+ return staticExpr{val, rng}
+}
+
+func (e staticExpr) Value(ctx *EvalContext) (cty.Value, Diagnostics) {
+ return e.val, nil
+}
+
+func (e staticExpr) Variables() []Traversal {
+ return nil
+}
+
+func (e staticExpr) Range() Range {
+ return e.rng
+}
+
+func (e staticExpr) StartRange() Range {
+ return e.rng
+}