From 60b56f9b1c5c5de76ba8e0a01a367b9cb8ccd15d Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 10 May 2018 00:47:28 +0200 Subject: [PATCH] Slight tags API changes (Jae comments) --- types/tags.go | 37 ++++++++++++++++++++++++++----------- types/tags_test.go | 14 ++++---------- x/bank/keeper.go | 4 ++-- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/types/tags.go b/types/tags.go index 0811ef870..5a94e9bde 100644 --- a/types/tags.go +++ b/types/tags.go @@ -8,26 +8,41 @@ import ( type Tag = cmn.KVPair // Type synonym for convenience -type Tags = cmn.KVPairs - -// Append two lists of tags -func AppendTags(a, b Tags) Tags { - return append(a, b...) -} +type Tags cmn.KVPairs // New empty tags func EmptyTags() Tags { return make(Tags, 0) } -// Single tag to tags -func SingleTag(t Tag) Tags { - return append(EmptyTags(), t) +// Append a single tag +func (t Tags) AppendTag(k string, v []byte) Tags { + return append(t, MakeTag(k, v)) +} + +// Append two lists of tags +func AppendTags(a, b Tags) Tags { + return append(a, b...) +} + +// New variadic tags, must be k string, v []byte repeating +func NewTags(tags ...interface{}) Tags { + var ret Tags + if len(tags)%2 != 0 { + panic("must specify key-value pairs as varargs") + } + i := 0 + for { + if i == len(tags) { + break + } + ret = append(ret, Tag{Key: []byte(tags[i].(string)), Value: tags[i+1].([]byte)}) + i += 2 + } + return ret } // 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? diff --git a/types/tags_test.go b/types/tags_test.go index ae00ffdd9..84dc10b33 100644 --- a/types/tags_test.go +++ b/types/tags_test.go @@ -7,8 +7,8 @@ import ( ) func TestAppendTags(t *testing.T) { - a := SingleTag(MakeTag("a", []byte("1"))) - b := SingleTag(MakeTag("b", []byte("2"))) + a := NewTags("a", []byte("1")) + b := NewTags("b", []byte("2")) c := AppendTags(a, b) require.Equal(t, c, Tags{MakeTag("a", []byte("1")), MakeTag("b", []byte("2"))}) } @@ -18,13 +18,7 @@ func TestEmptyTags(t *testing.T) { require.Equal(t, a, Tags{}) } -func TestSingleTag(t *testing.T) { - a := MakeTag("a", []byte("1")) - b := SingleTag(a) +func TestNewTags(t *testing.T) { + b := NewTags("a", []byte("1")) 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")}) -} diff --git a/x/bank/keeper.go b/x/bank/keeper.go index 869caeb25..8a73b964d 100644 --- a/x/bank/keeper.go +++ b/x/bank/keeper.go @@ -138,7 +138,7 @@ func subtractCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt return amt, nil, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", oldCoins, amt)) } err := setCoins(ctx, am, addr, newCoins) - tags := sdk.SingleTag(sdk.MakeTag("sender", addr.Bytes())) + tags := sdk.NewTags("sender", addr.Bytes()) return newCoins, tags, err } @@ -150,7 +150,7 @@ func addCoins(ctx sdk.Context, am sdk.AccountMapper, addr sdk.Address, amt sdk.C return amt, nil, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", oldCoins, amt)) } err := setCoins(ctx, am, addr, newCoins) - tags := sdk.SingleTag(sdk.MakeTag("recipient", addr.Bytes())) + tags := sdk.NewTags("recipient", addr.Bytes()) return newCoins, tags, err }