-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
69 lines (62 loc) · 1.36 KB
/
match.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
package tt
import (
"strings"
)
// https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901241
func match(filter, topicName string) bool {
if filter == "#" {
if topicName[0] == '$' {
return false
}
return true
}
hasNumberSign := strings.HasSuffix(filter, "#")
hasPlusSign := strings.Contains(filter, "+")
switch {
case hasNumberSign && !hasPlusSign:
// a/b/# a/b
prefix := filter[:len(filter)-2]
return strings.HasPrefix(topicName, prefix)
case hasNumberSign && hasPlusSign:
// a/+/# a/b/c
filter = filter[:len(filter)-2]
filterLevels := strings.Split(filter, "/")
nameLevels := strings.Split(topicName, "/")
for i, f := range filterLevels {
// a/b/+/# a/b
if i == len(nameLevels) {
return false
}
if f == "+" {
continue
}
if f != nameLevels[i] {
return false
}
}
return true
case !hasNumberSign && hasPlusSign:
filterLevels := strings.Split(filter, "/")
nameLevels := strings.Split(topicName, "/")
if len(nameLevels) > len(filterLevels) {
return false
}
for i, f := range filterLevels {
// a/+/+ a/b
if i == len(nameLevels) {
return false
}
if f == "+" {
if i == 0 && (len(nameLevels[i]) > 0 && nameLevels[i][0] == '$') {
return false
}
continue
}
if f != nameLevels[i] {
return false
}
}
return true
}
return filter == topicName
}