From daa631dbb2891b6fb88357c4e6e5750bf4ed6e70 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Tue, 29 Nov 2016 20:53:06 +0100 Subject: Do something in the StringFilter --- manager.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/manager.go b/manager.go index 91933f9..b6b41fb 100644 --- a/manager.go +++ b/manager.go @@ -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) -- cgit v1.2.3