-
Notifications
You must be signed in to change notification settings - Fork 1
/
publisher.go
50 lines (43 loc) · 909 Bytes
/
publisher.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
package peanats
import (
"errors"
"github.com/nats-io/nats.go"
)
type Publisher interface {
PublisherMsg
Subject() string
WithSubject(subject string) Publisher
Header() *nats.Header
Publish(data []byte) error
}
type publisherImpl struct {
PublisherMsg
subject string
header nats.Header
}
func (p *publisherImpl) Subject() string {
return p.subject
}
func (p *publisherImpl) WithSubject(subject string) Publisher {
return &publisherImpl{p.PublisherMsg, subject, p.header}
}
func (p *publisherImpl) Header() *nats.Header {
if p.header == nil {
p.header = make(nats.Header)
}
return &p.header
}
func (p *publisherImpl) Publish(data []byte) error {
if p.subject == "" {
return errors.New("reply subject is not set")
}
if p.header == nil {
p.header = make(nats.Header)
}
msg := nats.Msg{
Subject: p.subject,
Header: p.header,
Data: data,
}
return p.PublishMsg(&msg)
}