simplify filtering

This commit is contained in:
Alex Yatskov 2018-12-09 12:45:06 -08:00
parent 989bb18482
commit c8e3e98432
3 changed files with 17 additions and 8 deletions

View File

@ -67,9 +67,9 @@ func (ctx *Context) step() {
defer close(ctx.outputFiles) defer close(ctx.outputFiles)
var err error var err error
var filters []Filter var filter Filter
if initializer, ok := ctx.plugin.(Initializer); ok { if initializer, ok := ctx.plugin.(Initializer); ok {
filters, err = initializer.Initialize(ctx) filter, err = initializer.Initialize(ctx)
if err != nil { if err != nil {
ctx.goldsmith.fault(ctx.plugin.Name(), nil, err) ctx.goldsmith.fault(ctx.plugin.Name(), nil, err)
return return
@ -86,9 +86,16 @@ func (ctx *Context) step() {
defer wg.Done() defer wg.Done()
for inputFile := range ctx.inputFiles { for inputFile := range ctx.inputFiles {
accept := processor != nil accept := processor != nil
for _, filter := range append(ctx.fileFilters, filters...) {
if accept, err = filter.Accept(ctx, inputFile); err != nil { var fileFilters []Filter
ctx.goldsmith.fault(filter.Name(), inputFile, err) fileFilters = append(fileFilters, ctx.fileFilters...)
if filter != nil {
fileFilters = append(fileFilters, filter)
}
for _, fileFilter := range fileFilters {
if accept, err = fileFilter.Accept(ctx, inputFile); err != nil {
ctx.goldsmith.fault(fileFilter.Name(), inputFile, err)
return return
} }
if !accept { if !accept {

View File

@ -1,7 +1,7 @@
package goldsmith package goldsmith
type Initializer interface { type Initializer interface {
Initialize(ctx *Context) ([]Filter, error) Initialize(ctx *Context) (Filter, error)
} }
type Processor interface { type Processor interface {

View File

@ -2,13 +2,15 @@ package goldsmith
import "path/filepath" import "path/filepath"
type loader struct{} type loader struct {
Initializer
}
func (*loader) Name() string { func (*loader) Name() string {
return "loader" return "loader"
} }
func (*loader) Initialize(ctx *Context) ([]Filter, error) { func (*loader) Initialize(ctx *Context) (Filter, error) {
infos := make(chan fileInfo) infos := make(chan fileInfo)
go scanDir(ctx.goldsmith.sourceDir, infos) go scanDir(ctx.goldsmith.sourceDir, infos)