From a75d3722f7e8056e7957526599f08d0f1db2aaaa Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Sun, 4 Dec 2022 23:16:03 +0700 Subject: [PATCH] add func convert between interface and bytes --- utils.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/utils.go b/utils.go index f93f64d..ee6469b 100644 --- a/utils.go +++ b/utils.go @@ -42,11 +42,19 @@ func GenerateReqrepSubject(server, service, subject string) string { return fmt.Sprintf("%s.reqrep.%s.%s.%s", globalConfig.StreamName, server, service, subject) } -// ToBytes ... -func ToBytes(data interface{}) []byte { +// InterfaceToBytes ... +func InterfaceToBytes(data interface{}) []byte { b, err := json.Marshal(data) if err != nil { - fmt.Printf("[natsio.ToBytes] error: %v with data: %v\n", err, data) + fmt.Printf("[natsio.InterfaceToBytes] error: %v with data: %v\n", err, data) } return b } + +func BytesToInterface(b []byte, pointer interface{}) error { + err := json.Unmarshal(b, pointer) + if err != nil { + fmt.Printf("[natsio.BytesToInterface] error: %v with data: %s\n", err, string(b)) + } + return err +}