add func convert between interface and bytes

This commit is contained in:
Nam Huynh 2022-12-04 23:16:03 +07:00
parent 756352eb19
commit a75d3722f7
1 changed files with 11 additions and 3 deletions

View File

@ -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
}