From af40a95e52ba89c0234c370d0aec3578d62ef696 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Tue, 29 Nov 2016 19:39:56 +0100 Subject: Initial Commit --- manager.go | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 manager.go diff --git a/manager.go b/manager.go new file mode 100644 index 0000000..0c933ec --- /dev/null +++ b/manager.go @@ -0,0 +1,113 @@ +package main + +import ( + "bufio" + "bytes" + "encoding/json" + "fmt" + "io" + "os" +) + +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(chan *Work) chan *Work +} + +type StringFilter struct { + retchan chan *Work +} + +type Output interface { + Output(chan *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} + } + }() + + return i.retchan +} + +func (f *StringFilter) Filter(ic chan *Work) chan *Work { + f.retchan = make(chan *Work, 100) + + go func() { + for w := range ic { + dec := json.NewDecoder(bytes.NewReader(w.data)) + + for { + tok, err := dec.Token() + if err == io.EOF { + break + } + if err != nil { + fmt.Printf("Error when reading JSON: %q\n", err) + os.Exit(1) + } + fmt.Printf("Token: %T %v\n", tok, tok) + } + + f.retchan <- w + } + }() + + return f.retchan +} + +func (o *StdoutOutput) Output(wc chan *Work) { + for w := range wc { + fmt.Printf("%s\n", w.data) + } +} + +func (m *Manager) Go() { + ic := m.Input.Start() + fc := m.Filter.Filter(ic) + m.Output.Output(fc) +} + +func main() { + m := Manager{ + Input: &StdinInput{}, + Filter: &StringFilter{}, + Output: &StdoutOutput{}, + } + + m.Go() +} -- cgit v1.2.3