From e91951d7eda14733dbffb5d29afd202969a6561a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 6 Jan 2023 16:50:00 +0100 Subject: [PATCH] Adding internal/redis/channel.go --- internal/redis/channel.go | 49 +++++++++++++++++++++++ internal/redis/channel_test.go | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 internal/redis/channel.go create mode 100644 internal/redis/channel_test.go diff --git a/internal/redis/channel.go b/internal/redis/channel.go new file mode 100644 index 0000000..6d21a76 --- /dev/null +++ b/internal/redis/channel.go @@ -0,0 +1,49 @@ +package redis + +import ( + "fmt" + "strings" +) + +// Generic interface for all channel types. +type ChannelInterface interface { + String() string +} + +// Standard channel. Just a wrapper around string slice +type Channel []string + +func (c *Channel) Append(name string) { + *c = append(*c, name) +} + +func (c Channel) String() string { + return strings.Join(c, ".") +} + +// Define channels without any variables. +var ( + TransactionChannel = Channel{"transaction"} + HeartbeatChannel = Channel{"heartbeat"} +) + +// Action channel. + +type ActionChannel struct { + Contract string + Action string +} + +func (ac ActionChannel) String() string { + ch := Channel{"actions"} + + if len(ac.Contract) > 0 { + ch.Append(fmt.Sprintf("contract:%s", ac.Contract)) + } + + if len(ac.Action) > 0 { + ch.Append(fmt.Sprintf("action:%s", ac.Action)) + } + + return ch.String() +} diff --git a/internal/redis/channel_test.go b/internal/redis/channel_test.go new file mode 100644 index 0000000..56b9621 --- /dev/null +++ b/internal/redis/channel_test.go @@ -0,0 +1,72 @@ +package redis + +import ( + "reflect" + "testing" +) + +func TestChannel_Append(t *testing.T) { + tests := []struct { + name string + arg string + obj Channel + expected Channel + }{ + {"One", "one", Channel{}, Channel{"one"}}, + {"More", "more", Channel{"one", "two"}, Channel{"one", "two", "more"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.obj.Append(tt.arg) + if reflect.DeepEqual(tt.obj, tt.expected) == false { + t.Errorf("Channel.Append() expected %v, got %v", tt.expected, tt.obj) + } + }) + } +} + +func TestChannel_String(t *testing.T) { + tests := []struct { + name string + c Channel + want string + }{ + {"Empty", Channel{}, ""}, + {"Alot", Channel{"one", "two", "three"}, "one.two.three"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.c.String(); got != tt.want { + t.Errorf("Channel.String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestActionChannel_String(t *testing.T) { + type fields struct { + Contract string + Action string + } + tests := []struct { + name string + fields fields + want string + }{ + {"Empty", fields{}, "actions"}, + {"Contract", fields{Contract: "mycontract"}, "actions.contract:mycontract"}, + {"Action", fields{Action: "myaction"}, "actions.action:myaction"}, + {"ContractAction", fields{Contract: "mycontract", Action: "myaction"}, "actions.contract:mycontract.action:myaction"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ac := ActionChannel{ + Contract: tt.fields.Contract, + Action: tt.fields.Action, + } + if got := ac.String(); got != tt.want { + t.Errorf("ActionChannel.String() = %v, want %v", got, tt.want) + } + }) + } +}