-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
203 lines (166 loc) · 3.71 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
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
package di
import (
"context"
"fmt"
"sync"
)
type Variables interface {
Get(key string) interface{}
GetInt(key string) int
GetString(key string) string
GetBool(key string) bool
GetFloat(key string) float64
TryGet(key string, defVal interface{}) interface{}
TryGetInt(key string, defVal int) int
TryGetString(key string, defVal string) string
TryGetBool(key string, defVal bool) bool
TryGetFloat(key string, defVal float64) float64
Set(key string, val interface{})
SetInt(key string, val int)
SetString(key string, val string)
SetBool(key string, val bool)
SetFloat(key string, val float64)
}
type variables struct {
sync.RWMutex
values map[string]interface{}
}
func (v *variables) Get(key string) interface{} {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
return val
}
panic(fmt.Errorf("Variable not found: %s", key))
}
func (v *variables) GetInt(key string) int {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(int); ok {
return v
}
panic(fmt.Errorf("Type mismatch for variable: %s", key))
}
panic(fmt.Errorf("Variable not found: %s", key))
}
func (v *variables) GetString(key string) string {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(string); ok {
return v
}
panic(fmt.Errorf("Type mismatch for variable: %s", key))
}
panic(fmt.Errorf("Variable not found: %s", key))
}
func (v *variables) GetBool(key string) bool {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(bool); ok {
return v
}
panic(fmt.Errorf("Type mismatch for variable: %s", key))
}
panic(fmt.Errorf("Variable not found: %s", key))
}
func (v *variables) GetFloat(key string) float64 {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(float64); ok {
return v
}
panic(fmt.Errorf("Type mismatch for variable: %s", key))
}
panic(fmt.Errorf("Variable not found: %s", key))
}
func (v *variables) TryGet(key string, defVal interface{}) interface{} {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(int); ok {
return v
}
}
return defVal
}
func (v *variables) TryGetInt(key string, defVal int) int {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(int); ok {
return v
}
}
return defVal
}
func (v *variables) TryGetString(key string, defVal string) string {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(string); ok {
return v
}
}
return defVal
}
func (v *variables) TryGetBool(key string, defVal bool) bool {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(bool); ok {
return v
}
}
return defVal
}
func (v *variables) TryGetFloat(key string, defVal float64) float64 {
v.RLock()
defer v.RUnlock()
if val, ok := v.values[key]; ok {
if v, ok := val.(float64); ok {
return v
}
}
return defVal
}
func (v *variables) Set(key string, val interface{}) {
v.Lock()
defer v.Unlock()
v.values[key] = val
}
func (v *variables) SetInt(key string, val int) {
v.Lock()
defer v.Unlock()
v.values[key] = val
}
func (v *variables) SetString(key string, val string) {
v.Lock()
defer v.Unlock()
v.values[key] = val
}
func (v *variables) SetBool(key string, val bool) {
v.Lock()
defer v.Unlock()
v.values[key] = val
}
func (v *variables) SetFloat(key string, val float64) {
v.Lock()
defer v.Unlock()
v.values[key] = val
}
func NewVariables(values map[string]interface{}) Variables {
if values == nil {
values = make(map[string]interface{})
}
return &variables{
values: values,
}
}
func GetVariables(ctx context.Context) Variables {
app := GetAppFromContext(ctx)
return app.Variables()
}