mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-18 04:40:03 +02:00
rename transport to api
This commit is contained in:
parent
044ed4e891
commit
102045e47e
15 changed files with 40 additions and 40 deletions
19
api/redis_common/key.go
Normal file
19
api/redis_common/key.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package redis_common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"thalos/api"
|
||||
)
|
||||
|
||||
// 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 api.Channel
|
||||
}
|
||||
|
||||
func (k Key) String() string {
|
||||
return fmt.Sprintf("%s::%s", k.NS, k.Channel.Format("/"))
|
||||
}
|
||||
35
api/redis_common/key_test.go
Normal file
35
api/redis_common/key_test.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package redis_common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"thalos/api"
|
||||
)
|
||||
|
||||
func TestKey_String(t *testing.T) {
|
||||
type fields struct {
|
||||
NS Namespace
|
||||
Channel api.Channel
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
{"Empty", fields{NS: Namespace{}, Channel: api.Channel{}}, "ship::0000000000000000000000000000000000000000000000000000000000000000::"},
|
||||
{"Transactions", fields{NS: Namespace{ChainID: "id"}, Channel: api.Channel{"transactions"}}, "ship::id::transactions"},
|
||||
{"Nested", fields{NS: Namespace{ChainID: "id"}, Channel: api.Channel{"one.two"}}, "ship::id::one.two"},
|
||||
{"Action", fields{NS: Namespace{ChainID: "id"}, Channel: api.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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
47
api/redis_common/namespace.go
Normal file
47
api/redis_common/namespace.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package redis_common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"thalos/api"
|
||||
)
|
||||
|
||||
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 api.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}, "::")
|
||||
}
|
||||
23
api/redis_common/namespace_test.go
Normal file
23
api/redis_common/namespace_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue