-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubject.go
55 lines (44 loc) · 1.14 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
package nevent
import (
"fmt"
"strings"
)
type SubjectTransformer func(subject string) string
type SubjectNormalizer func(subject string) string
func DefaultSubjectTransformer() SubjectTransformer {
return func(subject string) string {
return strings.ToLower(subject)
}
}
func PatternSubjectTransformer(pattern string) SubjectTransformer {
base := DefaultSubjectTransformer()
return func(subject string) string {
return fmt.Sprintf(pattern, base(subject))
}
}
func STSubordinate(ns string) SubjectTransformer {
return PatternSubjectTransformer("%s."+ns)
}
func STAny() SubjectTransformer {
return STSubordinate("*")
}
func STGlob() SubjectTransformer {
return STSubordinate(">")
}
func STValue(value string) SubjectTransformer {
return STSubordinate(value)
}
func DefaultSubjectNormalize(subject string) string {
s := subject
s = strings.Replace(s, ".", "/", -1)
s = strings.Replace(s, "*", "%", -1)
s = strings.Replace(s, ">", "%%", -1)
return s
}
func SubjectDenormalize(name string) string {
s := name
s = strings.Replace(s, "%%", ">", -1)
s = strings.Replace(s, "%", "*", -1)
s = strings.Replace(s, "/", ".", -1)
return s
}