summaryrefslogtreecommitdiff
path: root/filter
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2017-02-18 07:35:24 +0100
committerSilvan Jegen <s.jegen@gmail.com>2017-02-18 07:42:25 +0100
commit1bfb3355633b21bacfce57fde8e922cb9970b6d6 (patch)
tree81b11d8545e55244b1351a138893d0f304602980 /filter
parent6f82a101fbfe6a040bed68c107e1f66012b0dbab (diff)
Make the StringFilter configurable
Diffstat (limited to 'filter')
-rw-r--r--filter/str/string.go27
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 {