forked from project-flogo/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
275 lines (228 loc) · 6.76 KB
/
resolve.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package resolve
import (
"fmt"
"strings"
"github.com/qingcloudhx/core/data"
)
// Resolver is for resolving a value for a specific environment or construct, ex. OS environment
type Resolver interface {
// GetResolverInfo returns ResolverInfo which contains information about the resolver
GetResolverInfo() *ResolverInfo
// Resolve resolves to a value using the specified item and valueName, note: the scope might not be used
Resolve(scope data.Scope, itemName, valueName string) (interface{}, error)
}
// CompositeResolver is a resolver that is typically composed of other simple 'Resolvers',
// the specified directive is dispatched to the appropriate embedded resolvers
type CompositeResolver interface {
// GetResolution creates a "delayed" resolution object, who's value isn't fully resolved
// util GetValue is called
GetResolution(resolveDirective string) (Resolution, error)
// Resolve resolves to a value using the specified directive
Resolve(resolveDirective string, scope data.Scope) (value interface{}, err error)
}
// Resolution structure that is allows for delayed resolving of values, the value can then be fully
// resolved calling GetValue for a particular scope
type Resolution interface {
// IsStatic indicates that resolution can be done statically without a scope
IsStatic() bool
// GetValue resolves and returns the value using the specified scope
GetValue(scope data.Scope) (interface{}, error)
}
// NewResolverInfo creates a ResolverInfo object
func NewResolverInfo(isStatic, usesItemFormat bool) *ResolverInfo {
return &ResolverInfo{isStatic: isStatic, usesItemFormat: usesItemFormat}
}
func NewImplicitResolverInfo(isStatic, isImplicit bool) *ResolverInfo {
return &ResolverInfo{isStatic: isStatic, isImplicit: isImplicit}
}
// ResolverInfo structure that contains information about the resolver
type ResolverInfo struct {
usesItemFormat bool
isStatic bool
isImplicit bool
}
// IsStatic determines if the resolver's values are static and can be resolved immediately without a scope
func (i *ResolverInfo) IsStatic() bool {
return i.isStatic
}
// UsesItemFormat determines if the resolver uses the item format (ex. $test[itemName])
func (i *ResolverInfo) UsesItemFormat() bool {
return i.usesItemFormat
}
// IsImplicit determines if the resolver try to uses the item format and no item format then
func (i *ResolverInfo) IsImplicit() bool {
return i.isImplicit
}
// GetResolverInfo gets the resolver name and position to start parsing the ResolutionDetails from
func GetResolverInfo(toResolve string) (string, int) {
if toResolve[0] == '.' {
return ".", 1
}
for i, char := range toResolve {
if char == '.' || char == '[' {
return toResolve[0:i], i
}
}
return toResolve, len(toResolve)
}
// ResolveDirectiveDetails is the Resolve Directive broken into components to assist in resolving the value
type ResolveDirectiveDetails struct {
ItemName string
ValueName string
Path string
}
// GetResolveDirectiveDetails breaks Resolution Directive into components
func GetResolveDirectiveDetails(directive string, hasItems, isImplicit bool) (*ResolveDirectiveDetails, error) {
//todo optimize
details := &ResolveDirectiveDetails{}
start := 0
strLen := len(directive)
hasNamedValue := true
//isImplicit will try to support both ithem or without item
if isImplicit || hasItems {
//uses the "item format" (ex. foo[bar].valueName; where 'bar' is the item)
if directive[0] != '[' {
if hasItems {
return nil, fmt.Errorf("invalid resolve directive: '%s' needs to start with [item]", directive)
}
if isImplicit {
hasItems = false
hasNamedValue = true
}
} else {
hasItems = true
hasNamedValue = false
}
if hasItems {
start = 1
for i := 1; i < strLen; i++ {
if directive[i] == ']' {
details.ItemName = removeQuotes(directive[start:i])
start = i + 1
//if we started with an item, it must either end or the next segment should start with '.' or '['
if start < strLen {
if directive[start] != '.' && directive[start] != '[' {
return nil, fmt.Errorf("invalid resolve directive: '%s'", directive)
}
if directive[start] == '.' {
hasNamedValue = true
start++
}
}
break
}
}
}
}
var i int
if hasNamedValue {
if start == 0 && directive[0] == '.' {
start = 1
}
for i = start; i < strLen; i++ {
if directive[i] == '.' || directive[i] == '[' {
details.ValueName = directive[start:i]
start = i
break
}
}
}
if i == strLen {
// we have gotten to the end of the string, so the last part of the string is the ValueName
details.ValueName = directive[start:]
} else if start < strLen {
// we have a remaining component, should be the 'path'
details.Path = directive[start:]
}
return details, nil
}
var ends = map[byte]byte{'(': ')', '"': '"', '\'': '\'', '[': ']', '`': '`'}
func isLetter(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
// IsResolveExpr determines if the provide expression string is a resolver expression, ie. "$env[MY_VAR]
func IsResolveExpr(exprStr string) bool {
strLen := len(exprStr)
if exprStr[0] == '$' && strLen > 2 {
if exprStr[1] != '.' && !isLetter(exprStr[2]) {
return false
}
for i := 1; i < strLen; i++ {
switch c := exprStr[i]; c {
case ' ':
return false
case '"', '\'', '`':
end := ends[c]
i++
for i < len(exprStr) {
if exprStr[i] == end {
break
}
i++
}
case '[':
end := ends[c]
i++
j := i
for i < len(exprStr) {
if exprStr[i] == end {
//Checking whether value insde [] is expr
if hasExprChar(exprStr[j:i]) {
return false
}
break
}
i++
}
case '.':
if i+1 >= strLen || !isLetter(exprStr[i+1]) {
return false
}
default:
if isExprChar(c) {
return false
}
}
}
} else {
return false
}
return true
}
func hasExprChar(str string) bool {
//condition expression, tenray expression, array indexer expression
if len(str) > 0 {
//String
if str[0] == '"' || str[0] == '\'' || str[0] == '`' {
return false
} else {
strLen := len(str)
for i := 0; i < strLen; i++ {
if isExprChar(str[i]) {
return true
}
}
}
}
return false
}
func isExprChar(ch byte) bool {
//condition expression, tenray expression, array indexer expression
switch ch {
case '(', '=', '>', '<', '*', '/', '!', '&', '%', '+', '-', '|', '?', ':', '$':
return true
}
return false
}
func removeQuotes(str string) string {
if strings.HasPrefix(str, `"`) && strings.HasSuffix(str, `"`) {
return str[1 : len(str)-1]
}
if strings.HasPrefix(str, "'") && strings.HasSuffix(str, "'") {
return str[1 : len(str)-1]
}
if strings.HasPrefix(str, "`") && strings.HasSuffix(str, "`") {
return str[1 : len(str)-1]
}
return str
}