From 2eb6ecf4e9d8040bd2a698dbfa80bcd45b0c371e Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sat, 21 Jan 2017 12:34:51 +0100 Subject: Use the registry for filter modules as well --- filter/filterinterface.go | 7 ++++++ filter/http.go | 35 ----------------------------- filter/http/http.go | 42 +++++++++++++++++++++++++++++++++++ filter/interface.go | 7 ------ filter/str/string.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ filter/string.go | 56 ----------------------------------------------- 6 files changed, 105 insertions(+), 98 deletions(-) create mode 100644 filter/filterinterface.go delete mode 100644 filter/http.go create mode 100644 filter/http/http.go delete mode 100644 filter/interface.go create mode 100644 filter/str/string.go delete mode 100644 filter/string.go (limited to 'filter') diff --git a/filter/filterinterface.go b/filter/filterinterface.go new file mode 100644 index 0000000..83d2dfe --- /dev/null +++ b/filter/filterinterface.go @@ -0,0 +1,7 @@ +package filter + +import "github.com/Shugyousha/stasher/work" + +type Filter interface { + Filter(*work.Work) *work.Work +} diff --git a/filter/http.go b/filter/http.go deleted file mode 100644 index 97fffa7..0000000 --- a/filter/http.go +++ /dev/null @@ -1,35 +0,0 @@ -package filter - -import ( - "bytes" - "io/ioutil" - "net/http" - - "github.com/Shugyousha/stasher/work" -) - -type HTTPFilter struct { - url string -} - -func NewHTTPFilter(url string) *HTTPFilter { - return &HTTPFilter{url: url} -} - -func (hf *HTTPFilter) Filter(w *work.Work) *work.Work { - resp, err := http.Post(hf.url, "application/json", bytes.NewReader(w.Data)) - if err != nil { - w.Err = err - return w - } - defer resp.Body.Close() - - filtered, err := ioutil.ReadAll(resp.Body) - if err != nil { - w.Err = err - return w - } - - w.Data = filtered - return w -} diff --git a/filter/http/http.go b/filter/http/http.go new file mode 100644 index 0000000..e3d447b --- /dev/null +++ b/filter/http/http.go @@ -0,0 +1,42 @@ +package http + +import ( + "bytes" + "io/ioutil" + "net/http" + + "github.com/Shugyousha/stasher/filter" + "github.com/Shugyousha/stasher/registry" + "github.com/Shugyousha/stasher/work" +) + +func init() { + registry.Filterregistry["http"] = New +} + +type HTTPFilter struct { + url string +} + +func New(kv map[string]string) filter.Filter { + url := kv["url"] + return &HTTPFilter{url: url} +} + +func (hf *HTTPFilter) Filter(w *work.Work) *work.Work { + resp, err := http.Post(hf.url, "application/json", bytes.NewReader(w.Data)) + if err != nil { + w.Err = err + return w + } + defer resp.Body.Close() + + filtered, err := ioutil.ReadAll(resp.Body) + if err != nil { + w.Err = err + return w + } + + w.Data = filtered + return w +} diff --git a/filter/interface.go b/filter/interface.go deleted file mode 100644 index 83d2dfe..0000000 --- a/filter/interface.go +++ /dev/null @@ -1,7 +0,0 @@ -package filter - -import "github.com/Shugyousha/stasher/work" - -type Filter interface { - Filter(*work.Work) *work.Work -} diff --git a/filter/str/string.go b/filter/str/string.go new file mode 100644 index 0000000..0ae407d --- /dev/null +++ b/filter/str/string.go @@ -0,0 +1,56 @@ +package str + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + + "github.com/Shugyousha/stasher/work" +) + +type StringFilter struct { + FilterFuncMap map[string]func(string) string +} + +func New(ffmap map[string]func(string) string) *StringFilter { + return &StringFilter{FilterFuncMap: ffmap} +} + +func (f *StringFilter) Filter(w *work.Work) *work.Work { + dec := json.NewDecoder(bytes.NewReader(w.Data)) + jm := make(map[string]string, 10) + + err := dec.Decode(&jm) + if err == io.EOF { + fmt.Printf("EOF jm: %v\n", jm) + return w + } + if err != nil { + fmt.Printf("Error when decoding JSON: %q\n", err) + w.Err = err + return w + } + + changed := false + for field, ff := range f.FilterFuncMap { + str, ok := jm[field] + if !ok { + continue + } + + jm[field] = ff(str) + 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 + } + } + + return w +} diff --git a/filter/string.go b/filter/string.go deleted file mode 100644 index cb44d75..0000000 --- a/filter/string.go +++ /dev/null @@ -1,56 +0,0 @@ -package filter - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - - "github.com/Shugyousha/stasher/work" -) - -type StringFilter struct { - FilterFuncMap map[string]func(string) string -} - -func NewStringFilter(ffmap map[string]func(string) string) *StringFilter { - return &StringFilter{FilterFuncMap: ffmap} -} - -func (f *StringFilter) Filter(w *work.Work) *work.Work { - dec := json.NewDecoder(bytes.NewReader(w.Data)) - jm := make(map[string]string, 10) - - err := dec.Decode(&jm) - if err == io.EOF { - fmt.Printf("EOF jm: %v\n", jm) - return w - } - if err != nil { - fmt.Printf("Error when decoding JSON: %q\n", err) - w.Err = err - return w - } - - changed := false - for field, ff := range f.FilterFuncMap { - str, ok := jm[field] - if !ok { - continue - } - - jm[field] = ff(str) - 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 - } - } - - return w -} -- cgit v1.2.3