 e788757b32
			
		
	
	
		e788757b32
		
			
		
	
	
	
	
		
			
			This makes the data more consistent and easier to use with other tools. For example, a third-party tool can iterate over `Issues` without having to make a preliminary null check.
		
			
				
	
	
		
			45 lines
		
	
	
		
			716 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			716 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package printers
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 
 | |
| 	"github.com/golangci/golangci-lint/pkg/logutils"
 | |
| 	"github.com/golangci/golangci-lint/pkg/report"
 | |
| 	"github.com/golangci/golangci-lint/pkg/result"
 | |
| )
 | |
| 
 | |
| type JSON struct {
 | |
| 	rd *report.Data
 | |
| }
 | |
| 
 | |
| func NewJSON(rd *report.Data) *JSON {
 | |
| 	return &JSON{
 | |
| 		rd: rd,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| type JSONResult struct {
 | |
| 	Issues []result.Issue
 | |
| 	Report *report.Data
 | |
| }
 | |
| 
 | |
| func (p JSON) Print(ctx context.Context, issues []result.Issue) error {
 | |
| 	res := JSONResult{
 | |
| 		Issues: issues,
 | |
| 		Report: p.rd,
 | |
| 	}
 | |
| 	if res.Issues == nil {
 | |
| 		res.Issues = []result.Issue{}
 | |
| 	}
 | |
| 
 | |
| 	outputJSON, err := json.Marshal(res)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	fmt.Fprint(logutils.StdOut, string(outputJSON))
 | |
| 	return nil
 | |
| }
 |