Skip to content

Commit

Permalink
Fix payload decoder to cover more payload types
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <jim.zhang@kaleido.io>
  • Loading branch information
jimthematrix committed Jan 20, 2022
1 parent 9e4af37 commit 4d675ce
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/events/submanager_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions internal/events/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
16 changes: 5 additions & 11 deletions internal/utils/payload_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
67 changes: 67 additions & 0 deletions internal/utils/payload_decoder_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4d675ce

Please sign in to comment.