-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubject.go
72 lines (57 loc) · 1.18 KB
/
subject.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
package eventstore
import "strings"
type Subject interface {
subject()
}
var (
SingleToken = Subject(singleToken{})
MultiToken = Subject(multiToken{})
)
type TextSubject string
func (TextSubject) subject() {}
type singleToken struct{}
func (singleToken) subject() {}
type multiToken struct{}
func (multiToken) subject() {}
type TextSubjects []TextSubject
func (ts TextSubjects) Join(sep string) string {
return join(ts, sep)
}
func (ts TextSubjects) Compare(comp ...Subject) bool {
for i, s := range ts {
switch c := comp[i].(type) {
case TextSubject:
if s == c {
continue
}
return false
case singleToken:
continue
case multiToken:
return true
}
}
return true
}
type text interface{ ~string }
// join is a copy of [strings.Join] which allows type constraints
func join[t text](elems []t, sep string) string {
switch len(elems) {
case 0:
return ""
case 1:
return string(elems[0])
}
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
n += len(elems[i])
}
var b strings.Builder
b.Grow(n)
b.WriteString(string(elems[0]))
for _, s := range elems[1:] {
b.WriteString(sep)
b.WriteString(string(s))
}
return b.String()
}