forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_vars.go
54 lines (40 loc) · 1.11 KB
/
static_vars.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
package template
import (
"strings"
)
type StaticVariables map[string]interface{}
var _ Variables = StaticVariables{}
func (v StaticVariables) Get(varDef VariableDefinition) (interface{}, bool, error) {
val, found := v.processed()[varDef.Name]
return val, found, nil
}
func (v StaticVariables) List() ([]VariableDefinition, error) {
var defs []VariableDefinition
for name, _ := range v.processed() {
defs = append(defs, VariableDefinition{Name: name})
}
return defs, nil
}
func (v StaticVariables) processed() map[string]interface{} {
processed := map[interface{}]interface{}{}
for name, val := range v {
pieces := strings.Split(name, ".")
if len(pieces) == 1 {
processed[name] = val
} else {
mapRef := processed
for _, p := range pieces[0 : len(pieces)-1] {
if _, found := processed[p]; !found {
mapRef[p] = map[interface{}]interface{}{}
}
mapRef = mapRef[p].(map[interface{}]interface{})
}
mapRef[pieces[len(pieces)-1]] = val
}
}
processedTyped := map[string]interface{}{}
for k, v := range processed {
processedTyped[k.(string)] = v
}
return processedTyped
}