-
Notifications
You must be signed in to change notification settings - Fork 0
/
topo.go
134 lines (121 loc) · 2.49 KB
/
topo.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
package itsy
import (
"sort"
"strings"
)
type NameList []Name
func (n NameList) Len() int {
return len(n)
}
func (n NameList) Less(i, j int) bool {
return n[i].Full < n[j].Full
}
func (n NameList) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
func (n NameList) FullNames() []string {
var result []string
for _, name := range n {
result = append(result, name.Full)
}
return result
}
func (n NameList) Lookup(fullName string) (name Name, ok bool) {
for _, candidate := range n {
if candidate.Full == fullName {
return candidate, true
}
}
return
}
type Name struct {
Full string // full name, e.g. "base.all.a.x"
Base string // base, e.g. "base"
Extra string // extra name, e.g. "all.a.x"
Topo string // topology, e.g. "a.x"
All bool // if this is an 'all' name for broadcast
}
func (n Name) String() string {
return n.Full
}
func join(a, b string) string {
switch {
case a == "":
return b
case b == "":
return a
default:
return a + "." + b
}
}
func newName(base, middle, topo string, isAll bool) Name {
var extra string
if middle != "" {
extra = join(extra, middle)
}
if topo != "" {
extra = join(extra, topo)
}
full := extra
if base != "" {
full = join(base, extra)
}
return Name{
Full: full,
Base: base,
Extra: extra,
Topo: topo,
All: isAll,
}
}
// ExpandTopology expand the base label with all levels of the provided
// topology strings and filters our any duplicates.
// The base label MUST NOT end with a dot.
//
// Example: a base of "base" and topo ["a.b.c", "b"] with id "x123" is expanded to:
//
// - "base"
// - "base.id.x123"
// - "base.all"
// - "base.all.a"
// - "base.all.a.x"
// - "base.all.a.x.y"
// - "base.all.b"
// - "base.any"
// - "base.any.a"
// - "base.any.a.x"
// - "base.any.a.x.y"
// - "base.any.b"
func ExpandTopology(base string, topo []string, id string) (result NameList) {
names := make(map[string]Name)
addName := func(base, middle, topo string, isAll bool) {
n := newName(base, middle, topo, isAll)
names[n.Full] = n
}
addName(base, "", "", false)
addName(base, "all", "", true)
addName(base, "any", "", false)
if id != "" {
addName(base, "id", id, false)
}
for _, t := range topo {
if t == "" {
continue
}
for {
// "foo.bar.baz", "foo.bar", "foo"
addName(base, "all", t, true)
addName(base, "any", t, false)
idx := strings.LastIndexByte(t, '.')
if idx < 0 {
break
}
t = t[:idx]
}
}
for _, n := range names {
result = append(result, n)
}
sort.Sort(result)
return result
}