diff options
-rw-r--r-- | filter/str/string.go | 27 | ||||
-rw-r--r-- | main.go | 18 |
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 { @@ -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() } |