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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
package utils
import (
"fmt"
"github.com/NeowayLabs/wabbit"
amqp "github.com/rabbitmq/amqp091-go"
)
type xstring []string
var (
amqpOptions xstring = []string{
"headers",
"contentType",
"contentEncoding",
"deliveryMode",
"priority",
"messageId",
}
)
func (s xstring) Contains(key string) bool {
for _, v := range s {
if key == v {
return true
}
}
return false
}
func ConvertOpt(opt wabbit.Option) (amqp.Publishing, error) {
var (
headers = amqp.Table{}
contentType = "text/plain"
contentEncoding = ""
deliveryMode = amqp.Transient
priority = uint8(0)
messageId = ""
)
if wrongOpt, ok := checkOptions(opt); !ok {
return amqp.Publishing{}, fmt.Errorf("Wring option '%s'. Check the docs.", wrongOpt)
}
if opt != nil {
if h, ok := opt["headers"].(amqp.Table); ok {
headers = h
}
if c, ok := opt["contentType"].(string); ok {
contentType = c
}
if c, ok := opt["contentEncoding"].(string); ok {
contentEncoding = c
}
if d, ok := opt["deliveryMode"].(uint8); ok {
deliveryMode = d
}
if p, ok := opt["priority"].(uint8); ok {
priority = p
}
if p, ok := opt["messageId"].(string); ok {
messageId = p
}
}
return amqp.Publishing{
Headers: headers,
ContentType: contentType,
ContentEncoding: contentEncoding,
DeliveryMode: deliveryMode, // 1=non-persistent, 2=persistent
Priority: priority, // 0-9
MessageId: messageId,
// a bunch of application/implementation-specific fields
}, nil
}
func checkOptions(opt wabbit.Option) (string, bool) {
optMap := map[string]interface{}(opt)
for k, _ := range optMap {
if !amqpOptions.Contains(k) {
return k, false
}
}
return "", true
}
|