-
Notifications
You must be signed in to change notification settings - Fork 19
/
references.go
387 lines (305 loc) · 11.5 KB
/
references.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package assets
import (
"fmt"
validator "gopkg.in/go-playground/validator.v9"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/goflow/utils/jsonx"
"github.com/nyaruka/goflow/utils/uuids"
)
func init() {
utils.Validator.RegisterStructValidation(GroupReferenceValidation, GroupReference{})
utils.Validator.RegisterStructValidation(LabelReferenceValidation, LabelReference{})
}
// Reference is interface for all reference types
type Reference interface {
fmt.Stringer
Type() string
Identity() string
Variable() bool
}
// UUIDReference is interface for all reference types that contain a UUID
type UUIDReference interface {
Reference
GenericUUID() uuids.UUID
}
// ChannelReference is used to reference a channel
type ChannelReference struct {
UUID ChannelUUID `json:"uuid" validate:"required,uuid"`
Name string `json:"name"`
}
// NewChannelReference creates a new channel reference with the given UUID and name
func NewChannelReference(uuid ChannelUUID, name string) *ChannelReference {
return &ChannelReference{UUID: uuid, Name: name}
}
// Type returns the name of the asset type
func (r *ChannelReference) Type() string {
return "channel"
}
// GenericUUID returns the untyped UUID
func (r *ChannelReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}
// Identity returns the unique identity of the asset
func (r *ChannelReference) Identity() string {
return string(r.UUID)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *ChannelReference) Variable() bool {
return false
}
func (r *ChannelReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ UUIDReference = (*ChannelReference)(nil)
// ClassifierReference is used to reference a classifier
type ClassifierReference struct {
UUID ClassifierUUID `json:"uuid" validate:"required,uuid"`
Name string `json:"name"`
}
// NewClassifierReference creates a new classifier reference with the given UUID and name
func NewClassifierReference(uuid ClassifierUUID, name string) *ClassifierReference {
return &ClassifierReference{UUID: uuid, Name: name}
}
// Type returns the name of the asset type
func (r *ClassifierReference) Type() string {
return "classifier"
}
// GenericUUID returns the untyped UUID
func (r *ClassifierReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}
// Identity returns the unique identity of the asset
func (r *ClassifierReference) Identity() string {
return string(r.UUID)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *ClassifierReference) Variable() bool {
return false
}
func (r *ClassifierReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ UUIDReference = (*ClassifierReference)(nil)
// GroupReference is used to reference a group
type GroupReference struct {
UUID GroupUUID `json:"uuid,omitempty" validate:"omitempty,uuid4"`
Name string `json:"name,omitempty"`
NameMatch string `json:"name_match,omitempty" engine:"evaluated"`
}
// NewGroupReference creates a new group reference with the given UUID and name
func NewGroupReference(uuid GroupUUID, name string) *GroupReference {
return &GroupReference{UUID: uuid, Name: name}
}
// NewVariableGroupReference creates a new group reference from the given templatized name match
func NewVariableGroupReference(nameMatch string) *GroupReference {
return &GroupReference{NameMatch: nameMatch}
}
// Type returns the name of the asset type
func (r *GroupReference) Type() string {
return "group"
}
// GenericUUID returns the untyped UUID
func (r *GroupReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}
// Identity returns the unique identity of the asset
func (r *GroupReference) Identity() string {
return string(r.UUID)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *GroupReference) Variable() bool {
return r.Identity() == ""
}
func (r *GroupReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ UUIDReference = (*GroupReference)(nil)
// FieldReference is a reference to a field
type FieldReference struct {
Key string `json:"key" validate:"required"`
Name string `json:"name"`
}
// NewFieldReference creates a new field reference with the given key and name
func NewFieldReference(key string, name string) *FieldReference {
return &FieldReference{Key: key, Name: name}
}
// Type returns the name of the asset type
func (r *FieldReference) Type() string {
return "field"
}
// Identity returns the unique identity of the asset
func (r *FieldReference) Identity() string {
return string(r.Key)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *FieldReference) Variable() bool {
return false
}
func (r *FieldReference) String() string {
return fmt.Sprintf("%s[key=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ Reference = (*FieldReference)(nil)
// FlowReference is used to reference a flow from another flow
type FlowReference struct {
UUID FlowUUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name"`
}
// NewFlowReference creates a new flow reference with the given UUID and name
func NewFlowReference(uuid FlowUUID, name string) *FlowReference {
return &FlowReference{UUID: uuid, Name: name}
}
// Type returns the name of the asset type
func (r *FlowReference) Type() string {
return "flow"
}
// GenericUUID returns the untyped UUID
func (r *FlowReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}
// Identity returns the unique identity of the asset
func (r *FlowReference) Identity() string {
return string(r.UUID)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *FlowReference) Variable() bool {
return false
}
func (r *FlowReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ UUIDReference = (*FlowReference)(nil)
// GlobalReference is a reference to a global
type GlobalReference struct {
Key string `json:"key" validate:"required"`
Name string `json:"name"`
}
// NewGlobalReference creates a new global reference with the given key and name
func NewGlobalReference(key string, name string) *GlobalReference {
return &GlobalReference{Key: key, Name: name}
}
// Type returns the name of the asset type
func (r *GlobalReference) Type() string {
return "global"
}
// Identity returns the unique identity of the asset
func (r *GlobalReference) Identity() string {
return string(r.Key)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *GlobalReference) Variable() bool {
return false
}
func (r *GlobalReference) String() string {
return fmt.Sprintf("%s[key=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ Reference = (*GlobalReference)(nil)
// LabelReference is used to reference a label
type LabelReference struct {
UUID LabelUUID `json:"uuid,omitempty" validate:"omitempty,uuid4"`
Name string `json:"name,omitempty"`
NameMatch string `json:"name_match,omitempty" engine:"evaluated"`
}
// NewLabelReference creates a new label reference with the given UUID and name
func NewLabelReference(uuid LabelUUID, name string) *LabelReference {
return &LabelReference{UUID: uuid, Name: name}
}
// NewVariableLabelReference creates a new label reference from the given templatized name match
func NewVariableLabelReference(nameMatch string) *LabelReference {
return &LabelReference{NameMatch: nameMatch}
}
// Type returns the name of the asset type
func (r *LabelReference) Type() string {
return "label"
}
// GenericUUID returns the untyped UUID
func (r *LabelReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}
// Identity returns the unique identity of the asset
func (r *LabelReference) Identity() string {
return string(r.UUID)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *LabelReference) Variable() bool {
return r.Identity() == ""
}
func (r *LabelReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
var _ UUIDReference = (*LabelReference)(nil)
// TemplateReference is used to reference a Template
type TemplateReference struct {
UUID TemplateUUID `json:"uuid" validate:"required,uuid"`
Name string `json:"name"`
}
// NewTemplateReference creates a new template reference with the given UUID and name
func NewTemplateReference(uuid TemplateUUID, name string) *TemplateReference {
return &TemplateReference{UUID: uuid, Name: name}
}
// GenericUUID returns the untyped UUID
func (r *TemplateReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}
// Identity returns the unique identity of the asset
func (r *TemplateReference) Identity() string {
return string(r.UUID)
}
// Type returns the name of the asset type
func (r *TemplateReference) Type() string {
return "template"
}
func (r *TemplateReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}
// Variable returns whether this a variable (vs concrete) reference
func (r *TemplateReference) Variable() bool {
return false
}
var _ UUIDReference = (*TemplateReference)(nil)
//------------------------------------------------------------------------------------------
// Callbacks for missing assets
//------------------------------------------------------------------------------------------
// MissingCallback is callback to be invoked when an asset is missing
type MissingCallback func(Reference, error)
// PanicOnMissing panics if an asset is reported missing
var PanicOnMissing MissingCallback = func(a Reference, err error) { panic(fmt.Sprintf("missing asset: %s, due to: %s", a, err)) }
// IgnoreMissing does nothing if an asset is reported missing
var IgnoreMissing MissingCallback = func(Reference, error) {}
//------------------------------------------------------------------------------------------
// Validation
//------------------------------------------------------------------------------------------
// GroupReferenceValidation validates that the given group reference is either a concrete
// reference or a name matcher
func GroupReferenceValidation(sl validator.StructLevel) {
ref := sl.Current().Interface().(GroupReference)
if neitherOrBoth(string(ref.UUID), ref.NameMatch) {
sl.ReportError(ref.UUID, "uuid", "UUID", "mutually_exclusive", "name_match")
sl.ReportError(ref.NameMatch, "name_match", "NameMatch", "mutually_exclusive", "uuid")
}
}
// LabelReferenceValidation validates that the given label reference is either a concrete
// reference or a name matcher
func LabelReferenceValidation(sl validator.StructLevel) {
ref := sl.Current().Interface().(LabelReference)
if neitherOrBoth(string(ref.UUID), ref.NameMatch) {
sl.ReportError(ref.UUID, "uuid", "UUID", "mutually_exclusive", "name_match")
sl.ReportError(ref.NameMatch, "name_match", "NameMatch", "mutually_exclusive", "uuid")
}
}
// utility method which returns true if both string values or neither string values is defined
func neitherOrBoth(s1 string, s2 string) bool {
return (len(s1) > 0) == (len(s2) > 0)
}
// TypedReference is a utility struct for when we need to serialize a reference with a type
type TypedReference struct {
Reference Reference `json:"-"`
Type string `json:"type"`
}
// NewTypedReference creates a new typed reference
func NewTypedReference(r Reference) TypedReference {
return TypedReference{Reference: r, Type: r.Type()}
}
func (r TypedReference) MarshalJSON() ([]byte, error) {
type typed TypedReference // need to alias type to avoid circular calls to this method
return jsonx.MarshalMerged(r.Reference, typed(r))
}