From e2693471d7d033036e2a0c8cb9c50724bd1521b9 Mon Sep 17 00:00:00 2001 From: Arseny Sazanov Date: Thu, 24 Dec 2020 15:18:59 +0300 Subject: [PATCH] fix. Reset proto before Unmarshall --- nats/client.go | 4 ++++ serializable/json/json.go | 4 ++++ serializable/mock/serializable.go | 8 ++++++++ serializable/serializable.go | 1 + streaming/client.go | 4 ++++ 5 files changed, 21 insertions(+) diff --git a/nats/client.go b/nats/client.go index 1c58224..c0efb63 100644 --- a/nats/client.go +++ b/nats/client.go @@ -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)), @@ -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)), diff --git a/serializable/json/json.go b/serializable/json/json.go index d62e537..4f85a89 100644 --- a/serializable/json/json.go +++ b/serializable/json/json.go @@ -35,3 +35,7 @@ func (e *Example) Unmarshal(data []byte) error { } return err } + +func (e *Example) Reset() { + return +} diff --git a/serializable/mock/serializable.go b/serializable/mock/serializable.go index 6c29b6b..31f834f 100644 --- a/serializable/mock/serializable.go +++ b/serializable/mock/serializable.go @@ -20,6 +20,10 @@ func (d *DataMock) Unmarshal(data []byte) error { return nil } +func (d *DataMock) Reset() { + return +} + func (d *BadDataMock) Marshal() ([]byte, error) { return nil, ErrBadDataMock } @@ -27,3 +31,7 @@ func (d *BadDataMock) Marshal() ([]byte, error) { func (d *BadDataMock) Unmarshal([]byte) error { return ErrBadDataMock } + +func (d *BadDataMock) Reset() { + return +} diff --git a/serializable/serializable.go b/serializable/serializable.go index cf17c09..489b173 100644 --- a/serializable/serializable.go +++ b/serializable/serializable.go @@ -4,5 +4,6 @@ type ( Serializable interface { Marshal() ([]byte, error) Unmarshal([]byte) error + Reset() } ) diff --git a/streaming/client.go b/streaming/client.go index 7b8bc30..71e5f1c 100644 --- a/streaming/client.go +++ b/streaming/client.go @@ -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), @@ -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)),