From f661d7dca22c573a15da25b1c6421868bb021cc2 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 5 Feb 2019 15:13:32 -0500 Subject: [PATCH] bot: update callback registry types Unfortunately, we can't hash the plugins. I went ahead and hashed the types instead. --- bot/bot.go | 12 +++++++----- bot/handlers.go | 4 +++- bot/interfaces.go | 4 +++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index d7eca51..1859f32 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -6,6 +6,7 @@ import ( "html/template" "log" "net/http" + "reflect" "strings" "github.com/jmoiron/sqlx" @@ -249,11 +250,12 @@ func (b *bot) RegisterFilter(name string, f func(string) string) { // Register a callback func (b *bot) Register(p Plugin, kind Kind, cb Callback) { - if _, ok := b.callbacks[p]; !ok { - b.callbacks[p] = make(map[Kind][]Callback) + t := reflect.TypeOf(p) + if _, ok := b.callbacks[t]; !ok { + b.callbacks[t] = make(map[Kind][]Callback) } - if _, ok := b.callbacks[p][kind]; !ok { - b.callbacks[p][kind] = []Callback{} + if _, ok := b.callbacks[t][kind]; !ok { + b.callbacks[t][kind] = []Callback{} } - b.callbacks[p][kind] = append(b.callbacks[p][kind], cb) + b.callbacks[t][kind] = append(b.callbacks[t][kind], cb) } diff --git a/bot/handlers.go b/bot/handlers.go index be789cd..4c95f1c 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "math/rand" + "reflect" "regexp" "strconv" "strings" @@ -39,7 +40,8 @@ RET: } func (b *bot) runCallback(plugin Plugin, evt Kind, message msg.Message, args ...interface{}) bool { - for _, cb := range b.callbacks[plugin][evt] { + t := reflect.TypeOf(plugin) + for _, cb := range b.callbacks[t][evt] { if cb(evt, message, args) { return true } diff --git a/bot/interfaces.go b/bot/interfaces.go index 1a348dc..e4ec4d8 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -3,6 +3,8 @@ package bot import ( + "reflect" + "github.com/jmoiron/sqlx" "github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/user" @@ -32,7 +34,7 @@ const ( type Kind int type Callback func(Kind, msg.Message, ...interface{}) bool -type CallbackMap map[Plugin]map[Kind][]Callback +type CallbackMap map[reflect.Type]map[Kind][]Callback // Bot interface serves to allow mocking of the actual bot type Bot interface {