Skip to content

Commit

Permalink
add NewParser method to create parser from option
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Dec 11, 2016
1 parent 9d803d4 commit 1f02bcb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
28 changes: 2 additions & 26 deletions cmd/reviewdog/main.go
Expand Up @@ -106,9 +106,9 @@ func run(r io.Reader, w io.Writer, opt *option) error {
return runList(w)
}

p, err := parser(opt)
p, err := reviewdog.NewParser(&reviewdog.ParserOpt{FormatName: opt.f, Errorformat: opt.efms})
if err != nil {
return err
return fmt.Errorf("fail to create parser. use either -f or -efm: %v", err)
}

var cs reviewdog.CommentService
Expand Down Expand Up @@ -184,30 +184,6 @@ func sortedFmts(fs fmts.Fmts) []*fmts.Fmt {
return r
}

func parser(opt *option) (reviewdog.Parser, error) {
if opt.f == "checkstyle" {
return reviewdog.NewCheckStyleParser(), nil
}

// use defined errorformat
if opt.f != "" {
if len(opt.efms) > 0 {
return nil, errors.New("you cannot specify both -f and -efm at the same time")
}
efm, ok := fmts.DefinedFmts()[opt.f]
if !ok {
return nil, fmt.Errorf("%q is not supported. Use -efm or consider to add new errrorformat to https://github.com/haya14busa/errorformat", opt.f)
}
opt.efms = efm.Errorformat
}

if len(opt.efms) == 0 {
return nil, errors.New("cannot parse input. Please specify -f or -efm")
}

return reviewdog.NewErrorformatParserString(opt.efms)
}

// FlashCommentService is CommentService which uses Flash method to post comment.
type FlashCommentService interface {
reviewdog.CommentService
Expand Down
34 changes: 34 additions & 0 deletions parser.go
Expand Up @@ -2,12 +2,46 @@ package reviewdog

import (
"encoding/xml"
"errors"
"fmt"
"io"

"github.com/haya14busa/errorformat"
"github.com/haya14busa/errorformat/fmts"
)

// ParserOpt represents option to create Parser. Either FormatName or
// Errorformat should be specified.
type ParserOpt struct {
FormatName string
Errorformat []string
}

// NewParser returns Parser based on ParserOpt.
func NewParser(opt *ParserOpt) (Parser, error) {
name := opt.FormatName

if name != "" && len(opt.Errorformat) > 0 {
return nil, errors.New("you cannot specify both format name and errorformat at the same time")
}

if name == "checkstyle" {
return NewCheckStyleParser(), nil
}
// use defined errorformat
if name != "" {
efm, ok := fmts.DefinedFmts()[name]
if !ok {
return nil, fmt.Errorf("%q is not supported. consider to add new errrorformat to https://github.com/haya14busa/errorformat", name)
}
opt.Errorformat = efm.Errorformat
}
if len(opt.Errorformat) == 0 {
return nil, errors.New("errorformat is empty")
}
return NewErrorformatParserString(opt.Errorformat)
}

var _ Parser = &ErrorformatParser{}

// ErrorformatParser is errorformat parser.
Expand Down
58 changes: 58 additions & 0 deletions parser_test.go
@@ -1,10 +1,68 @@
package reviewdog

import (
"reflect"
"strings"
"testing"
)

func TestNewParser(t *testing.T) {
tests := []struct {
in *ParserOpt
typ Parser
wantErr bool
}{
{
in: &ParserOpt{
FormatName: "checkstyle",
},
typ: &CheckStyleParser{},
},
{
in: &ParserOpt{
FormatName: "golint",
},
typ: &ErrorformatParser{},
},
{
in: &ParserOpt{
Errorformat: []string{`%f:%l:%c:%m`},
},
typ: &ErrorformatParser{},
},
{ // empty
in: &ParserOpt{},
wantErr: true,
},
{ // both
in: &ParserOpt{
FormatName: "checkstyle",
Errorformat: []string{`%f:%l:%c:%m`},
},
wantErr: true,
},
{ // unsupported
in: &ParserOpt{
FormatName: "unsupported format",
},
wantErr: true,
},
}
for _, tt := range tests {
p, err := NewParser(tt.in)
if tt.wantErr && err != nil {
continue
}
if err != nil {
t.Error(err)
continue
}
if got, want := reflect.TypeOf(p), reflect.TypeOf(tt.typ); got != want {
t.Errorf("typ: got %v, want %v", got, want)
}
}
}

func TestNewErrorformatParserString(t *testing.T) {
in := []string{`%f:%l:%c:%m`, `%-G%.%#`}

Expand Down

0 comments on commit 1f02bcb

Please sign in to comment.