[server] add method request with bind data
This commit is contained in:
parent
a75d3722f7
commit
930f4a0020
|
@ -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)
|
||||
|
|
4
utils.go
4
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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue