1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-22 10:03:41 +02:00

Move Key and Namespace from transport/redis_pubsub to transport/redis_common

This commit is contained in:
Henrik Hautakoski 2023-03-08 16:28:52 +01:00
parent acdf827223
commit 531a2d3d17
9 changed files with 10 additions and 5 deletions

View file

@ -0,0 +1,19 @@
package redis_common
import (
"fmt"
"eosio-ship-trace-reader/transport"
)
// Key consists of a namespace and a channel.
// And is encoded to a string in this format: `<namespace>::<channel>`
type Key struct {
NS Namespace
Channel transport.Channel
}
func (k Key) String() string {
return fmt.Sprintf("%s::%s", k.NS, k.Channel)
}

View file

@ -0,0 +1,35 @@
package redis_common
import (
"testing"
"eosio-ship-trace-reader/transport"
)
func TestKey_String(t *testing.T) {
type fields struct {
NS Namespace
Channel transport.Channel
}
tests := []struct {
name string
fields fields
want string
}{
{"Empty", fields{NS: Namespace{}, Channel: transport.Channel{}}, "ship::0000000000000000000000000000000000000000000000000000000000000000::"},
{"Transactions", fields{NS: Namespace{ChainID: "id"}, Channel: transport.Channel{"transactions"}}, "ship::id::transactions"},
{"Nested", fields{NS: Namespace{ChainID: "id"}, Channel: transport.Channel{"one.two"}}, "ship::id::one.two"},
{"Action", fields{NS: Namespace{ChainID: "id"}, Channel: transport.Action{Contract: "mycontract"}.Channel()}, "ship::id::actions/contract/mycontract"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := Key{
NS: tt.fields.NS,
Channel: tt.fields.Channel,
}
if got := k.String(); got != tt.want {
t.Errorf("Key.String() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -0,0 +1,47 @@
package redis_common
import (
"strings"
"eosio-ship-trace-reader/transport"
)
const (
// Default prefix to use when none is set.
defaultPrefix = "ship"
// We need to have some chain_id, so if no one is specified.
// we use a "null" id that is all zeros.
nullChain = "0000000000000000000000000000000000000000000000000000000000000000"
)
// Namespace type.
//
// Contains a prefix and chain_id to guard keys against collision.
// Prefix should be sufficient to not collide with other application using the same redis database.
// chain_id should be ok to not let multiple reader with different chains to write to the same channels.
type Namespace struct {
Prefix string
ChainID string
}
// Create a new key with this namespace.
func (ns Namespace) NewKey(ch transport.Channel) Key {
return Key{NS: ns, Channel: ch}
}
func (ns Namespace) String() string {
// No Chain id, set to "nullChain"
if len(ns.ChainID) < 1 {
ns.ChainID = nullChain
}
// Set default prefix if empty.
if len(ns.Prefix) < 1 {
ns.Prefix = defaultPrefix
}
// Otherwise. return both.
return strings.Join([]string{ns.Prefix, ns.ChainID}, "::")
}

View file

@ -0,0 +1,23 @@
package redis_common
import "testing"
func TestNamespace_String(t *testing.T) {
tests := []struct {
name string
ns Namespace
want string
}{
{"Empty", Namespace{}, "ship::0000000000000000000000000000000000000000000000000000000000000000"},
{"Prefix Only", Namespace{Prefix: "some.prefix"}, "some.prefix::0000000000000000000000000000000000000000000000000000000000000000"},
{"ChainID Only", Namespace{ChainID: "1234"}, "ship::1234"},
{"Both", Namespace{Prefix: "my.prefix", ChainID: "1234"}, "my.prefix::1234"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.ns.String(); got != tt.want {
t.Errorf("Namespace.String() = %v, want %v", got, tt.want)
}
})
}
}