You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.go 883B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package formatter
  2. import (
  3. "encoding/json"
  4. "github.com/mgechev/revive/lint"
  5. )
  6. // JSON is an implementation of the Formatter interface
  7. // which formats the errors to JSON.
  8. type JSON struct {
  9. Metadata lint.FormatterMetadata
  10. }
  11. // Name returns the name of the formatter
  12. func (f *JSON) Name() string {
  13. return "json"
  14. }
  15. // jsonObject defines a JSON object of an failure
  16. type jsonObject struct {
  17. Severity lint.Severity
  18. lint.Failure `json:",inline"`
  19. }
  20. // Format formats the failures gotten from the lint.
  21. func (f *JSON) Format(failures <-chan lint.Failure, config lint.Config) (string, error) {
  22. var slice []jsonObject
  23. for failure := range failures {
  24. obj := jsonObject{}
  25. obj.Severity = severity(config, failure)
  26. obj.Failure = failure
  27. slice = append(slice, obj)
  28. }
  29. result, err := json.Marshal(slice)
  30. if err != nil {
  31. return "", err
  32. }
  33. return string(result), err
  34. }