From 95be4b4720e7b08a755a7a9e1d8f602aa58e123d Mon Sep 17 00:00:00 2001 From: ayushkedia29 Date: Wed, 27 Mar 2024 12:31:24 +0530 Subject: [PATCH] fix: nats publisher func to encode req --- transport/nats/publisher_fn.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/transport/nats/publisher_fn.go b/transport/nats/publisher_fn.go index 171beeb..39ddbe7 100644 --- a/transport/nats/publisher_fn.go +++ b/transport/nats/publisher_fn.go @@ -1,21 +1,31 @@ package nats import ( + "bytes" "context" "encoding/json" + + "github.com/nats-io/nats.go" natn "github.com/nats-io/nats.go" + "github.com/unbxd/go-base/v2/errors" ) // EncodeJSONRequest is an Encoder that serializes the request as a // JSON object to the Data of the Msg. Many JSON-over-NATS services can use it as // a sensible default. -func EncodeJSONRequest(_ context.Context, msg *natn.Msg, request interface{}) error { - b, err := json.Marshal(request) +func EncodeJSONRequest(_ context.Context, subject string, request interface{}) (*nats.Msg, error) { + var ( + buf bytes.Buffer + err error + ) + + err = json.NewEncoder(&buf).Encode(request) if err != nil { - return err + return nil, errors.Wrap(err, "defaultencoder: encoding error") } - msg.Data = b - - return nil + return &natn.Msg{ + Subject: subject, + Data: buf.Bytes(), + }, err }