From 4d675ce3cd0d9f1515aee1fe581b1e1d91d75ba7 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Thu, 20 Jan 2022 17:26:50 -0500 Subject: [PATCH] Fix payload decoder to cover more payload types Signed-off-by: Jim Zhang --- internal/events/submanager_test.go | 2 +- internal/events/subscription.go | 6 +-- internal/utils/payload_decoder.go | 16 ++---- internal/utils/payload_decoder_test.go | 67 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 internal/utils/payload_decoder_test.go diff --git a/internal/events/submanager_test.go b/internal/events/submanager_test.go index 0a00e82..0346b90 100644 --- a/internal/events/submanager_test.go +++ b/internal/events/submanager_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 Kaleido +// Copyright 2021 Kaleido // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/internal/events/subscription.go b/internal/events/subscription.go index 6496b0a..0e5c567 100644 --- a/internal/events/subscription.go +++ b/internal/events/subscription.go @@ -155,7 +155,7 @@ func (s *subscription) getEventTimestamp(evt *eventsapi.EventEntry) { blockNumber := strconv.FormatUint(evt.BlockNumber, 10) if ts, ok := s.ep.stream.blockTimestampCache.Get(blockNumber); ok { // we found the timestamp for the block in our local cache, assert it's type and return, no need to query the chain - timestamps := ts.(map[int]int64) + timestamps := ts.([]int64) evt.Timestamp = timestamps[evt.TransactionIndex] return } @@ -167,8 +167,8 @@ func (s *subscription) getEventTimestamp(evt *eventsapi.EventEntry) { return } // blocks in Fabric does not have a timestamp. instead only transactions have their own timestamps - // so each entry in the cache is a map of (tx index, tx timestamp) - timestamps := make(map[int]int64) + // so each entry in the cache is a slice of (tx timestamp) + timestamps := make([]int64, len(block.Transactions)) for idx, tx := range block.Transactions { timestamps[idx] = tx.Timestamp } diff --git a/internal/utils/payload_decoder.go b/internal/utils/payload_decoder.go index 916ad5d..a18a973 100644 --- a/internal/utils/payload_decoder.go +++ b/internal/utils/payload_decoder.go @@ -20,17 +20,11 @@ import "encoding/json" func DecodePayload(payload []byte) interface{} { // first attempt to parse for JSON, if not successful then just decode to string - var structuredArray []map[string]interface{} - err2 := json.Unmarshal(payload, &structuredArray) - if err2 != nil { - structuredMap := make(map[string]interface{}) - err2 = json.Unmarshal(payload, &structuredMap) - if err2 != nil { - return string(payload) - } else { - return structuredMap - } + var structured interface{} + err := json.Unmarshal(payload, &structured) + if err != nil { + return string(payload) } else { - return structuredArray + return structured } } diff --git a/internal/utils/payload_decoder_test.go b/internal/utils/payload_decoder_test.go new file mode 100644 index 0000000..5fc9bb0 --- /dev/null +++ b/internal/utils/payload_decoder_test.go @@ -0,0 +1,67 @@ +// Copyright 2022 Kaleido + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDecodingArrayOfJson(t *testing.T) { + assert := assert.New(t) + + testBytes := []byte(`[{"a":1, "b":2}, {"a":10, "b":20}]`) + result := DecodePayload(testBytes) + m, ok := result.([]interface{}) + assert.Equal(true, ok) + n, ok := m[0].(map[string]interface{}) + assert.Equal(true, ok) + assert.Equal(float64(1), n["a"]) +} + +func TestDecodingJson(t *testing.T) { + assert := assert.New(t) + + testBytes := []byte(`{"a":1, "b":2}`) + result := DecodePayload(testBytes) + m, ok := result.(map[string]interface{}) + assert.Equal(true, ok) + assert.Equal(float64(1), m["a"]) +} + +func TestDecodingArrayOfStrings(t *testing.T) { + assert := assert.New(t) + + testBytes := []byte(`["string1", "string2"]`) + result := DecodePayload(testBytes) + m, ok := result.([]interface{}) + assert.Equal(true, ok) + n, ok := m[0].(string) + assert.Equal(true, ok) + assert.Equal("string1", n) +} + +func TestDecodingArrayOfNumbers(t *testing.T) { + assert := assert.New(t) + + testBytes := []byte(`[1, 2, 3]`) + result := DecodePayload(testBytes) + m, ok := result.([]interface{}) + assert.Equal(true, ok) + n, ok := m[0].(float64) + assert.Equal(true, ok) + assert.Equal(float64(1), n) +}