Simplification

This commit is contained in:
Alex Yatskov 2015-10-31 14:12:03 +09:00
parent ef1d7d3621
commit b343397ec5
2 changed files with 38 additions and 5 deletions

View File

@ -27,6 +27,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"sync"
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
) )
@ -85,9 +86,37 @@ func (gs *goldsmith) NewFile(relPath string) File {
return &file{relPath: relPath} return &file{relPath: relPath}
} }
func (gs *goldsmith) Apply(p Processor) Goldsmith { func (gs *goldsmith) applySingle(proc ProcessorSingle) {
s := gs.makeStage() s := gs.makeStage()
go p.Process(gs, s.input, s.output)
var wg sync.WaitGroup
for file := range s.input {
wg.Add(1)
go func(f File) {
s.output <- proc.ProcessSingle(gs, f)
wg.Done()
}(file)
}
go func() {
wg.Wait()
close(s.output)
}()
}
func (gs *goldsmith) applyMultiple(proc ProcessorMultiple) {
s := gs.makeStage()
proc.ProcessMultiple(gs, s.input, s.output)
}
func (gs *goldsmith) Apply(proc interface{}) Goldsmith {
switch p := proc.(type) {
case ProcessorSingle:
gs.applySingle(p)
case ProcessorMultiple:
gs.applyMultiple(p)
}
return gs return gs
} }

View File

@ -41,11 +41,15 @@ type File interface {
Data() *bytes.Buffer Data() *bytes.Buffer
} }
type Processor interface { type ProcessorMultiple interface {
Process(ctx Context, input, output chan File) ProcessMultiple(ctx Context, input, output chan File)
}
type ProcessorSingle interface {
ProcessSingle(ctx Context, file File) File
} }
type Goldsmith interface { type Goldsmith interface {
Apply(p Processor) Goldsmith Apply(proc interface{}) Goldsmith
Complete(path string) []File Complete(path string) []File
} }