diff --git a/context.go b/context.go index c105631..1fe1192 100644 --- a/context.go +++ b/context.go @@ -9,18 +9,18 @@ import ( // Context context that's passed through handlers and middleware type Context struct { - Msg *nats.Msg - handlers HandlersChain - ByteReplyPayload []byte - JSONReplyPayload interface{} - didReply bool - index int8 - NatsConnection *nats.Conn - NatsEncodedConnection *nats.EncodedConn - Err error - Keys map[string]interface{} - outWriter io.ReadWriter - errWriter io.ReadWriter + *nats.Msg + handlers HandlersChain + ByteReplyPayload []byte + JSONReplyPayload interface{} + didReply bool + index int8 + *nats.Conn + *nats.EncodedConn + Err error + Keys map[string]interface{} + outWriter io.ReadWriter + errWriter io.ReadWriter } // Next to be called in middleware to invoke the middleware chain diff --git a/examples/encoded_connection/main.go b/examples/encoded_connection/main.go index 44d5872..f255b9d 100644 --- a/examples/encoded_connection/main.go +++ b/examples/encoded_connection/main.go @@ -15,7 +15,7 @@ func main() { if err != nil { return err } - e.NatsEncodedConnection = c + e.EncodedConn = c return nil } diff --git a/examples/recovery/main.go b/examples/recovery/main.go index d6458bb..e9eb596 100644 --- a/examples/recovery/main.go +++ b/examples/recovery/main.go @@ -20,7 +20,7 @@ func main() { logger.Error().Msg(fmt.Sprintf("%v", err)) if c.Msg.Reply != "" { - c.NatsConnection.Publish(c.Msg.Reply, []byte("oops")) + c.Conn.Publish(c.Msg.Reply, []byte("oops")) } })) // engine.Use(natsby.WithRecovery()) diff --git a/natsby.go b/natsby.go index 1af014d..d6c4c4f 100644 --- a/natsby.go +++ b/natsby.go @@ -21,14 +21,14 @@ type Subscriber struct { // Engine framework instance type Engine struct { - NatsConnection *nats.Conn - NatsEncodedConnection *nats.EncodedConn - subscribers []*Subscriber - middleware HandlersChain - done chan bool - QueueGroup string - OutWriter io.ReadWriter - ErrWriter io.ReadWriter + *nats.Conn + *nats.EncodedConn + subscribers []*Subscriber + middleware HandlersChain + done chan bool + QueueGroup string + OutWriter io.ReadWriter + ErrWriter io.ReadWriter } // New creates a new Router object @@ -40,7 +40,7 @@ func New(nc *nats.Conn, options ...func(*Engine) error) (*Engine, error) { e.OutWriter = os.Stdout e.ErrWriter = os.Stderr - e.NatsConnection = nc + e.Conn = nc for _, option := range options { err = option(e) @@ -78,23 +78,23 @@ func (e *Engine) Run(callbacks ...func()) error { func(subscriber *Subscriber) { handler := func(m *nats.Msg) { c := &Context{ - Msg: m, - handlers: subscriber.Handlers, - NatsConnection: e.NatsConnection, - NatsEncodedConnection: e.NatsEncodedConnection, - Keys: make(map[string]interface{}), - outWriter: e.OutWriter, - errWriter: e.ErrWriter, + Msg: m, + handlers: subscriber.Handlers, + Conn: e.Conn, + EncodedConn: e.EncodedConn, + Keys: make(map[string]interface{}), + outWriter: e.OutWriter, + errWriter: e.ErrWriter, } c.reset() c.Next() } if e.QueueGroup == "" { - e.NatsConnection.Subscribe(subscriber.Subject, handler) + e.Conn.Subscribe(subscriber.Subject, handler) return } - e.NatsConnection.QueueSubscribe(subscriber.Subject, e.QueueGroup, handler) + e.Conn.QueueSubscribe(subscriber.Subject, e.QueueGroup, handler) }(subscriber) } @@ -104,7 +104,7 @@ func (e *Engine) Run(callbacks ...func()) error { <-e.done - e.NatsConnection.Drain() + e.Conn.Drain() return nil } diff --git a/natsby_test.go b/natsby_test.go index 83ee762..d74220e 100644 --- a/natsby_test.go +++ b/natsby_test.go @@ -15,7 +15,7 @@ func TestNew(t *testing.T) { assert.Equal(t, os.Stdout, engine.OutWriter) assert.Equal(t, os.Stderr, engine.ErrWriter) - assert.Equal(t, nc, engine.NatsConnection) + assert.Equal(t, nc, engine.Conn) } func TestNewOptions(t *testing.T) { @@ -23,19 +23,19 @@ func TestNewOptions(t *testing.T) { var c *nats.EncodedConn configureEncodedConnection := func(e *Engine) error { var err error - c, err = nats.NewEncodedConn(e.NatsConnection, nats.JSON_ENCODER) + c, err = nats.NewEncodedConn(e.Conn, nats.JSON_ENCODER) if err != nil { return err } - e.NatsEncodedConnection = c + e.EncodedConn = c return nil } engine, _ := New(nc, configureEncodedConnection) assert.Equal(t, os.Stdout, engine.OutWriter) assert.Equal(t, os.Stderr, engine.ErrWriter) - assert.Equal(t, nc, engine.NatsConnection) - assert.Equal(t, c, engine.NatsEncodedConnection) + assert.Equal(t, nc, engine.Conn) + assert.Equal(t, c, engine.EncodedConn) } func TestUse(t *testing.T) { diff --git a/replier.go b/replier.go index 776c9fa..bbf3856 100644 --- a/replier.go +++ b/replier.go @@ -7,7 +7,7 @@ func WithByteReply() HandlerFunc { if c.Msg.Reply != "" { c.didReply = true - c.NatsConnection.Publish(c.Msg.Reply, c.ByteReplyPayload) + c.Respond(c.ByteReplyPayload) } } } @@ -17,9 +17,9 @@ func WithJSONReply() HandlerFunc { return func(c *Context) { c.Next() - if c.NatsEncodedConnection != nil && c.Msg.Reply != "" { + if c.EncodedConn != nil && c.Msg.Reply != "" { c.didReply = true - c.NatsEncodedConnection.Publish(c.Msg.Reply, c.JSONReplyPayload) + c.EncodedConn.Publish(c.Msg.Reply, c.JSONReplyPayload) } } } diff --git a/replier_test.go b/replier_test.go index ba5b37f..2cbc5f4 100644 --- a/replier_test.go +++ b/replier_test.go @@ -10,7 +10,7 @@ import ( func TestWithByteReply(t *testing.T) { nc, _ := nats.Connect(nats.DefaultURL) context := &Context{ - NatsConnection: nc, + Conn: nc, Msg: &nats.Msg{ Reply: "reply.inbox", }, @@ -27,12 +27,12 @@ func TestWithJsonReply(t *testing.T) { nc, _ := nats.Connect(nats.DefaultURL) encodedConnection, _ := nats.NewEncodedConn(nc, nats.JSON_ENCODER) context := &Context{ - NatsConnection: nc, + Conn: nc, Msg: &nats.Msg{ Reply: "reply.inbox", }, - ByteReplyPayload: []byte(""), - NatsEncodedConnection: encodedConnection, + ByteReplyPayload: []byte(""), + EncodedConn: encodedConnection, } handler := WithJSONReply()