github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/json.go (about)

     1  package printers
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  
     7  	"github.com/vanstinator/golangci-lint/pkg/report"
     8  	"github.com/vanstinator/golangci-lint/pkg/result"
     9  )
    10  
    11  type JSON struct {
    12  	rd *report.Data
    13  	w  io.Writer
    14  }
    15  
    16  func NewJSON(rd *report.Data, w io.Writer) *JSON {
    17  	return &JSON{
    18  		rd: rd,
    19  		w:  w,
    20  	}
    21  }
    22  
    23  type JSONResult struct {
    24  	Issues []result.Issue
    25  	Report *report.Data
    26  }
    27  
    28  func (p JSON) Print(issues []result.Issue) error {
    29  	res := JSONResult{
    30  		Issues: issues,
    31  		Report: p.rd,
    32  	}
    33  	if res.Issues == nil {
    34  		res.Issues = []result.Issue{}
    35  	}
    36  
    37  	return json.NewEncoder(p.w).Encode(res)
    38  }