-
Notifications
You must be signed in to change notification settings - Fork 80
/
message.go
93 lines (83 loc) · 1.78 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
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
package machinery
import (
"github.com/RichardKnop/machinery/v2/tasks"
"go.opentelemetry.io/otel/propagation"
"strconv"
)
var _ propagation.TextMapCarrier = (*MessageCarrier)(nil)
type MessageCarrier struct {
msg *tasks.Headers
}
func NewMessageCarrier(msg *tasks.Headers) MessageCarrier {
return MessageCarrier{msg: msg}
}
func (c MessageCarrier) Get(key string) string {
if c.msg == nil {
*c.msg = make(tasks.Headers)
}
value := (*c.msg)[key]
if value == nil {
return ""
}
switch value.(type) {
case float64:
ft := value.(float64)
return strconv.FormatFloat(ft, 'f', -1, 64)
case float32:
ft := value.(float32)
return strconv.FormatFloat(float64(ft), 'f', -1, 64)
case int:
it := value.(int)
return strconv.Itoa(it)
case uint:
it := value.(uint)
return strconv.Itoa(int(it))
case int8:
it := value.(int8)
return strconv.Itoa(int(it))
case uint8:
it := value.(uint8)
return strconv.Itoa(int(it))
case int16:
it := value.(int16)
return strconv.Itoa(int(it))
case uint16:
it := value.(uint16)
return strconv.Itoa(int(it))
case int32:
it := value.(int32)
return strconv.Itoa(int(it))
case uint32:
it := value.(uint32)
return strconv.Itoa(int(it))
case int64:
it := value.(int64)
return strconv.FormatInt(it, 10)
case uint64:
it := value.(uint64)
return strconv.FormatUint(it, 10)
case string:
return value.(string)
case []byte:
return string(value.([]byte))
default:
return ""
}
}
func (c MessageCarrier) Set(key, val string) {
if c.msg == nil {
*c.msg = make(tasks.Headers)
}
(*c.msg)[key] = val
}
func (c MessageCarrier) Keys() []string {
if c.msg == nil {
*c.msg = make(tasks.Headers)
}
var keys []string
_ = c.msg.ForeachKey(func(key, _ string) error {
keys = append(keys, key)
return nil
})
return keys
}