summaryrefslogtreecommitdiff
path: root/filter
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2016-12-09 21:12:18 +0100
committerSilvan Jegen <s.jegen@gmail.com>2016-12-09 21:12:18 +0100
commit26412f3077698dabcdbdf6a6e40276c56a6f74b5 (patch)
treec10ed9b5dbe3612da22247ec6c5afcd1879495eb /filter
parent70299d51aeb8dc53a15eb06d46d4d2419619fd47 (diff)
Reorganize files
Diffstat (limited to 'filter')
-rw-r--r--filter/string.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/filter/string.go b/filter/string.go
new file mode 100644
index 0000000..cb44d75
--- /dev/null
+++ b/filter/string.go
@@ -0,0 +1,56 @@
+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
+}