diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/expr_map.go')
-rw-r--r-- | vendor/github.com/hashicorp/hcl2/hcl/expr_map.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/expr_map.go b/vendor/github.com/hashicorp/hcl2/hcl/expr_map.go new file mode 100644 index 0000000..96d1ce4 --- /dev/null +++ b/vendor/github.com/hashicorp/hcl2/hcl/expr_map.go | |||
@@ -0,0 +1,44 @@ | |||
1 | package hcl | ||
2 | |||
3 | // ExprMap tests if the given expression is a static map construct and, | ||
4 | // if so, extracts the expressions that represent the map elements. | ||
5 | // If the given expression is not a static map, error diagnostics are | ||
6 | // returned. | ||
7 | // | ||
8 | // A particular Expression implementation can support this function by | ||
9 | // offering a method called ExprMap that takes no arguments and returns | ||
10 | // []KeyValuePair. This method should return nil if a static map cannot | ||
11 | // be extracted. Alternatively, an implementation can support | ||
12 | // UnwrapExpression to delegate handling of this function to a wrapped | ||
13 | // Expression object. | ||
14 | func ExprMap(expr Expression) ([]KeyValuePair, Diagnostics) { | ||
15 | type exprMap interface { | ||
16 | ExprMap() []KeyValuePair | ||
17 | } | ||
18 | |||
19 | physExpr := UnwrapExpressionUntil(expr, func(expr Expression) bool { | ||
20 | _, supported := expr.(exprMap) | ||
21 | return supported | ||
22 | }) | ||
23 | |||
24 | if exM, supported := physExpr.(exprMap); supported { | ||
25 | if pairs := exM.ExprMap(); pairs != nil { | ||
26 | return pairs, nil | ||
27 | } | ||
28 | } | ||
29 | return nil, Diagnostics{ | ||
30 | &Diagnostic{ | ||
31 | Severity: DiagError, | ||
32 | Summary: "Invalid expression", | ||
33 | Detail: "A static map expression is required.", | ||
34 | Subject: expr.StartRange().Ptr(), | ||
35 | }, | ||
36 | } | ||
37 | } | ||
38 | |||
39 | // KeyValuePair represents a pair of expressions that serve as a single item | ||
40 | // within a map or object definition construct. | ||
41 | type KeyValuePair struct { | ||
42 | Key Expression | ||
43 | Value Expression | ||
44 | } | ||