Merge pull request #83 from golangci/feature/improve-json-output

Write JSON output more compactly and output object, not array
This commit is contained in:
Isaev Denis 2018-06-11 11:46:43 +03:00 committed by GitHub
commit 89921943e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 7 deletions

View File

@ -41,7 +41,7 @@ func (d Dupl) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue,
Filename: i.From.Filename(),
Line: i.From.LineStart(),
},
LineRange: result.Range{
LineRange: &result.Range{
From: i.From.LineStart(),
To: i.From.LineEnd(),
},

View File

@ -41,9 +41,10 @@ func (lint Gas) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issu
res := make([]result.Issue, 0, len(issues))
for _, i := range issues {
text := fmt.Sprintf("%s: %s", i.RuleID, i.What) // TODO: use severity and confidence
var r result.Range
var r *result.Range
line, err := strconv.Atoi(i.Line)
if err != nil {
r = &result.Range{}
if n, rerr := fmt.Sscanf(i.Line, "%d-%d", &r.From, &r.To); rerr != nil || n != 2 {
logutils.HiddenWarnf("Can't convert gas line number %q of %v to int: %s", i.Line, i, err)
continue

View File

@ -14,15 +14,25 @@ func NewJSON() *JSON {
return &JSON{}
}
type JSONResult struct {
Issues []result.Issue
}
func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
var allIssues []result.Issue
allIssues := []result.Issue{}
for i := range issues {
allIssues = append(allIssues, i)
}
outputJSON, err := json.Marshal(allIssues)
res := JSONResult{
Issues: allIssues,
}
outputJSON, err := json.Marshal(res)
if err != nil {
return false, err
}
fmt.Fprint(StdOut, string(outputJSON))
return len(allIssues) != 0, nil
}

View File

@ -11,8 +11,8 @@ type Issue struct {
Text string
Pos token.Position
LineRange Range
HunkPos int
LineRange *Range `json:",omitempty"`
HunkPos int `json:",omitempty"`
}
func (i Issue) FilePath() string {
@ -24,6 +24,13 @@ func (i Issue) Line() int {
}
func (i Issue) GetLineRange() Range {
if i.LineRange == nil {
return Range{
From: i.Line(),
To: i.Line(),
}
}
if i.LineRange.From == 0 {
return Range{
From: i.Line(),
@ -31,5 +38,5 @@ func (i Issue) GetLineRange() Range {
}
}
return i.LineRange
return *i.LineRange
}