twitch-bot/functions.go
Knut Ahlers 024980ee14
Extract template functions into registry
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-04-21 22:43:33 +02:00

65 lines
1.5 KiB
Go

package main
import (
"strings"
"sync"
"text/template"
korvike "github.com/Luzifer/korvike/functions"
"github.com/go-irc/irc"
)
var tplFuncs = newTemplateFuncProvider()
type (
templateFuncGetter func(*irc.Message, *rule, map[string]interface{}) interface{}
templateFuncProvider struct {
funcs map[string]templateFuncGetter
lock *sync.RWMutex
}
)
func newTemplateFuncProvider() *templateFuncProvider {
out := &templateFuncProvider{
funcs: map[string]templateFuncGetter{},
lock: new(sync.RWMutex),
}
return out
}
func (t *templateFuncProvider) GetFuncMap(m *irc.Message, r *rule, fields map[string]interface{}) template.FuncMap {
t.lock.RLock()
defer t.lock.RUnlock()
out := make(template.FuncMap)
for n, fg := range t.funcs {
out[n] = fg(m, r, fields)
}
return out
}
func (t *templateFuncProvider) Register(name string, fg templateFuncGetter) {
t.lock.Lock()
defer t.lock.Unlock()
t.funcs[name] = fg
}
func genericTemplateFunctionGetter(f interface{}) templateFuncGetter {
return func(*irc.Message, *rule, map[string]interface{}) interface{} { return f }
}
func init() {
// Register Korvike functions
for n, f := range korvike.GetFunctionMap() {
tplFuncs.Register(n, genericTemplateFunctionGetter(f))
}
tplFuncs.Register("toLower", genericTemplateFunctionGetter(strings.ToLower))
tplFuncs.Register("toUpper", genericTemplateFunctionGetter(strings.ToUpper))
tplFuncs.Register("followDate", genericTemplateFunctionGetter(twitch.GetFollowDate))
}