From 61beca435ccc986f5115bd81eda1ebbff9df96dc Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 6 Jan 2023 17:05:44 +0100 Subject: [PATCH] Adding internal/redis/key.go --- internal/redis/key.go | 17 +++++++++++++++++ internal/redis/key_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 internal/redis/key.go create mode 100644 internal/redis/key_test.go diff --git a/internal/redis/key.go b/internal/redis/key.go new file mode 100644 index 0000000..7e89e68 --- /dev/null +++ b/internal/redis/key.go @@ -0,0 +1,17 @@ +package redis + +import ( + "fmt" +) + +// Key consists of a namespace and a channel. +// And is encoded to a string in this format: `::` + +type Key struct { + NS Namespace + Channel ChannelInterface +} + +func (k Key) String() string { + return fmt.Sprintf("%s::%s", k.NS, k.Channel) +} diff --git a/internal/redis/key_test.go b/internal/redis/key_test.go new file mode 100644 index 0000000..caaf34e --- /dev/null +++ b/internal/redis/key_test.go @@ -0,0 +1,31 @@ +package redis + +import "testing" + +func TestKey_String(t *testing.T) { + type fields struct { + NS Namespace + Channel ChannelInterface + } + tests := []struct { + name string + fields fields + want string + }{ + {"Empty", fields{NS: Namespace{}, Channel: Channel{}}, "ship.0000000000000000000000000000000000000000000000000000000000000000::"}, + {"Transactions", fields{NS: Namespace{ChainID: "id"}, Channel: Channel{"transactions"}}, "ship.id::transactions"}, + {"Nested", fields{NS: Namespace{ChainID: "id"}, Channel: Channel{"one.two"}}, "ship.id::one.two"}, + {"Action", fields{NS: Namespace{ChainID: "id"}, Channel: ActionChannel{Contract: "mycontract"}}, "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) + } + }) + } +}