forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
257 lines (215 loc) · 6.96 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package flows
import (
"encoding/json"
"fmt"
"strings"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
)
// Group represents a grouping of contacts. It can be static (contacts are added and removed manually through
// [actions](#action:add_contact_groups)) or dynamic (contacts are added automatically by a query). It renders as its name in a
// template, and has the following properties which can be accessed:
//
// * `uuid` the UUID of the group
// * `name` the name of the group
//
// Examples:
//
// @contact.groups -> ["Testers","Males"]
// @contact.groups.0.uuid -> b7cf0d83-f1c9-411c-96fd-c511a4cfa86d
// @contact.groups.1.name -> Males
// @(json(contact.groups.1)) -> {"name":"Males","uuid":"4f1f98fc-27a7-4a69-bbdb-24744ba739a9"}
//
// @context group
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 != "" }
// CheckDynamicMembership returns whether the given contact belongs in this dynamic group
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)
}
// Reference returns a reference to this group
func (g *Group) Reference() *GroupReference { return NewGroupReference(g.uuid, g.name) }
// Resolve resolves the given key when this group is referenced in an expression
func (g *Group) Resolve(key string) types.XValue {
switch key {
case "uuid":
return types.NewXText(string(g.uuid))
case "name":
return types.NewXText(g.name)
}
return types.NewXResolveError(g, key)
}
// Reduce is called when this object needs to be reduced to a primitive
func (g *Group) Reduce() types.XPrimitive { return types.NewXText(g.name) }
// ToXJSON is called when this type is passed to @(json(...))
func (g *Group) ToXJSON() types.XText {
return types.ResolveKeys(g, "uuid", "name").ToXJSON()
}
var _ types.XValue = (*Group)(nil)
var _ types.XResolvable = (*Group)(nil)
// GroupList defines a contact's list of groups
type GroupList struct {
groups []*Group
}
// NewGroupList creates a new group list
func NewGroupList(groups []*Group) *GroupList {
return &GroupList{groups: groups}
}
// Clone returns a clone of this group list
func (l *GroupList) clone() *GroupList {
groups := make([]*Group, len(l.groups))
copy(groups, l.groups)
return NewGroupList(groups)
}
// FindByUUID 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
}
// Add adds the given group to this group list
func (l *GroupList) Add(group *Group) bool {
if l.FindByUUID(group.uuid) == nil {
l.groups = append(l.groups, group)
return true
}
return false
}
// Remove removes the given group from this group list
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
}
// All returns all groups in this group list
func (l *GroupList) All() []*Group {
return l.groups
}
// Count returns the number of groups in this group list
func (l *GroupList) Count() int {
return len(l.groups)
}
// Index is called when this object is indexed into in an expression
func (l *GroupList) Index(index int) types.XValue {
return l.groups[index]
}
// Length is called when the length of this object is requested in an expression
func (l *GroupList) Length() int {
return len(l.groups)
}
// Reduce is called when this object needs to be reduced to a primitive
func (l GroupList) Reduce() types.XPrimitive {
array := types.NewXArray()
for _, group := range l.groups {
array.Append(group)
}
return array
}
// ToXJSON is called when this type is passed to @(json(...))
func (l GroupList) ToXJSON() types.XText {
return l.Reduce().ToXJSON()
}
var _ types.XValue = (*GroupList)(nil)
var _ types.XIndexable = (*GroupList)(nil)
// GroupSet defines the unordered set of all groups for a session
type GroupSet struct {
groups []*Group
groupsByUUID map[GroupUUID]*Group
}
// NewGroupSet creates a new group set from the given list of groups
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
}
// All returns all groups in this group set
func (s *GroupSet) All() []*Group {
return s.groups
}
// FindByUUID finds the group with the given UUID
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"`
}
// ReadGroup reads a group from the given JSON
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
}
// ReadGroupSet reads a group set from the given JSON
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
}