2017-08-29 10:47:29 -07:00
|
|
|
package formatter
|
2017-08-27 16:57:16 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2018-01-24 15:44:03 -08:00
|
|
|
"github.com/mgechev/revive/lint"
|
2017-08-27 16:57:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// JSONFormatter is an implementation of the Formatter interface
|
|
|
|
// which formats the errors to JSON.
|
|
|
|
type JSONFormatter struct {
|
2018-01-24 15:44:03 -08:00
|
|
|
Metadata lint.FormatterMetadata
|
2017-08-27 16:57:16 -07:00
|
|
|
}
|
|
|
|
|
2018-01-24 15:44:03 -08:00
|
|
|
// 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
|
2018-01-23 18:19:06 -08:00
|
|
|
for failure := range failures {
|
|
|
|
slice = append(slice, failure)
|
2017-08-27 16:57:16 -07:00
|
|
|
}
|
2018-01-23 18:19:06 -08:00
|
|
|
result, err := json.Marshal(slice)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(result), err
|
2017-08-27 16:57:16 -07:00
|
|
|
}
|