| 1 | package jl |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | ) |
| 8 | |
| 9 | type Parser struct { |
| 10 | r io.Reader |
| 11 | scan *bufio.Scanner |
| 12 | printer EntryPrinter |
| 13 | } |
| 14 | |
| 15 | func NewParser(r io.Reader, h EntryPrinter) *Parser { |
| 16 | return &Parser{ |
| 17 | r: r, |
| 18 | scan: bufio.NewScanner(r), |
| 19 | printer: h, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func (p *Parser) Consume() error { |
| 24 | s := p.scan |
| 25 | for s.Scan() { |
| 26 | raw := s.Bytes() |
| 27 | var partials map[string]json.RawMessage |
| 28 | _ = json.Unmarshal(raw, &partials) |
| 29 | message := &Entry{ |
| 30 | Partials: partials, |
| 31 | Raw: raw, |
| 32 | } |
| 33 | p.printer.Print(message) |
| 34 | } |
| 35 | return p.scan.Err() |
| 36 | } |
| 37 | |
| 38 | type EntryPrinter interface { |
| 39 | Print(*Entry) |
| 40 | } |
| 41 | |
| 42 | type Entry struct { |
| 43 | Partials map[string]json.RawMessage |
| 44 | Raw []byte |
| 45 | } |