~foosoft/goldsmith

b343397ec587272b1497be0dc8ef0f12daed6aa3 — Alex Yatskov 9 years ago ef1d7d3
Simplification
2 files changed, 38 insertions(+), 5 deletions(-)

M goldsmith.go
M types.go
M goldsmith.go => goldsmith.go +31 -2
@@ 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
}


M types.go => types.go +7 -3
@@ 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
}

Do not follow this link