-
Notifications
You must be signed in to change notification settings - Fork 20
/
label.go
77 lines (64 loc) · 1.67 KB
/
label.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
package flows
import (
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/pkg/errors"
)
// Label represents a message label
type Label struct {
assets.Label
}
// NewLabel creates a new label from the given asset
func NewLabel(asset assets.Label) *Label {
return &Label{Label: asset}
}
// Asset returns the underlying asset
func (l *Label) Asset() assets.Label { return l.Label }
// Reference returns a reference to this label
func (l *Label) Reference() *assets.LabelReference {
if l == nil {
return nil
}
return assets.NewLabelReference(l.UUID(), l.Name())
}
var _ assets.Label = (*Label)(nil)
// LabelAssets provides access to all label assets
type LabelAssets struct {
all []*Label
byUUID map[assets.LabelUUID]*Label
}
// NewLabelAssets creates a new set of label assets
func NewLabelAssets(labels []assets.Label) *LabelAssets {
s := &LabelAssets{
all: make([]*Label, len(labels)),
byUUID: make(map[assets.LabelUUID]*Label, len(labels)),
}
for g, asset := range labels {
label := NewLabel(asset)
s.all[g] = label
s.byUUID[label.UUID()] = label
}
return s
}
// All returns all the labels
func (s *LabelAssets) All() []*Label {
return s.all
}
// Get returns the label with the given UUID
func (s *LabelAssets) Get(uuid assets.LabelUUID) (*Label, error) {
c, found := s.byUUID[uuid]
if !found {
return nil, errors.Errorf("no such label with UUID '%s'", uuid)
}
return c, nil
}
// FindByName looks for a label with the given name (case-insensitive)
func (s *LabelAssets) FindByName(name string) *Label {
name = strings.ToLower(name)
for _, label := range s.all {
if strings.ToLower(label.Name()) == name {
return label
}
}
return nil
}