-
Notifications
You must be signed in to change notification settings - Fork 20
/
topic.go
83 lines (70 loc) · 1.93 KB
/
topic.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
package flows
import (
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
)
// Topic represents a ticket topic
type Topic struct {
assets.Topic
}
// NewTopic creates a new topic from the given asset
func NewTopic(asset assets.Topic) *Topic {
return &Topic{Topic: asset}
}
// Asset returns the underlying asset
func (t *Topic) Asset() assets.Topic { return t.Topic }
// Reference returns a reference to this topic
func (t *Topic) Reference() *assets.TopicReference {
if t == nil {
return nil
}
return assets.NewTopicReference(t.UUID(), t.Name())
}
// Context returns the properties available in expressions
//
// __default__:text -> the name
// uuid:text -> the UUID of the topic
// name:text -> the name of the topic
//
// @context topic
func (t *Topic) Context(env envs.Environment) map[string]types.XValue {
return map[string]types.XValue{
"__default__": types.NewXText(t.Name()),
"uuid": types.NewXText(string(t.UUID())),
"name": types.NewXText(t.Name()),
}
}
var _ assets.Topic = (*Topic)(nil)
// TopicAssets provides access to all topic assets
type TopicAssets struct {
all []*Topic
byUUID map[assets.TopicUUID]*Topic
}
// NewTopicAssets creates a new set of topic assets
func NewTopicAssets(topics []assets.Topic) *TopicAssets {
s := &TopicAssets{
byUUID: make(map[assets.TopicUUID]*Topic, len(topics)),
}
for _, asset := range topics {
topic := NewTopic(asset)
s.all = append(s.all, topic)
s.byUUID[topic.UUID()] = topic
}
return s
}
// Get returns the topic with the given UUID
func (s *TopicAssets) Get(uuid assets.TopicUUID) *Topic {
return s.byUUID[uuid]
}
// FindByName looks for a topic with the given name (case-insensitive)
func (s *TopicAssets) FindByName(name string) *Topic {
name = strings.ToLower(name)
for _, topic := range s.all {
if strings.ToLower(topic.Name()) == name {
return topic
}
}
return nil
}