Files
learngo/27-functional-programming/log-parser-exp/report.go

63 lines
1.0 KiB
Go
Raw Normal View History

package main
2019-08-06 17:15:29 +03:00
import "os"
type (
2019-08-06 03:32:24 +03:00
inputFunc func() ([]result, error)
outputFunc func([]result) error
)
type report struct {
2019-08-06 03:32:24 +03:00
input inputFunc
filter filterFunc
group groupFunc
output outputFunc
}
func newReport() *report {
return &report{
2019-08-06 01:42:59 +03:00
filter: noopFilter,
group: noopGrouper,
2019-08-06 17:15:29 +03:00
input: textReader(os.Stdin),
output: textWriter(os.Stdout),
}
}
2019-08-06 03:32:24 +03:00
func (r *report) from(fn inputFunc) *report {
r.input = fn
return r
}
2019-08-06 03:32:24 +03:00
func (r *report) to(fn outputFunc) *report {
r.output = fn
return r
}
func (r *report) filterBy(fn filterFunc) *report {
2019-08-06 01:42:59 +03:00
r.filter = fn
return r
}
func (r *report) groupBy(fn groupFunc) *report {
2019-08-06 01:42:59 +03:00
r.group = fn
return r
}
2019-08-06 03:32:24 +03:00
func (r *report) start() ([]result, error) {
// input filterBy groupBy
// scanner (result) bool map[string]result
//
// stdin -> []result -> []results -> []result -> output(stdout)
res, err := r.input()
if err != nil {
return nil, err
}
res = filterBy(res, r.filter)
res = groupBy(res, r.group)
err = r.output(res)
return res, err
}