1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-10 03:17:11 +02:00
revive/formatter/json_formatter.go
2018-01-27 16:22:17 -08:00

32 lines
704 B
Go

package formatter
import (
"encoding/json"
"github.com/mgechev/revive/lint"
)
// JSONFormatter is an implementation of the Formatter interface
// which formats the errors to JSON.
type JSONFormatter struct {
Metadata lint.FormatterMetadata
}
// Name returns the name of the formatter
func (f *JSONFormatter) Name() string {
return "json"
}
// Format formats the failures gotten from the lint.
func (f *JSONFormatter) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
var slice []lint.Failure
for failure := range failures {
slice = append(slice, failure)
}
result, err := json.Marshal(slice)
if err != nil {
return "", err
}
return string(result), err
}