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