1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"sync"
)
type Manager struct {
Input Input
Filter Filter
Output Output
}
type Input interface {
Start() chan *Work
}
type StdinInput struct {
retchan chan *Work
}
type Filter interface {
Filter(*Work) *Work
}
type StringFilter struct {
retchan chan *Work
FilterFuncMap map[string]func(string) string
}
type Output interface {
Output(*Work)
}
type StdoutOutput struct {
}
type Work struct {
data []byte
err error
}
func (i *StdinInput) Start() chan *Work {
i.retchan = make(chan *Work, 100)
r := bufio.NewReader(os.Stdin)
go func() {
for {
bs, err := r.ReadBytes(byte('\n'))
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("Error when reading input from Stdin: %q", err)
os.Exit(1)
}
i.retchan <- &Work{data: bs}
}
close(i.retchan)
}()
return i.retchan
}
func (f *StringFilter) Filter(w *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
}
func (o *StdoutOutput) Output(w *Work) {
fmt.Printf("%s\n", w.data)
}
func (m *Manager) Run() {
var wg sync.WaitGroup
ic := m.Input.Start()
for w := range ic {
wg.Add(1)
go func(w *Work) {
nw := m.Filter.Filter(w)
m.Output.Output(nw)
wg.Done()
}(w)
}
wg.Wait()
}
func main() {
ffmap := make(map[string]func(string) string, 10)
ffmap["F"] = func(s string) string { return strings.ToUpper(s) }
m := Manager{
Input: &StdinInput{},
Filter: &StringFilter{FilterFuncMap: ffmap},
Output: &StdoutOutput{},
}
m.Run()
}
|