-
Notifications
You must be signed in to change notification settings - Fork 15
/
functions.go
328 lines (269 loc) · 8.71 KB
/
functions.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package functions
import (
"crypto/rand"
"encoding/hex"
"regexp"
"strings"
"github.com/jexia/semaphore/pkg/broker"
"github.com/jexia/semaphore/pkg/broker/logger"
"github.com/jexia/semaphore/pkg/references"
"github.com/jexia/semaphore/pkg/specs"
"github.com/jexia/semaphore/pkg/specs/template"
"go.uber.org/zap"
)
// Custom represents a collection of custom defined functions that could be called inside a template
type Custom map[string]Intermediate
// Intermediate prepares the custom defined function.
// The given arguments represent the exprected types that are passed when called.
// Properties returned should be absolute.
type Intermediate func(args ...*specs.Property) (*specs.Property, Exec, error)
// Exec is a executable function.
// A store should be returned which could be used to encode the function property
type Exec func(store references.Store) error
// Collection represents a collection of stacks grouped by nodes
type Collection map[*specs.ParameterMap]Stack
// Reserve reserves a new function stack for the given node.
// If a stack already exists for the given node is it returned.
func (collection Collection) Reserve(params *specs.ParameterMap) Stack {
stack, has := collection[params]
if has {
return stack
}
collection[params] = Stack{}
return collection[params]
}
// Load attempts to load the function stack for the given parameter map
func (collection Collection) Load(params *specs.ParameterMap) Stack {
return collection[params]
}
// Stack represents a collection of functions
type Stack map[string]*Function
// Function represents a custom defined function
type Function struct {
Arguments []*specs.Property
Fn Exec
Returns *specs.Property
}
var (
// FunctionPattern is the matching pattern for custom defined functions
FunctionPattern = regexp.MustCompile(`(\w+)\((.*)\)$`)
)
const (
// ArgumentDelimiter represents the character delimiting function arguments
ArgumentDelimiter = ","
)
// PrepareFunctions prepares all function definitions inside the given flows
func PrepareFunctions(ctx *broker.Context, mem Collection, functions Custom, flows specs.FlowListInterface) (err error) {
logger.Info(ctx, "preparing manifest functions")
for _, flow := range flows {
err := PrepareFlowFunctions(logger.WithFields(ctx, zap.String("flow", flow.GetName())), mem, functions, flow)
if err != nil {
return err
}
}
return nil
}
// PrepareFlowFunctions prepares the functions definitions inside the given flow
func PrepareFlowFunctions(ctx *broker.Context, mem Collection, functions Custom, flow specs.FlowInterface) (err error) {
logger.Info(ctx, "comparing flow functions")
for _, node := range flow.GetNodes() {
err = PrepareNodeFunctions(ctx, mem, functions, flow, node)
if err != nil {
return err
}
}
if flow.GetOutput() != nil {
stack := mem.Reserve(flow.GetOutput())
err := PrepareParameterMapFunctions(ctx, nil, flow, stack, flow.GetOutput(), functions)
if err != nil {
return err
}
}
if flow.GetForward() != nil {
if flow.GetForward().Request != nil && flow.GetForward().Request.Header != nil {
stack := mem.Reserve(flow.GetForward().Request)
err = PrepareHeaderFunctions(ctx, flow, stack, flow.GetForward().Request.Header, functions)
if err != nil {
return err
}
}
}
return nil
}
// PrepareNodeFunctions prepares the available functions within the given node
func PrepareNodeFunctions(ctx *broker.Context, mem Collection, functions Custom, flow specs.FlowInterface, node *specs.Node) (err error) {
if node.Intermediate != nil {
stack := mem.Reserve(node.Intermediate)
err = PrepareParameterMapFunctions(ctx, node, flow, stack, node.Intermediate, functions)
if err != nil {
return err
}
}
if node.Condition != nil {
stack := mem.Reserve(node.Condition.Params)
err = PrepareParameterMapFunctions(ctx, node, flow, stack, node.Condition.Params, functions)
if err != nil {
return err
}
}
if node.Call != nil {
err = PrepareCallFunctions(ctx, node, flow, mem, functions, node.Call)
if err != nil {
return err
}
}
if node.Rollback != nil {
err = PrepareCallFunctions(ctx, node, flow, mem, functions, node.Rollback)
if err != nil {
return err
}
}
return nil
}
// PrepareCallFunctions prepares the function definitions inside the given flow
func PrepareCallFunctions(ctx *broker.Context, node *specs.Node, flow specs.FlowInterface, mem Collection, functions Custom, call *specs.Call) error {
if call.Request != nil {
stack := mem.Reserve(call.Request)
err := PrepareParameterMapFunctions(ctx, node, flow, stack, call.Request, functions)
if err != nil {
return err
}
}
if call.Response != nil {
stack := mem.Reserve(call.Response)
err := PrepareParameterMapFunctions(ctx, node, flow, stack, call.Response, functions)
if err != nil {
return err
}
}
return nil
}
// PrepareParameterMapFunctions prepares the function definitions inside the given parameter map
func PrepareParameterMapFunctions(ctx *broker.Context, node *specs.Node, flow specs.FlowInterface, stack Stack, params *specs.ParameterMap, functions Custom) error {
if params.Header != nil {
err := PrepareHeaderFunctions(ctx, flow, stack, params.Header, functions)
if err != nil {
return err
}
}
if params.Params != nil {
err := PrepareParamsFunctions(ctx, node, flow, stack, params.Params, functions)
if err != nil {
return err
}
}
if params.Property != nil {
err := PreparePropertyFunctions(ctx, node, flow, stack, params.Property, functions)
if err != nil {
return err
}
}
params.Stack = make(map[string]*specs.Property, len(stack))
for ref, fn := range stack {
params.Stack[ref] = fn.Returns
}
return nil
}
// PrepareHeaderFunctions prepares the function definitions inside the given header
func PrepareHeaderFunctions(ctx *broker.Context, flow specs.FlowInterface, stack Stack, header specs.Header, functions Custom) error {
for _, prop := range header {
err := PrepareFunction(ctx, nil, flow, prop, stack, functions)
if err != nil {
return err
}
}
return nil
}
// PrepareParamsFunctions prepares the function definitions inside the given property
func PrepareParamsFunctions(ctx *broker.Context, node *specs.Node, flow specs.FlowInterface, stack Stack, params map[string]*specs.Property, functions Custom) error {
if params == nil {
return nil
}
for _, param := range params {
err := PrepareFunction(ctx, node, flow, param, stack, functions)
if err != nil {
return err
}
}
return nil
}
// PreparePropertyFunctions prepares the function definitions inside the given property
func PreparePropertyFunctions(ctx *broker.Context, node *specs.Node, flow specs.FlowInterface, stack Stack, prop *specs.Property, functions Custom) error {
if prop == nil {
return nil
}
if prop.Message != nil {
for _, nested := range prop.Message {
err := PreparePropertyFunctions(ctx, node, flow, stack, nested, functions)
if err != nil {
return err
}
}
}
err := PrepareFunction(ctx, node, flow, prop, stack, functions)
if err != nil {
return err
}
return nil
}
// PrepareFunction attempts to parses the given function
func PrepareFunction(ctx *broker.Context, node *specs.Node, flow specs.FlowInterface, property *specs.Property, stack Stack, methods Custom) error {
if property == nil {
return nil
}
if !FunctionPattern.MatchString(property.Raw) {
return nil
}
pattern := FunctionPattern.FindStringSubmatch(property.Raw)
fn := pattern[1]
args := strings.Split(pattern[2], ArgumentDelimiter)
if methods[fn] == nil {
return ErrUndefinedFunction{
Function: fn,
Property: property.Raw,
}
}
arguments := make([]*specs.Property, len(args))
for index, arg := range args {
result, err := template.ParseContent(property.Path, property.Name, strings.TrimSpace(arg))
if err != nil {
return err
}
err = references.ResolveProperty(ctx, node, result, flow)
if err != nil {
return err
}
err = PrepareFunction(ctx, node, flow, result, stack, methods)
if err != nil {
return err
}
arguments[index] = result
}
returns, handle, err := methods[fn](arguments...)
if err != nil {
return err
}
ref := GenerateStackReference()
function := &Function{
Arguments: arguments,
Fn: handle,
Returns: property,
}
stack[ref] = function
property.Template = returns.Template
property.Label = returns.Label
resource := template.JoinPath(template.StackResource, ref)
property.Reference = &specs.PropertyReference{
Resource: resource,
Path: ".",
Property: returns,
}
references.ScopeNestedReferences(resource, "", &returns.Template, &property.Template)
return nil
}
// GenerateStackReference generates a unique path prefix which could be used to isolate functions
func GenerateStackReference() string {
bb := make([]byte, 5)
rand.Read(bb)
return hex.EncodeToString(bb)
}