[server] add method request with bind data

This commit is contained in:
Nam Huynh 2022-12-04 23:22:27 +07:00
parent a75d3722f7
commit 930f4a0020
2 changed files with 13 additions and 2 deletions

View File

@ -25,6 +25,17 @@ func (sv Server) Request(subject string, payload []byte) (*nats.Msg, error) {
return msg, err 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 ... // Reply ...
func (sv Server) Reply(msg *nats.Msg, payload []byte) error { func (sv Server) Reply(msg *nats.Msg, payload []byte) error {
return sv.instance.Publish(msg.Reply, payload) return sv.instance.Publish(msg.Reply, payload)

View File

@ -51,8 +51,8 @@ func InterfaceToBytes(data interface{}) []byte {
return b return b
} }
func BytesToInterface(b []byte, pointer interface{}) error { func BytesToInterface(b []byte, dest interface{}) error {
err := json.Unmarshal(b, pointer) err := json.Unmarshal(b, dest)
if err != nil { if err != nil {
fmt.Printf("[natsio.BytesToInterface] error: %v with data: %s\n", err, string(b)) fmt.Printf("[natsio.BytesToInterface] error: %v with data: %s\n", err, string(b))
} }