Slight tags API changes (Jae comments)

This commit is contained in:
Christopher Goes 2018-05-10 00:47:28 +02:00
parent f103cd412d
commit 60b56f9b1c
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
3 changed files with 32 additions and 23 deletions

View File

@ -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?

View File

@ -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")})
}

View File

@ -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
}