-
Notifications
You must be signed in to change notification settings - Fork 19
/
group.go
88 lines (72 loc) · 2.56 KB
/
group.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
package assets
import (
"fmt"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/utils"
validator "gopkg.in/go-playground/validator.v9"
)
func init() {
utils.RegisterStructValidator(GroupReferenceValidation, GroupReference{})
}
// GroupUUID is the UUID of a group
type GroupUUID uuids.UUID
// Group is a set of contacts which can be added to and removed from manually, or based on a query.
//
// {
// "uuid": "14782905-81a6-4910-bc9f-93ad287b23c3",
// "name": "Youth",
// "query": "age <= 18"
// }
//
// @asset group
type Group interface {
UUID() GroupUUID
Name() string
Query() string
}
// 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)
//------------------------------------------------------------------------------------------
// 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")
}
}