From 930f4a0020649c0fb4e6e9a6487cd063c7f61520 Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Sun, 4 Dec 2022 23:22:27 +0700 Subject: [PATCH] [server] add method request with bind data --- server_reqres.go | 11 +++++++++++ utils.go | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/server_reqres.go b/server_reqres.go index b155b16..ff1b975 100644 --- a/server_reqres.go +++ b/server_reqres.go @@ -25,6 +25,17 @@ func (sv Server) Request(subject string, payload []byte) (*nats.Msg, error) { return msg, err } +// RequestWithBindData ... +func (sv Server) RequestWithBindData(subject string, payload []byte, result interface{}) error { + msg, err := sv.Request(subject, payload) + if msg == nil || err != nil { + return err + } + + // map + return BytesToInterface(msg.Data, &result) +} + // Reply ... func (sv Server) Reply(msg *nats.Msg, payload []byte) error { return sv.instance.Publish(msg.Reply, payload) diff --git a/utils.go b/utils.go index ee6469b..fc25b06 100644 --- a/utils.go +++ b/utils.go @@ -51,8 +51,8 @@ func InterfaceToBytes(data interface{}) []byte { return b } -func BytesToInterface(b []byte, pointer interface{}) error { - err := json.Unmarshal(b, pointer) +func BytesToInterface(b []byte, dest interface{}) error { + err := json.Unmarshal(b, dest) if err != nil { fmt.Printf("[natsio.BytesToInterface] error: %v with data: %s\n", err, string(b)) }