-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathaddress.go
80 lines (72 loc) · 1.65 KB
/
address.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
package keys
import (
"sort"
"strings"
"github.com/pkg/errors"
)
// Address is a canonical list of IDs.
type Address struct {
ids []ID
idmap map[ID]bool
}
// NewAddress returns an Address from a list of IDs.
func NewAddress(ids ...ID) (*Address, error) {
if len(ids) == 0 {
return nil, errors.Errorf("no ids")
}
sort.Slice(ids, func(i, j int) bool { return ids[i].String() < ids[j].String() })
idmap := make(map[ID]bool, len(ids))
for _, r := range ids {
if ok := idmap[r]; ok {
return nil, errors.Errorf("duplicate address %s", r)
}
idmap[r] = true
}
return &Address{
ids: ids,
idmap: idmap,
}, nil
}
// ParseAddress returns address from a string.
func ParseAddress(saddrs ...string) (*Address, error) {
if len(saddrs) == 0 {
return nil, errors.Errorf("no addresses to parse")
}
ids := []ID{}
for _, saddr := range saddrs {
recs := strings.Split(saddr, ":")
for _, r := range recs {
id, err := ParseID(r)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
}
return NewAddress(ids...)
}
// Contains returns true if address contains the specified id.
func (a *Address) Contains(id ID) bool {
for _, r := range a.ids {
if r == id {
return true
}
}
return false
}
// Strings returns IDs as strings.
func (a *Address) Strings() []string {
s := make([]string, 0, len(a.ids))
for _, r := range a.ids {
s = append(s, r.String())
}
return s
}
// String returns a canonical string representation of an address.
// The first address part is less than the second part.
//
// NewAddress("bob", "alice").String() => "alice:bob"
//
func (a *Address) String() string {
return strings.Join(a.Strings(), ":")
}