-
Notifications
You must be signed in to change notification settings - Fork 20
/
optin.go
64 lines (55 loc) · 1.58 KB
/
optin.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
package flows
import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
)
// OptIn adds some functionality to optin assets.
type OptIn struct {
assets.OptIn
}
// NewOptIn returns a new optin object from the given optin asset
func NewOptIn(asset assets.OptIn) *OptIn {
return &OptIn{OptIn: asset}
}
// Asset returns the underlying asset
func (o *OptIn) Asset() assets.OptIn { return o.OptIn }
// Reference returns a reference to this optin
func (o *OptIn) Reference() *assets.OptInReference {
if o == nil {
return nil
}
return assets.NewOptInReference(o.UUID(), o.Name())
}
// Context returns the properties available in expressions
//
// uuid:text -> the UUID of the optin
// name:text -> the name of the optin
//
// @context optin
func (o *OptIn) Context(env envs.Environment) map[string]types.XValue {
return map[string]types.XValue{
"__default__": types.NewXText(string(o.Name())),
"uuid": types.NewXText(string(o.UUID())),
"name": types.NewXText(string(o.Name())),
}
}
// OptInAssets provides access to all optin assets
type OptInAssets struct {
byUUID map[assets.OptInUUID]*OptIn
}
// NewOptInAssets creates a new set of optin assets
func NewOptInAssets(optins []assets.OptIn) *OptInAssets {
s := &OptInAssets{
byUUID: make(map[assets.OptInUUID]*OptIn, len(optins)),
}
for _, asset := range optins {
optin := NewOptIn(asset)
s.byUUID[asset.UUID()] = optin
}
return s
}
// Get finds the optin with the given UUID
func (s *OptInAssets) Get(uuid assets.OptInUUID) *OptIn {
return s.byUUID[uuid]
}