Example tag implementation for CoinKeeper

This commit is contained in:
Christopher Goes
2018-05-10 17:36:55 +02:00
parent be975fc264
commit bef7e44f6d
12 changed files with 109 additions and 41 deletions
+1 -2
View File
@@ -2,7 +2,6 @@ package types
import (
abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
)
// Result is the union of ResponseDeliverTx and ResponseCheckTx.
@@ -31,7 +30,7 @@ type Result struct {
ValidatorUpdates []abci.Validator
// Tags are used for transaction indexing and pubsub.
Tags []cmn.KVPair
Tags Tags
}
// TODO: In the future, more codes may be OK.
+31
View File
@@ -0,0 +1,31 @@
package types
import (
cmn "github.com/tendermint/tmlibs/common"
)
type Tag = cmn.KVPair
type Tags = cmn.KVPairs
// Append two lists of tags
func AppendTags(a, b Tags) Tags {
return append(a, b...)
}
// New empty tags
func EmptyTags() Tags {
return make(Tags, 0)
}
// Single tag to tags
func SingleTag(t Tag) Tags {
return append(EmptyTags(), t)
}
// Make a tag from a key and a value
func MakeTag(k string, v []byte) Tag {
return Tag{Key: []byte(k), Value: v}
}
// TODO: Deduplication?
+30
View File
@@ -0,0 +1,30 @@
package types
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAppendTags(t *testing.T) {
a := SingleTag(MakeTag("a", []byte("1")))
b := SingleTag(MakeTag("b", []byte("2")))
c := AppendTags(a, b)
require.Equal(t, c, Tags{MakeTag("a", []byte("1")), MakeTag("b", []byte("2"))})
}
func TestEmptyTags(t *testing.T) {
a := EmptyTags()
require.Equal(t, a, Tags{})
}
func TestSingleTag(t *testing.T) {
a := MakeTag("a", []byte("1"))
b := SingleTag(a)
require.Equal(t, b, Tags{MakeTag("a", []byte("1"))})
}
func TestMakeTag(t *testing.T) {
a := MakeTag("a", []byte("1"))
require.Equal(t, a, Tag{[]byte("a"), []byte("1")})
}