Sdk2 kvpair (#102)

* Canonical KVPair in common
* Simplify common/Bytes to just hex encode
This commit is contained in:
Jae Kwon
2017-12-17 13:04:15 -08:00
committed by GitHub
parent 9226659413
commit aab2d70dd3
8 changed files with 254 additions and 90 deletions
+53
View File
@@ -0,0 +1,53 @@
package common
import (
"encoding/hex"
"fmt"
"strings"
)
// The main purpose of Bytes is to enable HEX-encoding for json/encoding.
type Bytes []byte
// Marshal needed for protobuf compatibility
func (b Bytes) Marshal() ([]byte, error) {
return b, nil
}
// Unmarshal needed for protobuf compatibility
func (b *Bytes) Unmarshal(data []byte) error {
*b = data
return nil
}
// This is the point of Bytes.
func (b Bytes) MarshalJSON() ([]byte, error) {
s := strings.ToUpper(hex.EncodeToString(b))
jb := make([]byte, len(s)+2)
jb[0] = '"'
copy(jb[1:], []byte(s))
jb[1] = '"'
return jb, nil
}
// This is the point of Bytes.
func (b *Bytes) UnmarshalJSON(data []byte) error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return fmt.Errorf("Invalid hex string: %s", data)
}
bytes, err := hex.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*b = bytes
return nil
}
// Allow it to fulfill various interfaces in light-client, etc...
func (b Bytes) Bytes() []byte {
return b
}
func (b Bytes) String() string {
return strings.ToUpper(hex.EncodeToString(b))
}
+68
View File
@@ -0,0 +1,68 @@
package common
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// This is a trivial test for protobuf compatibility.
func TestMarshal(t *testing.T) {
assert := assert.New(t)
b := []byte("hello world")
dataB := Bytes(b)
b2, err := dataB.Marshal()
assert.Nil(err)
assert.Equal(b, b2)
var dataB2 Bytes
err = (&dataB2).Unmarshal(b)
assert.Nil(err)
assert.Equal(dataB, dataB2)
}
// Test that the hex encoding works.
func TestJSONMarshal(t *testing.T) {
assert := assert.New(t)
type TestStruct struct {
B1 []byte
B2 Bytes
}
cases := []struct {
input []byte
expected string
}{
{[]byte(``), `{"B1":"","B2":""}`},
{[]byte(``), `{"B1":"","B2":""}`},
{[]byte(``), `{"B1":"","B2":""}`},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
ts := TestStruct{B1: tc.input, B2: tc.input}
// Test that it marshals correctly to JSON.
jsonBytes, err := json.Marshal(ts)
if err != nil {
t.Fatal(err)
}
assert.Equal(string(jsonBytes), tc.expected)
// TODO do fuzz testing to ensure that unmarshal fails
// Test that unmarshaling works correctly.
ts2 := TestStruct{}
err = json.Unmarshal(jsonBytes, &ts2)
if err != nil {
t.Fatal(err)
}
assert.Equal(ts2.B1, tc.input)
assert.Equal(ts2.B2, Bytes(tc.input))
})
}
}
+30
View File
@@ -0,0 +1,30 @@
package common
import (
"bytes"
"sort"
)
type KVPair struct {
Key Bytes
Value Bytes
}
type KVPairs []KVPair
// Sorting
func (kvs KVPairs) Len() int { return len(kvs) }
func (kvs KVPairs) Less(i, j int) bool {
switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
case -1:
return true
case 0:
return bytes.Compare(kvs[i].Value, kvs[j].Value) < 0
case 1:
return false
default:
panic("invalid comparison result")
}
}
func (kvs KVPairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
func (kvs KVPairs) Sort() { sort.Sort(kvs) }