aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/ext/dynblock/iteration.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/ext/dynblock/iteration.go')
-rw-r--r--vendor/github.com/hashicorp/hcl2/ext/dynblock/iteration.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/ext/dynblock/iteration.go b/vendor/github.com/hashicorp/hcl2/ext/dynblock/iteration.go
new file mode 100644
index 0000000..7056d33
--- /dev/null
+++ b/vendor/github.com/hashicorp/hcl2/ext/dynblock/iteration.go
@@ -0,0 +1,66 @@
1package dynblock
2
3import (
4 "github.com/hashicorp/hcl2/hcl"
5 "github.com/zclconf/go-cty/cty"
6)
7
8type iteration struct {
9 IteratorName string
10 Key cty.Value
11 Value cty.Value
12 Inherited map[string]*iteration
13}
14
15func (s *expandSpec) MakeIteration(key, value cty.Value) *iteration {
16 return &iteration{
17 IteratorName: s.iteratorName,
18 Key: key,
19 Value: value,
20 Inherited: s.inherited,
21 }
22}
23
24func (i *iteration) Object() cty.Value {
25 return cty.ObjectVal(map[string]cty.Value{
26 "key": i.Key,
27 "value": i.Value,
28 })
29}
30
31func (i *iteration) EvalContext(base *hcl.EvalContext) *hcl.EvalContext {
32 new := base.NewChild()
33
34 if i != nil {
35 new.Variables = map[string]cty.Value{}
36 for name, otherIt := range i.Inherited {
37 new.Variables[name] = otherIt.Object()
38 }
39 new.Variables[i.IteratorName] = i.Object()
40 }
41
42 return new
43}
44
45func (i *iteration) MakeChild(iteratorName string, key, value cty.Value) *iteration {
46 if i == nil {
47 // Create entirely new root iteration, then
48 return &iteration{
49 IteratorName: iteratorName,
50 Key: key,
51 Value: value,
52 }
53 }
54
55 inherited := map[string]*iteration{}
56 for name, otherIt := range i.Inherited {
57 inherited[name] = otherIt
58 }
59 inherited[i.IteratorName] = i
60 return &iteration{
61 IteratorName: iteratorName,
62 Key: key,
63 Value: value,
64 Inherited: inherited,
65 }
66}