-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmessage.go
51 lines (43 loc) · 1.15 KB
/
message.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
package amqp
import (
"errors"
"net/http"
"time"
"github.com/devimteam/amqp/codecs"
"github.com/streadway/amqp"
)
type (
// ContentTyper is an interface, that is used for choose codec.
ContentTyper interface {
ContentType() string
}
subMessageOptions struct {
deliveryBefore []DeliveryBefore
allowedContentTypes []string
minPriority uint8
maxPriority uint8
defaultContentType string
}
)
var CodecNotFound = errors.New("codec not found")
// constructPublishing uses message options to construct amqp.Publishing.
func constructPublishing(v interface{}, priority uint8, defaultContentType string) (msg amqp.Publishing, err error) {
msg.Timestamp = time.Now()
msg.Priority = priority
var contentType string
if ct, ok := v.(ContentTyper); ok {
msg.ContentType = ct.ContentType()
contentType = msg.ContentType
} else if defaultContentType != "" {
msg.ContentType = defaultContentType
contentType = defaultContentType
} else {
msg.ContentType = http.DetectContentType(msg.Body)
}
codec, ok := codecs.Register.Get(contentType)
if !ok {
return msg, CodecNotFound
}
msg.Body, err = codec.Encode(v)
return
}