Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nats publisher func to encode req #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions transport/nats/publisher_fn.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading