-
Notifications
You must be signed in to change notification settings - Fork 6
/
topic.go
59 lines (47 loc) · 1.3 KB
/
topic.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
package jetstream
import (
"github.com/nats-io/nats.go"
)
// SubjectCalculator is a function used to calculate nats subject(s) for the given topic.
type SubjectCalculator func(topic string) *Subjects
// Subjects contains nats subject detail (primary + all additional) for a given watermill topic.
type Subjects struct {
Primary string
Additional []string
}
// All combines the primary and all additional subjects for use by the nats client on creation.
func (s *Subjects) All() []string {
return append([]string{s.Primary}, s.Additional...)
}
type topicInterpreter struct {
js nats.JetStreamManager
subjectCalculator SubjectCalculator
}
func defaultSubjectCalculator(topic string) *Subjects {
return &Subjects{
Primary: topic,
}
}
func newTopicInterpreter(js nats.JetStreamManager, formatter SubjectCalculator) *topicInterpreter {
if formatter == nil {
formatter = defaultSubjectCalculator
}
return &topicInterpreter{
js: js,
subjectCalculator: formatter,
}
}
func (b *topicInterpreter) ensureStream(topic string) error {
_, err := b.js.StreamInfo(topic)
if err != nil {
_, err = b.js.AddStream(&nats.StreamConfig{
Name: topic,
Description: "",
Subjects: b.subjectCalculator(topic).All(),
})
if err != nil {
return err
}
}
return err
}