Skip to content

Commit

Permalink
Refactored structs
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonblanchard committed Sep 13, 2020
1 parent 0652e06 commit 84e2a05
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
24 changes: 12 additions & 12 deletions context.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/encoded_connection/main.go
Expand Up @@ -15,7 +15,7 @@ func main() {
if err != nil {
return err
}
e.NatsEncodedConnection = c
e.EncodedConn = c
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion examples/recovery/main.go
Expand Up @@ -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())
Expand Down
38 changes: 19 additions & 19 deletions natsby.go
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -104,7 +104,7 @@ func (e *Engine) Run(callbacks ...func()) error {

<-e.done

e.NatsConnection.Drain()
e.Conn.Drain()

return nil
}
Expand Down
10 changes: 5 additions & 5 deletions natsby_test.go
Expand Up @@ -15,27 +15,27 @@ 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) {
nc, _ := nats.Connect(nats.DefaultURL)
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) {
Expand Down
6 changes: 3 additions & 3 deletions replier.go
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
8 changes: 4 additions & 4 deletions replier_test.go
Expand Up @@ -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",
},
Expand All @@ -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()

Expand Down

0 comments on commit 84e2a05

Please sign in to comment.