forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
233 lines (191 loc) · 5.57 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
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
package flows
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/utils"
)
// Group represents a grouping of contacts
type Group struct {
uuid GroupUUID
name string
query string
parsedQuery *contactql.ContactQuery
}
// NewGroup returns a new group object with the passed in uuid and name
func NewGroup(uuid GroupUUID, name string, query string) *Group {
return &Group{uuid: uuid, name: name, query: query}
}
// UUID returns the UUID of the group
func (g *Group) UUID() GroupUUID { return g.uuid }
// Name returns the name of the group
func (g *Group) Name() string { return g.name }
// Query returns the query of a dynamic group
func (g *Group) Query() string { return g.query }
// ParsedQuery returns the parsed query of a dynamic group (cached)
func (g *Group) ParsedQuery() (*contactql.ContactQuery, error) {
if g.query != "" && g.parsedQuery == nil {
var err error
if g.parsedQuery, err = contactql.ParseQuery(g.query); err != nil {
return nil, err
}
}
return g.parsedQuery, nil
}
// IsDynamic returns whether this group is dynamic
func (g *Group) IsDynamic() bool { return g.query != "" }
func (g *Group) CheckDynamicMembership(session Session, contact *Contact) (bool, error) {
if !g.IsDynamic() {
return false, fmt.Errorf("can't check membership on a non-dynamic group")
}
parsedQuery, err := g.ParsedQuery()
if err != nil {
return false, err
}
return contactql.EvaluateQuery(session.Environment(), parsedQuery, contact)
}
func (g *Group) Reference() *GroupReference { return NewGroupReference(g.uuid, g.name) }
// Resolve resolves the passed in key to a value
func (g *Group) Resolve(key string) interface{} {
switch key {
case "uuid":
return g.uuid
case "name":
return g.name
}
return fmt.Errorf("no field '%s' on group", key)
}
// Default returns the default value for this group
func (g *Group) Default() interface{} { return g }
// String satisfies the stringer interface returning the name of the group
func (g *Group) String() string { return g.name }
var _ utils.VariableResolver = (*Group)(nil)
// GroupList defines a contact's list of groups
type GroupList struct {
groups []*Group
}
func NewGroupList(groups []*Group) *GroupList {
return &GroupList{groups: groups}
}
func (l *GroupList) Clone() *GroupList {
groups := make([]*Group, len(l.groups))
copy(groups, l.groups)
return NewGroupList(groups)
}
// FindGroup returns the group with the passed in UUID or nil if not found
func (l *GroupList) FindByUUID(uuid GroupUUID) *Group {
for _, group := range l.groups {
if group.uuid == uuid {
return group
}
}
return nil
}
func (l *GroupList) Add(group *Group) bool {
if l.FindByUUID(group.uuid) == nil {
l.groups = append(l.groups, group)
return true
}
return false
}
func (l *GroupList) Remove(group *Group) bool {
for g := range l.groups {
if l.groups[g].uuid == group.uuid {
l.groups = append(l.groups[:g], l.groups[g+1:]...)
return true
}
}
return false
}
func (l *GroupList) All() []*Group {
return l.groups
}
func (l *GroupList) Count() int {
return len(l.groups)
}
// Resolve looks up the passed in key for the group list, which must be either "count" or a numerical index
func (l *GroupList) Resolve(key string) interface{} {
if key == "count" {
return l.Count()
}
// key must be a numerical index
i, err := strconv.Atoi(key)
if err != nil {
return fmt.Errorf("not a valid integer '%s'", key)
}
if i < l.Count() {
return l.groups[i]
}
return nil
}
// Default returns the default value for this group, which is our entire list
func (l GroupList) Default() interface{} {
return l
}
// String stringifies the group list, joining our names with a comma
func (l GroupList) String() string {
names := make([]string, len(l.groups))
for g := range l.groups {
names[g] = l.groups[g].name
}
return strings.Join(names, ", ")
}
var _ utils.VariableResolver = (*GroupList)(nil)
// GroupSet defines the unordered set of all groups for a session
type GroupSet struct {
groups []*Group
groupsByUUID map[GroupUUID]*Group
}
func NewGroupSet(groups []*Group) *GroupSet {
s := &GroupSet{groups: groups, groupsByUUID: make(map[GroupUUID]*Group, len(groups))}
for _, group := range s.groups {
s.groupsByUUID[group.uuid] = group
}
return s
}
func (s *GroupSet) All() []*Group {
return s.groups
}
func (s *GroupSet) FindByUUID(uuid GroupUUID) *Group {
return s.groupsByUUID[uuid]
}
// FindByName looks for a group with the given name (case-insensitive)
func (s *GroupSet) FindByName(name string) *Group {
name = strings.ToLower(name)
for _, group := range s.groups {
if strings.ToLower(group.name) == name {
return group
}
}
return nil
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type groupEnvelope struct {
UUID GroupUUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name"`
Query string `json:"query,omitempty"`
}
func ReadGroup(data json.RawMessage) (*Group, error) {
var ge groupEnvelope
if err := utils.UnmarshalAndValidate(data, &ge, "group"); err != nil {
return nil, err
}
return NewGroup(ge.UUID, ge.Name, ge.Query), nil
}
func ReadGroupSet(data json.RawMessage) (*GroupSet, error) {
items, err := utils.UnmarshalArray(data)
if err != nil {
return nil, err
}
groups := make([]*Group, len(items))
for d := range items {
if groups[d], err = ReadGroup(items[d]); err != nil {
return nil, err
}
}
return NewGroupSet(groups), nil
}