From 1bfb3355633b21bacfce57fde8e922cb9970b6d6 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sat, 18 Feb 2017 07:35:24 +0100 Subject: Make the StringFilter configurable --- filter/str/string.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'filter') diff --git a/filter/str/string.go b/filter/str/string.go index 0ae407d..c3e2213 100644 --- a/filter/str/string.go +++ b/filter/str/string.go @@ -5,16 +5,33 @@ import ( "encoding/json" "fmt" "io" + "strings" + "github.com/Shugyousha/stasher/filter" + "github.com/Shugyousha/stasher/registry" "github.com/Shugyousha/stasher/work" ) +func init() { + registry.Filterregistry["string"] = New +} + +type Replacer interface { + Replace(string) string +} + type StringFilter struct { - FilterFuncMap map[string]func(string) string + FilterFuncMap map[string]Replacer } -func New(ffmap map[string]func(string) string) *StringFilter { - return &StringFilter{FilterFuncMap: ffmap} +func New(kv map[string]string) filter.Filter { + replmap := make(map[string]Replacer, len(kv)) + + for field, repl := range kv { + replmap[field] = strings.NewReplacer(strings.Split(repl, "/")...) + } + + return &StringFilter{FilterFuncMap: replmap} } func (f *StringFilter) Filter(w *work.Work) *work.Work { @@ -33,13 +50,13 @@ func (f *StringFilter) Filter(w *work.Work) *work.Work { } changed := false - for field, ff := range f.FilterFuncMap { + for field, repl := range f.FilterFuncMap { str, ok := jm[field] if !ok { continue } - jm[field] = ff(str) + jm[field] = repl.Replace(str) changed = true } if changed { -- cgit v1.2.3