From fea7e02623b557b6a469048b19092cb7f0cb5a97 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 10 Jul 2016 13:05:23 -0700 Subject: [PATCH] Improved API --- context.go | 35 ++++++++++++++++++++--------------- goldsmith.go | 6 +----- loader.go | 4 ++-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/context.go b/context.go index 3bdbdfa..a29e9c9 100644 --- a/context.go +++ b/context.go @@ -40,36 +40,41 @@ type context struct { func (ctx *context) chain() { defer close(ctx.output) - initializer, _ := ctx.plug.(Initializer) - accepter, _ := ctx.plug.(Accepter) - processor, _ := ctx.plug.(Processor) - finalizer, _ := ctx.plug.(Finalizer) - - if initializer != nil { - if err := initializer.Initialize(ctx); err != nil { + var filters []string + if initializer, ok := ctx.plug.(Initializer); ok { + var err error + filters, err = initializer.Initialize(ctx) + if err != nil { ctx.gs.fault(nil, err) return } } if ctx.input != nil { + processor, _ := ctx.plug.(Processor) + var wg sync.WaitGroup for i := 0; i < runtime.NumCPU(); i++ { wg.Add(1) go func() { defer wg.Done() for f := range ctx.input { - accept := processor != nil && (accepter == nil || accepter.Accept(ctx, f)) - if accept && len(ctx.filters) > 0 { - accept = false - for _, filter := range ctx.filters { - if match, _ := doublestar.PathMatch(filter, f.Path()); match { - accept = true - break + accept := processor != nil + matcher := func(patterns []string) { + if accept && len(patterns) > 0 { + accept = false + for _, pattern := range patterns { + if match, _ := doublestar.PathMatch(pattern, f.Path()); match { + accept = true + break + } } } } + matcher(filters) + matcher(ctx.filters) + if accept { if _, err := f.Seek(0, os.SEEK_SET); err != nil { ctx.gs.fault(f, err) @@ -86,7 +91,7 @@ func (ctx *context) chain() { wg.Wait() } - if finalizer != nil { + if finalizer, ok := ctx.plug.(Finalizer); ok { if err := finalizer.Finalize(ctx); err != nil { ctx.gs.fault(nil, err) } diff --git a/goldsmith.go b/goldsmith.go index 897752e..b61b865 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -106,11 +106,7 @@ func (e Error) Error() string { } type Initializer interface { - Initialize(ctx Context) error -} - -type Accepter interface { - Accept(ctx Context, f File) bool + Initialize(ctx Context) ([]string, error) } type Processor interface { diff --git a/loader.go b/loader.go index e38dc84..9aae5ac 100644 --- a/loader.go +++ b/loader.go @@ -26,7 +26,7 @@ import "path/filepath" type loader struct{} -func (*loader) Initialize(ctx Context) error { +func (*loader) Initialize(ctx Context) ([]string, error) { infos := make(chan fileInfo) go scanDir(ctx.SrcDir(), infos) @@ -48,5 +48,5 @@ func (*loader) Initialize(ctx Context) error { ctx.DispatchFile(f) } - return nil + return nil, nil }