-
Notifications
You must be signed in to change notification settings - Fork 3
/
call.go
198 lines (163 loc) · 4.5 KB
/
call.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
package skipper
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"regexp"
"strconv"
"strings"
)
var (
// callRegex matches everything between `%{....}`
callRegex = regexp.MustCompile(`\%\{(.+)\}`)
// callActionRegex matches the actual call syntax `function:param`
callActionRegex = regexp.MustCompile(`(\w+)(\:(.+))?`)
callFuncMap = map[string]CallFunc{
"env": func(param string) string {
out := os.Getenv(param)
if len(out) == 0 {
return "UNDEFINED"
}
return out
},
"randomstring": func(param string) string {
const defaultLength = 32
var length int
if param == "" {
length = defaultLength
}
length, err := strconv.Atoi(param)
if err != nil {
length = defaultLength
}
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"
ret := make([]byte, length)
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return err.Error()
}
ret[i] = letters[num.Int64()]
}
return string(ret)
},
"loweralpha": func(param string) string {
reg, err := regexp.Compile("[^a-z0-9]+")
if err != nil {
return err.Error()
}
return reg.ReplaceAllString(strings.ToLower(param), "")
},
}
ErrEmptyFunctionName error = fmt.Errorf("empty function name")
)
type CallFunc func(param string) string
type Call struct {
// Identifier points to wherever the call is used in the [Data] map
Identifier []interface{}
FunctionName string
Param string
callback CallFunc
}
func NewCall(functionName string, param string, path []interface{}) (*Call, error) {
if functionName == "" {
return nil, ErrEmptyFunctionName
}
if !validCallFunc(functionName) {
return nil, fmt.Errorf("invalid call function '%s'", functionName)
}
return &Call{
Identifier: path,
FunctionName: functionName,
Param: param,
callback: callFuncMap[strings.ToLower(functionName)],
}, nil
}
func NewRawCall(callString string) (*Call, bool, error) {
// now we can use the second regex to extract the desired parts of the call
segments := callActionRegex.FindAllStringSubmatch(callString, -1)
// if len of the matches is not at least 1, we did not match and can continue
for _, call := range segments {
function := call[1]
param := call[3]
if !validCallFunc(function) {
return nil, false, fmt.Errorf("invalid call function '%s'", function)
}
return &Call{
Identifier: nil,
FunctionName: function,
Param: param,
callback: callFuncMap[strings.ToLower(function)],
}, true, nil
}
return nil, false, nil
}
func (c *Call) RawString() string {
if len(c.Param) == 0 {
return c.FunctionName
}
return fmt.Sprintf("%s:%s", c.FunctionName, c.Param)
}
func (c *Call) Execute() string {
return c.callback(c.Param)
}
func FindCalls(data Data) ([]*Call, error) {
var foundValues []interface{}
err := data.FindValues(findCallFunc(), &foundValues)
if err != nil {
return nil, err
}
var foundCalls []*Call
for _, val := range foundValues {
calls, ok := val.([]*Call)
if !ok {
return nil, fmt.Errorf("unexpected error during call detection, file a bug report")
}
foundCalls = append(foundCalls, calls...)
}
return foundCalls, nil
}
func findCallFunc() FindValueFunc {
return func(value string, path []interface{}) (interface{}, error) {
var calls []*Call
matches := callRegex.FindAllStringSubmatch(value, -1)
for _, match := range matches {
// matches should be a slice with two values. we're interested in the second
if len(match[0]) > 1 {
// now we can use the second regex to extract the desired parts of the call
segments := callActionRegex.FindAllStringSubmatch(match[1], -1)
// if len of the matches is not at least 1, we did not match and can continue
for _, call := range segments {
function := call[1]
param := call[3]
newFunc, err := NewCall(function, param, path)
if err != nil {
return nil, err
}
calls = append(calls, newFunc)
}
}
}
return calls, nil
}
}
func (c Call) FullName() string {
if len(c.Param) == 0 {
return "%" + fmt.Sprintf("{%s}", c.FunctionName)
}
return "%" + fmt.Sprintf("{%s:%s}", c.FunctionName, c.Param)
}
func (c Call) Path() string {
var segments []string
for _, seg := range c.Identifier {
segments = append(segments, fmt.Sprint(seg))
}
return strings.Join(segments, ".")
}
func validCallFunc(funcName string) bool {
if _, exists := callFuncMap[strings.ToLower(funcName)]; exists {
return true
}
return false
}