1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/formatter/ndjson.go
Markus Wiegand 095a25d375 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
2018-07-13 15:01:27 -07:00

35 lines
738 B
Go

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
}