-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.go
68 lines (54 loc) · 1.65 KB
/
variables.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
package factory
import (
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)
type Scope string
const (
GlobalScope Scope = "global"
StageScope Scope = "stage"
)
/*
Current issues with variables. Resolving the variables such as var.foo or resolving objects var.foo.bar
Scopes, there is only 1 global scope but can be many module and resource scopes.
*/
type Variables struct {
GlobalVariables map[string]cty.Value
StageVariables map[string]map[string]cty.Value
}
func NewVariables() *Variables {
return &Variables{
GlobalVariables: make(map[string]cty.Value),
StageVariables: make(map[string]map[string]cty.Value),
}
}
func (v *Variables) InsertStage(key string, value *cty.Value, scopeID string) {
if _, ok := v.StageVariables[scopeID]; !ok {
// Scope not found, create the scope
v.StageVariables[scopeID] = make(map[string]cty.Value)
}
v.StageVariables[scopeID][key] = *value
}
func (v *Variables) InsertGlobal(key string, value *cty.Value) {
v.GlobalVariables[key] = *value
}
func decodeGlobalVariableBlock(block *hcl.Block, file *File) hcl.Diagnostics {
vars, diags := block.Body.JustAttributes()
for _, attr := range vars {
name := attr.Name
value, d := attr.Expr.Value(file.GetEvalContext(nil))
diags = append(diags, d...)
file.Variables.InsertGlobal(name, &value)
}
return diags
}
func decodeVariableBlock(block *hcl.Block, file *File, scopeID string) hcl.Diagnostics {
vars, diags := block.Body.JustAttributes()
for _, attr := range vars {
name := attr.Name
value, d := attr.Expr.Value(file.GetEvalContext(&scopeID))
diags = append(diags, d...)
file.Variables.InsertStage(name, &value, scopeID)
}
return diags
}