Updating core

This commit is contained in:
Alex Yatskov 2015-11-01 16:41:56 +09:00
parent 2c2bda50b2
commit fa571ba089
2 changed files with 34 additions and 18 deletions

View File

@ -70,8 +70,8 @@ func (gs *goldsmith) scan(srcDir string) {
var f *os.File var f *os.File
if f, file.Err = os.Open(match); file.Err == nil { if f, file.Err = os.Open(match); file.Err == nil {
defer f.Close()
_, file.Err = file.Buff.ReadFrom(f) _, file.Err = file.Buff.ReadFrom(f)
f.Close()
} }
s.output <- file s.output <- file
@ -86,40 +86,51 @@ func (gs *goldsmith) makeStage() stage {
return s return s
} }
func (gs *goldsmith) chainSingle(ts ChainerSingle) { func (gs *goldsmith) chainSingle(s stage, cs ChainerSingle) {
s := gs.makeStage() defer close(s.output)
var wg sync.WaitGroup var wg sync.WaitGroup
for file := range s.input { for file := range s.input {
wg.Add(1) wg.Add(1)
go func(f File) { go func(f File) {
s.output <- ts.ChainSingle(f) defer wg.Done()
wg.Done() if f.Err == nil {
s.output <- cs.ChainSingle(f)
} else {
s.output <- f
}
}(file) }(file)
} }
go func() { wg.Wait()
wg.Wait()
close(s.output)
}()
} }
func (gs *goldsmith) chainMultiple(tm ChainerMultiple) { func (gs *goldsmith) chainMultiple(s stage, cm ChainerMultiple) {
s := gs.makeStage() filtered := make(chan File)
tm.ChainMultiple(s.input, s.output) defer close(filtered)
go cm.ChainMultiple(filtered, s.output)
for file := range s.input {
if file.Err == nil {
filtered <- file
} else {
s.output <- file
}
}
} }
func (gs *goldsmith) Chain(chain interface{}, err error) Goldsmith { func (gs *goldsmith) Chain(ctx *Context) Goldsmith {
if gs.err != nil { if gs.err != nil {
return gs return gs
} }
if gs.err = err; gs.err != nil { if gs.err = ctx.Err; gs.err != nil {
switch t := chain.(type) { switch c := ctx.Chainer.(type) {
case ChainerSingle: case ChainerSingle:
gs.chainSingle(t) go gs.chainSingle(gs.makeStage(), c)
case ChainerMultiple: case ChainerMultiple:
gs.chainMultiple(t) go gs.chainMultiple(gs.makeStage(), c)
} }
} }

View File

@ -25,7 +25,7 @@ package goldsmith
import "bytes" import "bytes"
type Goldsmith interface { type Goldsmith interface {
Chain(task interface{}, err error) Goldsmith Chain(ctx *Context) Goldsmith
Complete(dstDir string) ([]File, error) Complete(dstDir string) ([]File, error)
} }
@ -43,3 +43,8 @@ type File struct {
Buff bytes.Buffer Buff bytes.Buffer
Err error Err error
} }
type Context struct {
Chainer interface{}
Err error
}