Improved API

This commit is contained in:
Alex Yatskov 2016-07-10 13:05:23 -07:00
parent 3413e5a45f
commit fea7e02623
3 changed files with 23 additions and 22 deletions

View File

@ -40,36 +40,41 @@ type context struct {
func (ctx *context) chain() { func (ctx *context) chain() {
defer close(ctx.output) defer close(ctx.output)
initializer, _ := ctx.plug.(Initializer) var filters []string
accepter, _ := ctx.plug.(Accepter) if initializer, ok := ctx.plug.(Initializer); ok {
processor, _ := ctx.plug.(Processor) var err error
finalizer, _ := ctx.plug.(Finalizer) filters, err = initializer.Initialize(ctx)
if err != nil {
if initializer != nil {
if err := initializer.Initialize(ctx); err != nil {
ctx.gs.fault(nil, err) ctx.gs.fault(nil, err)
return return
} }
} }
if ctx.input != nil { if ctx.input != nil {
processor, _ := ctx.plug.(Processor)
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
for f := range ctx.input { for f := range ctx.input {
accept := processor != nil && (accepter == nil || accepter.Accept(ctx, f)) accept := processor != nil
if accept && len(ctx.filters) > 0 { matcher := func(patterns []string) {
accept = false if accept && len(patterns) > 0 {
for _, filter := range ctx.filters { accept = false
if match, _ := doublestar.PathMatch(filter, f.Path()); match { for _, pattern := range patterns {
accept = true if match, _ := doublestar.PathMatch(pattern, f.Path()); match {
break accept = true
break
}
} }
} }
} }
matcher(filters)
matcher(ctx.filters)
if accept { if accept {
if _, err := f.Seek(0, os.SEEK_SET); err != nil { if _, err := f.Seek(0, os.SEEK_SET); err != nil {
ctx.gs.fault(f, err) ctx.gs.fault(f, err)
@ -86,7 +91,7 @@ func (ctx *context) chain() {
wg.Wait() wg.Wait()
} }
if finalizer != nil { if finalizer, ok := ctx.plug.(Finalizer); ok {
if err := finalizer.Finalize(ctx); err != nil { if err := finalizer.Finalize(ctx); err != nil {
ctx.gs.fault(nil, err) ctx.gs.fault(nil, err)
} }

View File

@ -106,11 +106,7 @@ func (e Error) Error() string {
} }
type Initializer interface { type Initializer interface {
Initialize(ctx Context) error Initialize(ctx Context) ([]string, error)
}
type Accepter interface {
Accept(ctx Context, f File) bool
} }
type Processor interface { type Processor interface {

View File

@ -26,7 +26,7 @@ import "path/filepath"
type loader struct{} type loader struct{}
func (*loader) Initialize(ctx Context) error { func (*loader) Initialize(ctx Context) ([]string, error) {
infos := make(chan fileInfo) infos := make(chan fileInfo)
go scanDir(ctx.SrcDir(), infos) go scanDir(ctx.SrcDir(), infos)
@ -48,5 +48,5 @@ func (*loader) Initialize(ctx Context) error {
ctx.DispatchFile(f) ctx.DispatchFile(f)
} }
return nil return nil, nil
} }