-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
rdjsonl.go
39 lines (32 loc) · 965 Bytes
/
rdjsonl.go
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
package parser
import (
"bufio"
"fmt"
"io"
"google.golang.org/protobuf/encoding/protojson"
"github.com/reviewdog/reviewdog/proto/rdf"
)
var _ Parser = &RDJSONLParser{}
// RDJSONLParser is parser for rdjsonl format.
type RDJSONLParser struct{}
// NewRDJSONLParser returns a new RDJSONParser.
func NewRDJSONLParser() *RDJSONLParser {
return &RDJSONLParser{}
}
// Parse parses rdjson (JSONL of Diagnostic).
func (p *RDJSONLParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
var results []*rdf.Diagnostic
s := bufio.NewScanner(r)
for s.Scan() {
d := new(rdf.Diagnostic)
if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(s.Bytes(), d); err != nil {
return nil, fmt.Errorf("failed to unmarshal rdjsonl (Diagnostic): %w", err)
}
if d.GetOriginalOutput() == "" {
// TODO(haya14busa): Refactor not to fill in original output.
d.OriginalOutput = s.Text()
}
results = append(results, d)
}
return results, nil
}