diff options
author | Silvan Jegen <s.jegen@gmail.com> | 2016-11-29 20:53:06 +0100 |
---|---|---|
committer | Silvan Jegen <s.jegen@gmail.com> | 2016-11-29 20:53:06 +0100 |
commit | daa631dbb2891b6fb88357c4e6e5750bf4ed6e70 (patch) | |
tree | f89388a16562efcb68a6cdd3ea4899c4571714da | |
parent | 0175c3063667542f25179c050813cf2b391bba54 (diff) |
Do something in the StringFilter
-rw-r--r-- | manager.go | 29 |
1 files changed, 27 insertions, 2 deletions
@@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "strings" ) type Manager struct { @@ -28,7 +29,8 @@ type Filter interface { } type StringFilter struct { - retchan chan *Work + retchan chan *Work + FilterFuncMap map[string]func(string) string } type Output interface { @@ -67,6 +69,10 @@ func (i *StdinInput) Start() chan *Work { func (f *StringFilter) Filter(ic chan *Work) chan *Work { f.retchan = make(chan *Work, 100) + if f.FilterFuncMap == nil { + f.FilterFuncMap = make(map[string]func(string) string, 10) + f.FilterFuncMap["F"] = func(s string) string { return strings.ToUpper(s) } + } go func() { for w := range ic { @@ -84,7 +90,26 @@ func (f *StringFilter) Filter(ic chan *Work) chan *Work { w.err = err } - fmt.Printf("jm: %v\n", jm) + changed := false + for k, v := range jm { + ff, ok := f.FilterFuncMap[k] + if !ok { + continue + } + + jm[k] = ff(v) + changed = true + } + if changed { + bs, err := json.Marshal(jm) + if err != nil { + fmt.Printf("Error when marshalling JSON: %q\n", err) + w.err = err + } else { + w.data = bs + } + } + f.retchan <- w } close(f.retchan) |