1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00

Add formatter NDJSON (#34)

* Add formatter json-stream

* use new json struct in json-stream

* rename json-stream formatter to ndjson

* add ndjson formatter to readme
This commit is contained in:
Markus Wiegand 2018-07-14 00:01:27 +02:00 committed by Minko Gechev
parent 1e0238d20a
commit 095a25d375
3 changed files with 36 additions and 0 deletions

View File

@ -91,6 +91,7 @@ go get -u github.com/mgechev/revive
- `-formatter [NAME]` - formatter to be used for the output. The currently available formatters are: - `-formatter [NAME]` - formatter to be used for the output. The currently available formatters are:
- `default` - will output the failures the same way that `golint` does. - `default` - will output the failures the same way that `golint` does.
- `json` - outputs the failures in JSON format. - `json` - outputs the failures in JSON format.
- `ndjson` - outputs the failures as stream in newline delimited JSON (NDJSON) format.
- `friendly` - outputs the failures when found. Shows summary of all the failures. - `friendly` - outputs the failures when found. Shows summary of all the failures.
- `stylish` - formats the failures in a table. Keep in mind that it doesn't stream the output so it might be perceived as slower compared to others. - `stylish` - formats the failures in a table. Keep in mind that it doesn't stream the output so it might be perceived as slower compared to others.

View File

@ -61,6 +61,7 @@ var allFormatters = []lint.Formatter{
&formatter.Stylish{}, &formatter.Stylish{},
&formatter.Friendly{}, &formatter.Friendly{},
&formatter.JSON{}, &formatter.JSON{},
&formatter.NDJSON{},
&formatter.Default{}, &formatter.Default{},
} }

34
formatter/ndjson.go Normal file
View File

@ -0,0 +1,34 @@
package formatter
import (
"encoding/json"
"os"
"github.com/mgechev/revive/lint"
)
// NDJSON is an implementation of the Formatter interface
// which formats the errors to NDJSON stream.
type NDJSON struct {
Metadata lint.FormatterMetadata
}
// Name returns the name of the formatter
func (f *NDJSON) Name() string {
return "ndjson"
}
// Format formats the failures gotten from the lint.
func (f *NDJSON) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
enc := json.NewEncoder(os.Stdout)
for failure := range failures {
obj := jsonObject{}
obj.Severity = severity(config, failure)
obj.Failure = failure
err := enc.Encode(obj)
if err != nil {
return "", err
}
}
return "", nil
}