summaryrefslogtreecommitdiff
path: root/expr_list.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 /expr_list.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 'expr_list.go')
-rw-r--r--expr_list.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/expr_list.go b/expr_list.go
new file mode 100644
index 0000000..d05cca0
--- /dev/null
+++ b/expr_list.go
@@ -0,0 +1,37 @@
+package hcl
+
+// ExprList tests if the given expression is a static list construct and,
+// if so, extracts the expressions that represent the list elements.
+// If the given expression is not a static list, error diagnostics are
+// returned.
+//
+// A particular Expression implementation can support this function by
+// offering a method called ExprList that takes no arguments and returns
+// []Expression. This method should return nil if a static list cannot
+// be extracted. Alternatively, an implementation can support
+// UnwrapExpression to delegate handling of this function to a wrapped
+// Expression object.
+func ExprList(expr Expression) ([]Expression, Diagnostics) {
+ type exprList interface {
+ ExprList() []Expression
+ }
+
+ physExpr := UnwrapExpressionUntil(expr, func(expr Expression) bool {
+ _, supported := expr.(exprList)
+ return supported
+ })
+
+ if exL, supported := physExpr.(exprList); supported {
+ if list := exL.ExprList(); list != nil {
+ return list, nil
+ }
+ }
+ return nil, Diagnostics{
+ &Diagnostic{
+ Severity: DiagError,
+ Summary: "Invalid expression",
+ Detail: "A static list expression is required.",
+ Subject: expr.StartRange().Ptr(),
+ },
+ }
+}