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:
parent
1e0238d20a
commit
095a25d375
@ -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:
|
||||
- `default` - will output the failures the same way that `golint` does.
|
||||
- `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.
|
||||
- `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.
|
||||
|
||||
|
@ -61,6 +61,7 @@ var allFormatters = []lint.Formatter{
|
||||
&formatter.Stylish{},
|
||||
&formatter.Friendly{},
|
||||
&formatter.JSON{},
|
||||
&formatter.NDJSON{},
|
||||
&formatter.Default{},
|
||||
}
|
||||
|
||||
|
34
formatter/ndjson.go
Normal file
34
formatter/ndjson.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user