1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/formatter/json.go

41 lines
879 B
Go
Raw Normal View History

2017-08-29 19:47:29 +02:00
package formatter
2017-08-28 01:57:16 +02:00
import (
"encoding/json"
2018-01-25 01:44:03 +02:00
"github.com/mgechev/revive/lint"
2017-08-28 01:57:16 +02:00
)
2018-01-28 03:01:18 +02:00
// JSON is an implementation of the Formatter interface
2017-08-28 01:57:16 +02:00
// which formats the errors to JSON.
2018-01-28 03:01:18 +02:00
type JSON struct {
2018-01-25 01:44:03 +02:00
Metadata lint.FormatterMetadata
2017-08-28 01:57:16 +02:00
}
2018-01-28 02:22:17 +02:00
// Name returns the name of the formatter
2022-04-10 11:55:13 +02:00
func (*JSON) Name() string {
2018-01-28 02:22:17 +02:00
return "json"
}
// jsonObject defines a JSON object of an failure
type jsonObject struct {
Severity lint.Severity
lint.Failure `json:",inline"`
}
2018-01-25 01:44:03 +02:00
// Format formats the failures gotten from the lint.
2022-04-10 11:55:13 +02:00
func (*JSON) Format(failures <-chan lint.Failure, config lint.Config) (string, error) {
var slice []jsonObject
2018-01-24 04:19:06 +02:00
for failure := range failures {
obj := jsonObject{}
obj.Severity = severity(config, failure)
obj.Failure = failure
slice = append(slice, obj)
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
}