Simplification
This commit is contained in:
parent
ef1d7d3621
commit
b343397ec5
33
goldsmith.go
33
goldsmith.go
@ -27,6 +27,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/bmatcuk/doublestar"
|
||||
)
|
||||
@ -85,9 +86,37 @@ func (gs *goldsmith) NewFile(relPath string) File {
|
||||
return &file{relPath: relPath}
|
||||
}
|
||||
|
||||
func (gs *goldsmith) Apply(p Processor) Goldsmith {
|
||||
func (gs *goldsmith) applySingle(proc ProcessorSingle) {
|
||||
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
|
||||
}
|
||||
|
||||
|
10
types.go
10
types.go
@ -41,11 +41,15 @@ type File interface {
|
||||
Data() *bytes.Buffer
|
||||
}
|
||||
|
||||
type Processor interface {
|
||||
Process(ctx Context, input, output chan File)
|
||||
type ProcessorMultiple interface {
|
||||
ProcessMultiple(ctx Context, input, output chan File)
|
||||
}
|
||||
|
||||
type ProcessorSingle interface {
|
||||
ProcessSingle(ctx Context, file File) File
|
||||
}
|
||||
|
||||
type Goldsmith interface {
|
||||
Apply(p Processor) Goldsmith
|
||||
Apply(proc interface{}) Goldsmith
|
||||
Complete(path string) []File
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user