github.com/vipcoin-gold/reviewdog@v1.0.2/parser/errorformat.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/reviewdog/errorformat"
     9  
    10  	"github.com/vipcoin-gold/reviewdog/proto/rdf"
    11  )
    12  
    13  var _ Parser = &ErrorformatParser{}
    14  
    15  // ErrorformatParser is errorformat parser.
    16  type ErrorformatParser struct {
    17  	efm *errorformat.Errorformat
    18  }
    19  
    20  // NewErrorformatParser returns a new ErrorformatParser.
    21  func NewErrorformatParser(efm *errorformat.Errorformat) *ErrorformatParser {
    22  	return &ErrorformatParser{efm: efm}
    23  }
    24  
    25  // NewErrorformatParserString returns a new ErrorformatParser from errorformat
    26  // in string representation.
    27  func NewErrorformatParserString(efms []string) (*ErrorformatParser, error) {
    28  	efm, err := errorformat.NewErrorformat(efms)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return NewErrorformatParser(efm), nil
    33  }
    34  
    35  func (p *ErrorformatParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
    36  	s := p.efm.NewScanner(r)
    37  	var ds []*rdf.Diagnostic
    38  	for s.Scan() {
    39  		e := s.Entry()
    40  		if e.Valid {
    41  			d := &rdf.Diagnostic{
    42  				Location: &rdf.Location{
    43  					Path: e.Filename,
    44  					Range: &rdf.Range{
    45  						Start: &rdf.Position{
    46  							Line:   int32(e.Lnum),
    47  							Column: int32(e.Col),
    48  						},
    49  					},
    50  				},
    51  				Message:        e.Text,
    52  				Severity:       severity(string(e.Type)),
    53  				OriginalOutput: strings.Join(e.Lines, "\n"),
    54  			}
    55  			if e.Nr != 0 {
    56  				d.Code = &rdf.Code{Value: fmt.Sprintf("%d", e.Nr)}
    57  			}
    58  			ds = append(ds, d)
    59  		}
    60  	}
    61  	return ds, nil
    62  }