summaryrefslogtreecommitdiff
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
parent6f82a101fbfe6a040bed68c107e1f66012b0dbab (diff)
Make the StringFilter configurable
-rw-r--r--filter/str/string.go27
-rw-r--r--main.go18
2 files changed, 24 insertions, 21 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 {
diff --git a/main.go b/main.go
index 8295ea3..ec7e6b2 100644
--- a/main.go
+++ b/main.go
@@ -4,13 +4,8 @@ import (
"bufio"
"fmt"
"os"
- "strings"
"github.com/Shugyousha/stasher/conf"
- "github.com/Shugyousha/stasher/filter/str"
- "github.com/Shugyousha/stasher/input/stdin"
- "github.com/Shugyousha/stasher/manager"
- "github.com/Shugyousha/stasher/output/stdout"
)
func main() {
@@ -20,17 +15,8 @@ func main() {
os.Exit(1)
}
- man := conf.NewManagerFromConfig(bufio.NewReader(f))
- fmt.Fprintf(os.Stderr, "manager: %#v\n", man)
-
- ffmap := make(map[string]func(string) string, 10)
- ffmap["F"] = func(s string) string { return strings.ToUpper(s) }
-
- m := manager.Manager{
- Input: stdin.New(nil),
- Filter: str.New(ffmap),
- Output: stdout.New(nil),
- }
+ m := conf.NewManagerFromConfig(bufio.NewReader(f))
+ fmt.Fprintf(os.Stderr, "manager: %#v\n", m)
m.Run()
}