-
Notifications
You must be signed in to change notification settings - Fork 19
/
resolver.go
155 lines (129 loc) · 3.21 KB
/
resolver.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
package utils
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var snakedChars = regexp.MustCompile(`[^\p{L}\d_]+`)
// Snakify turns the passed in string into a context reference. We replace all whitespace
// characters with _ and replace any duplicate underscores
func Snakify(text string) string {
return strings.Trim(strings.ToLower(snakedChars.ReplaceAllString(text, "_")), "_")
}
// VariableResolver defines the interface used by Excellent objects that can be indexed into
type VariableResolver interface {
Resolve(key string) interface{}
Default() interface{}
String() string
}
// ResolveVariable will resolve the passed in string variable given in dot notation and return
// the value as defined by the VariableResolver passed in.
//
// Example syntaxes:
// foo.bar.0 - 0th element of bar slice within foo, could also be "0" key in bar map within foo
// foo.bar[0] - same as above
func ResolveVariable(env Environment, variable interface{}, key string) interface{} {
var err error
err, isErr := variable.(error)
if isErr {
return err
}
// self referencing
if key == "" {
return variable
}
// strip leading '.'
if key[0] == '.' {
key = key[1:]
}
rest := key
for rest != "" && !IsNil(variable) {
key, rest = popNextVariable(rest)
resolver, isResolver := variable.(VariableResolver)
// look it up in our resolver
if isResolver {
variable = resolver.Resolve(key)
err, isErr := variable.(error)
if isErr {
return err
}
continue
}
// we are a slice
if IsSlice(variable) {
idx, err := strconv.Atoi(key)
if err != nil {
return err
}
variable, err = LookupIndex(variable, idx)
if err != nil {
return err
}
continue
}
// we are a map
if IsMap(variable) {
variable, err = LookupKey(variable, key)
if err != nil {
return err
}
continue
}
variable = fmt.Sprintf("%s.%s", variable, key)
}
return variable
}
// popNextVariable pops the next variable off our string:
// foo.bar.baz -> "foo", "bar.baz"
// foo[0].bar -> "foo", "[0].baz"
// foo.0.bar -> "foo", "0.baz"
// [0].bar -> "0", "bar"
// foo["my key"] -> "foo", "my key"
func popNextVariable(input string) (string, string) {
var keyStart = 0
var keyEnd = -1
var restStart = -1
for i, c := range input {
if i == 0 && c == '[' {
keyStart++
} else if c == '[' {
keyEnd = i
restStart = i
break
} else if c == ']' {
keyEnd = i
restStart = i + 1
break
} else if c == '.' {
keyEnd = i
restStart = i + 1
break
}
}
if keyEnd == -1 {
return input, ""
}
key := strings.Trim(input[keyStart:keyEnd], "\"")
rest := input[restStart:]
return key, rest
}
type mapResolver struct {
values map[string]interface{}
}
// NewMapResolver returns a simple resolver that resolves variables according to the values
// passed in
func NewMapResolver(values map[string]interface{}) VariableResolver {
return &mapResolver{
values: values,
}
}
func (r *mapResolver) Resolve(key string) interface{} {
val, found := r.values[key]
if !found {
return fmt.Errorf("No key '%s' in map", key)
}
return val
}
func (r *mapResolver) Default() interface{} { return r }
func (r *mapResolver) String() string { return fmt.Sprintf("%s", r.values) }