2017-08-29 19:47:29 +02:00
|
|
|
package formatter
|
2017-08-28 01:57:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2018-01-22 04:04:41 +02:00
|
|
|
"github.com/mgechev/revive/linter"
|
2017-08-28 01:57:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// JSONFormatter is an implementation of the Formatter interface
|
|
|
|
// which formats the errors to JSON.
|
|
|
|
type JSONFormatter struct {
|
2018-01-22 04:04:41 +02:00
|
|
|
Metadata linter.FormatterMetadata
|
2017-08-28 01:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format formats the failures gotten from the linter.
|
2018-01-25 01:41:40 +02:00
|
|
|
func (f *JSONFormatter) Format(failures <-chan linter.Failure, config linter.RulesConfig) (string, error) {
|
2018-01-24 04:19:06 +02:00
|
|
|
var slice []linter.Failure
|
|
|
|
for failure := range failures {
|
|
|
|
slice = append(slice, failure)
|
2017-08-28 01:57:16 +02:00
|
|
|
}
|
2018-01-24 04:19:06 +02:00
|
|
|
result, err := json.Marshal(slice)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(result), err
|
2017-08-28 01:57:16 +02:00
|
|
|
}
|