-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtopics.go
39 lines (33 loc) · 968 Bytes
/
topics.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
package enats
import (
"strings"
)
const (
userPrefix = "raw."
)
// Given a nats subject, return the user-specified topic.
func ToTopic(subject string) string {
return strings.TrimPrefix(subject, EventStreamPrefix)
}
// Given a user-specified topic, return the subject names to use for NATS consumers/publishers.
func ToSubject(topic string) string {
// User specified topic
if subject, ok := strings.CutPrefix(topic, userPrefix); ok {
topic = subject
} else if !strings.HasPrefix(topic, EventStreamPrefix) {
topic = EventStreamPrefix + topic
}
// Wildcard
if strings.HasSuffix(topic, ".") {
topic += ">"
}
return topic
}
// Given a user-specified topic, return the queue name to use for NATS consumers.
func ToConsumerQueueName(pfx, topic string) string {
queue := pfx + ToSubject(topic)
queue = strings.ReplaceAll(queue, ".", "-")
queue = strings.ReplaceAll(queue, "*", "any")
queue = strings.ReplaceAll(queue, ">", "all")
return queue
}