Skip to content

Commit

Permalink
Merge pull request #1771 from reviewdog/rdf-ignore-unknown-field
Browse files Browse the repository at this point in the history
rdf: Discard unknown fields when unmarshalling JSON data
  • Loading branch information
haya14busa authored Jun 13, 2024
2 parents 4527845 + 6d8afbc commit 8031379
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion parser/rdjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (p *RDJSONParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
return nil, err
}
var dr rdf.DiagnosticResult
if err := protojson.Unmarshal(b, &dr); err != nil {
if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(b, &dr); err != nil {
return nil, fmt.Errorf("failed to unmarshal rdjson (DiagnosticResult): %w", err)
}
for _, d := range dr.Diagnostics {
Expand Down
3 changes: 2 additions & 1 deletion parser/rdjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func ExampleRDJSONParser() {
}
}
},
"severity": 1
"severity": 1,
"unknown_field": "this field will be ignored"
}
]
}`
Expand Down
2 changes: 1 addition & 1 deletion parser/rdjsonl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (p *RDJSONLParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
s := bufio.NewScanner(r)
for s.Scan() {
d := new(rdf.Diagnostic)
if err := protojson.Unmarshal(s.Bytes(), d); err != nil {
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() == "" {
Expand Down
2 changes: 1 addition & 1 deletion parser/rdjsonl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestRDJSONLParser(t *testing.T) {
{"source":{"name":"ineffassign"},"message":"ineffectual assignment to 'x'","location":{"path":"testdata/main.go","range":{"start":{"line":12,"column":2}}}}
{"source":{"name":"govet"},"message":"printf: Sprintf format %d reads arg #1, but call has 0 args","location":{"path":"testdata/main.go","range":{"start":{"line":13,"column":2}}}}
{"source":{"name":"severity-test"},"message":"severity test (string)","location":{"path":"testdata/main.go","range":{"start":{"line":24,"column":6}}}, "severity": "WARNING"}
{"source":{"name":"severity-test"},"message":"severity test (number)","location":{"path":"testdata/main.go","range":{"start":{"line":24,"column":6}}}, "severity": "WARNING"}`
{"source":{"name":"severity-test"},"message":"severity test (number)","location":{"path":"testdata/main.go","range":{"start":{"line":24,"column":6}}}, "severity": "WARNING", "unknown_field": "this field will be ignored"}`
sampleLines := strings.Split(sample, "\n")
p := NewRDJSONLParser()
diagnostics, err := p.Parse(strings.NewReader(sample))
Expand Down

0 comments on commit 8031379

Please sign in to comment.