-
Notifications
You must be signed in to change notification settings - Fork 31
/
scope.go
210 lines (163 loc) · 3.15 KB
/
scope.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package tachyon
import (
"encoding/json"
"fmt"
)
type Value interface {
Read() interface{}
}
type AnyValue struct {
v interface{}
}
func (a AnyValue) Read() interface{} {
return a.v
}
type AnyMap struct {
m map[interface{}]interface{}
}
func (a AnyMap) Read() interface{} {
return a.m
}
func (a AnyMap) Get(key string) (Value, bool) {
if v, ok := a.m[key]; ok {
return Any(v), true
}
return nil, false
}
type StrMap struct {
m map[string]interface{}
}
func (a StrMap) Get(key string) (Value, bool) {
if v, ok := a.m[key]; ok {
return Any(v), true
}
return nil, false
}
func (a StrMap) Read() interface{} {
return a.m
}
func (a AnyValue) MarshalJSON() ([]byte, error) {
return json.Marshal(a.v)
}
func (a AnyMap) MarshalJSON() ([]byte, error) {
return json.Marshal(a.m)
}
func (a StrMap) MarshalJSON() ([]byte, error) {
return json.Marshal(a.m)
}
func Any(v interface{}) Value {
switch sv := v.(type) {
case AnyValue:
return sv
case map[interface{}]interface{}:
return AnyMap{sv}
case map[string]interface{}:
return StrMap{sv}
default:
return AnyValue{v}
}
}
func (a AnyValue) GetYAML() (string, interface{}) {
return "", a.v
}
func (a AnyValue) SetYAML(tag string, v interface{}) bool {
a.v = v
return true
}
type Map interface {
Get(key string) (Value, bool)
}
type Scope interface {
Get(key string) (Value, bool)
Set(key string, val interface{})
}
type ScopeGetter interface {
Get(key string) (Value, bool)
}
func SV(v interface{}, ok bool) interface{} {
if !ok {
return nil
}
return v
}
type NestedScope struct {
Scope Scope
Vars Vars
}
func NewNestedScope(parent Scope) *NestedScope {
return &NestedScope{parent, make(Vars)}
}
func SpliceOverrides(cur Scope, override *NestedScope) *NestedScope {
ns := NewNestedScope(cur)
for k, v := range override.Vars {
ns.Set(k, v)
}
return ns
}
func (n *NestedScope) Get(key string) (v Value, ok bool) {
v, ok = n.Vars[key]
if !ok && n.Scope != nil {
v, ok = n.Scope.Get(key)
}
return
}
func (n *NestedScope) Set(key string, v interface{}) {
n.Vars[key] = Any(v)
}
func (n *NestedScope) Empty() bool {
return len(n.Vars) == 0
}
func (n *NestedScope) Flatten() Scope {
if len(n.Vars) == 0 && n.Scope != nil {
return n.Scope
}
return n
}
func (n *NestedScope) addMapVars(mv map[interface{}]interface{}) error {
for k, v := range mv {
if sk, ok := k.(string); ok {
if sv, ok := v.(string); ok {
var err error
v, err = ExpandVars(n, sv)
if err != nil {
return err
}
}
n.Set(sk, v)
}
}
return nil
}
func (n *NestedScope) addVars(vars interface{}) (err error) {
switch mv := vars.(type) {
case map[interface{}]interface{}:
err = n.addMapVars(mv)
case []interface{}:
for _, i := range mv {
err = n.addVars(i)
if err != nil {
return
}
}
}
return
}
func ImportVarsFile(s Scope, path string) error {
var fv map[string]string
err := yamlFile(path, &fv)
if err != nil {
return err
}
for k, v := range fv {
s.Set(k, inferString(v))
}
return nil
}
func DisplayScope(s Scope) {
if ns, ok := s.(*NestedScope); ok {
DisplayScope(ns.Scope)
for k, v := range ns.Vars {
fmt.Printf("%s: %v\n", k, v)
}
}
}