diff options
Diffstat (limited to 'filter/str')
-rw-r--r-- | filter/str/string.go | 27 |
1 files changed, 22 insertions, 5 deletions
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 { |