Skip to content

Commit 02379d4

Browse files
committed
add string to bytes util
1 parent 83e341c commit 02379d4

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

brokers/redis/list.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ func (b *List) Status() bool {
253253
}
254254

255255
func ListUnmarshalMessage(msg *disq.Message, body string) error {
256-
if err := msgpack.Unmarshal([]byte(body), (*disq.MessageRaw)(msg)); err != nil {
256+
err := msg.UnmarshalBinary(disq.StringToBytes(body))
257+
if err != nil {
257258
return err
258259
}
259260
return nil

brokers/redis/stream.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/go-redis/redis/v8"
1414
"github.com/google/uuid"
1515
log "github.com/sirupsen/logrus"
16-
"github.com/vmihailenco/msgpack"
1716
)
1817

1918
// Broker based on redis STREAM and ZSET.
@@ -316,7 +315,8 @@ func unixMs(tm time.Time) int64 {
316315

317316
func StreamUnmarshalMessage(msg *disq.Message, xmsg *redis.XMessage) error {
318317
body := xmsg.Values["body"].(string)
319-
if err := msgpack.Unmarshal([]byte(body), (*disq.MessageRaw)(msg)); err != nil {
318+
err := msg.UnmarshalBinary(disq.StringToBytes(body))
319+
if err != nil {
320320
return err
321321
}
322322
msg.ID = xmsg.ID

util.go

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strconv"
77
"time"
8+
"unsafe"
89
)
910

1011
func UnixMs(tm time.Time) int64 {
@@ -21,3 +22,18 @@ func ConsumerName() string {
2122
func DurEqual(d1, d2 time.Duration, threshold int) bool {
2223
return (d2 >= d1 && (d2-d1) < time.Duration(threshold)*time.Second)
2324
}
25+
26+
// BytesToString converts byte slice to string.
27+
func BytesToString(b []byte) string {
28+
return *(*string)(unsafe.Pointer(&b))
29+
}
30+
31+
// StringToBytes converts string to byte slice.
32+
func StringToBytes(s string) []byte {
33+
return *(*[]byte)(unsafe.Pointer(
34+
&struct {
35+
string
36+
Cap int
37+
}{s, len(s)},
38+
))
39+
}

0 commit comments

Comments
 (0)