/
invocation_foreach.go
143 lines (112 loc) · 3.47 KB
/
invocation_foreach.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package models
import (
"github.com/v-braun/ratt/pkg/utils"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/hcl/v2"
"github.com/v-braun/ratt/pkg/types"
)
var _ types.Invocation = &invocationForEach{}
type invocationForEach struct {
readModel *hcl.Block
model types.RattModel
}
func BuildForEachInvocationModel(block *hcl.Block, model types.RattModel, ctx types.RattContext) types.Invocation {
result := &invocationForEach{
readModel: block,
model: model,
}
return result
}
func (ir *invocationForEach) Exec(ctx types.RattContext) {
subCtx := ctx.NewChild("for_each")
ctx.Bag()[types.IdBagEntry] = subCtx.Id()
subCtx.Reporter().Invocation(ir, types.BeginState, ctx)
body := utils.ExtractContent(ir.readModel.Body, ForEachSchema(), ctx)
if ctx.Reporter().HasErrors() {
subCtx.Reporter().Invocation(ir, types.FailedState, ctx)
return
}
// nested, readModel := BuildInvocationBlockBody(block, model, ForEachSchema(), ctx)
// if ctx.Reporter().HasErrors() {
// return nil
// }
itemsAttr, found := utils.GetOptionalAttr("items", body, subCtx)
if ctx.Reporter().HasErrors() {
subCtx.Reporter().Invocation(ir, types.FailedState, ctx)
return
}
if !found {
ctx.Reporter().ErrorWithRange("attribute 'items' is missing", ir.DefRange())
subCtx.Reporter().Invocation(ir, types.FailedState, ctx)
return
}
if !itemsAttr.Type().IsObjectType() {
ctx.Reporter().ErrorWithRange("attribute 'items' should be an object", ir.DefRange())
subCtx.Reporter().Invocation(ir, types.FailedState, ctx)
return
}
items := itemsAttr.AsValueMap()
ctx.Bag()[types.ItemsLengthBagEntry] = len(items)
for k, v := range items {
nested, _ := BuildInvocationBlockBody(ir.readModel, ir.model, ForEachSchema(), ctx)
if ctx.Reporter().HasErrors() {
subCtx.Reporter().Invocation(ir, types.FailedState, ctx)
return
}
step := &invocationForEachStep{
nested: nested,
bodyModel: body,
name: k,
values: v,
}
step.Exec(subCtx)
}
subCtx.Reporter().Invocation(ir, types.EndState, ctx)
// subCtx.Reporter().Invocation(ir, types.RunningState, bag)
// start := time.Now()
// for _, invocation := range ir.nested {
// invocation.Exec(subCtx)
// }
// elapsed := time.Since(start)
// bag[types.DurationBagEntry] = elapsed.Milliseconds()
// subCtx.Reporter().Invocation(ir, types.EndState, bag)
}
func (ir *invocationForEach) Name() string {
return "for_each"
}
func (ir *invocationForEach) Type() types.InvocationType {
return types.ForEachInvocationType
}
func (ir *invocationForEach) DefRange() *hcl.Range {
return &ir.readModel.DefRange
}
var _ types.Invocation = &invocationForEachStep{}
type invocationForEachStep struct {
nested []types.Invocation
bodyModel *hcl.BodyContent
name string
values cty.Value
}
func (ir *invocationForEachStep) Name() string {
return ir.name
}
func (ir *invocationForEachStep) Type() types.InvocationType {
return types.ForEachStepInvocationType
}
func (ir *invocationForEachStep) DefRange() *hcl.Range {
return &ir.bodyModel.MissingItemRange
}
func (ir *invocationForEachStep) Exec(ctx types.RattContext) {
subCtx := ctx.NewChild(ir.name)
ctx.Bag()[types.IdBagEntry] = subCtx.Id()
subCtx.Reporter().Invocation(ir, types.BeginState, ctx)
eval := subCtx.Eval()
eval.Variables["each"] = cty.ObjectVal(map[string]cty.Value{
"key": cty.StringVal(ir.name),
"value": ir.values,
})
for _, nested := range ir.nested {
nested.Exec(subCtx)
}
subCtx.Reporter().Invocation(ir, types.EndState, ctx)
}