Skip to content

Commit

Permalink
fix. Reset proto before Unmarshall
Browse files Browse the repository at this point in the history
  • Loading branch information
imperiuse committed Dec 24, 2020
1 parent b5278e8 commit e269347
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nats/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ func (c *client) ReplyHandler(subj Subj, awaitData Serializable, msgHandler Hand
return
}

awaitData.Reset() // Important! For use clean struct

if err := awaitData.Unmarshal(msg.Data); err != nil {
c.log.Error("[ReplyHandler] Unmarshal",
zap.String("subj", string(subj)),
Expand Down Expand Up @@ -260,6 +262,8 @@ func (c *client) ReplyQueueHandler(subj Subj, qGroup QueueGroup, awaitData Seria
return
}

awaitData.Reset() // Important! For use clean struct

if err := awaitData.Unmarshal(msg.Data); err != nil {
c.log.Error("[ReplyQueueHandler] Unmarshal",
zap.String("subj", string(subj)),
Expand Down
4 changes: 4 additions & 0 deletions serializable/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ func (e *Example) Unmarshal(data []byte) error {
}
return err
}

func (e *Example) Reset() {
return
}
8 changes: 8 additions & 0 deletions serializable/mock/serializable.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ func (d *DataMock) Unmarshal(data []byte) error {
return nil
}

func (d *DataMock) Reset() {
return
}

func (d *BadDataMock) Marshal() ([]byte, error) {
return nil, ErrBadDataMock
}

func (d *BadDataMock) Unmarshal([]byte) error {
return ErrBadDataMock
}

func (d *BadDataMock) Reset() {
return
}
1 change: 1 addition & 0 deletions serializable/serializable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ type (
Serializable interface {
Marshal() ([]byte, error)
Unmarshal([]byte) error
Reset()
}
)
4 changes: 4 additions & 0 deletions streaming/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ func (c *client) Subscribe(subj Subj, awaitData Serializable, handler Handler, o
return
}

awaitData.Reset() // Important! For use clean struct

if err := awaitData.Unmarshal(msg.Data); err != nil {
c.log.Error("[Subscribe] Unmarshal",
zap.Error(err),
Expand Down Expand Up @@ -331,6 +333,8 @@ func (c *client) QueueSubscribe(subj Subj, qgroup QueueGroup, awaitData Serializ
return
}

awaitData.Reset() // Important! For use clean struct

if err := awaitData.Unmarshal(msg.Data); err != nil {
c.log.Error("[QueueSubscribe] Unmarshal",
zap.String("subj", string(subj)),
Expand Down

0 comments on commit e269347

Please sign in to comment.