File: parser.go

package info (click to toggle)
golang-github-mightyguava-jl 0.1.0%2Bgit20220705%2B8771236337c6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,112 kB
  • sloc: makefile: 5
file content (45 lines) | stat: -rw-r--r-- 700 bytes parent folder | download
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
package jl

import (
	"bufio"
	"encoding/json"
	"io"
)

type Parser struct {
	r       io.Reader
	scan    *bufio.Scanner
	printer EntryPrinter
}

func NewParser(r io.Reader, h EntryPrinter) *Parser {
	return &Parser{
		r:       r,
		scan:    bufio.NewScanner(r),
		printer: h,
	}
}

func (p *Parser) Consume() error {
	s := p.scan
	for s.Scan() {
		raw := s.Bytes()
		var partials map[string]json.RawMessage
		_ = json.Unmarshal(raw, &partials)
		message := &Entry{
			Partials:    partials,
			Raw:         raw,
		}
		p.printer.Print(message)
	}
	return p.scan.Err()
}

type EntryPrinter interface {
	Print(*Entry)
}

type Entry struct {
	Partials    map[string]json.RawMessage
	Raw         []byte
}