1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/formatter/ndjson.go
WillAbides cb72bd880d
formatters return output (#921)
Some of the formatters were writing directly to stdout instead of returning the output. That made them more difficult to use them with revivelib. This PR updates those formatters to write to a buffer and return the resulting string.
2023-10-29 08:05:08 +01:00

36 lines
759 B
Go

package formatter
import (
"bytes"
"encoding/json"
"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 (*NDJSON) Name() string {
return "ndjson"
}
// Format formats the failures gotten from the lint.
func (*NDJSON) Format(failures <-chan lint.Failure, config lint.Config) (string, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
for failure := range failures {
obj := jsonObject{}
obj.Severity = severity(config, failure)
obj.Failure = failure
err := enc.Encode(obj)
if err != nil {
return "", err
}
}
return buf.String(), nil
}