refactor: oop log parser names

This commit is contained in:
Inanc Gumus
2019-08-28 12:46:43 +03:00
parent aff765c4a8
commit 55a480a016
4 changed files with 17 additions and 11 deletions

View File

@ -33,7 +33,7 @@ func (g *group) digest(results iterator) error {
g.keys = append(g.keys, k)
}
g.sum[k] = r.add(g.sum[k])
g.sum[k] = r.sum(g.sum[k])
})
}

View File

@ -29,7 +29,7 @@ func main() {
pipe := newPipeline(
newTextLog(os.Stdin),
newTextReport(),
filterBy(domainExtFilter("com", "io")),
filterBy(notUsing(domainExtFilter("com", "io"))),
groupBy(domainGrouper),
)

View File

@ -16,7 +16,7 @@ type result struct {
uniques int
}
func (r result) add(other result) result {
func (r result) sum(other result) result {
r.visits += other.visits
r.uniques += other.uniques
return r
@ -31,18 +31,24 @@ func (r *result) UnmarshalText(p []byte) (err error) {
r.domain, r.page = fields[0], fields[1]
r.visits, err = strconv.Atoi(fields[2])
if err != nil || r.visits < 0 {
return fmt.Errorf("wrong input %q", fields[2])
if r.visits, err = parseStr("visits", fields[2]); err != nil {
return err
}
r.uniques, err = strconv.Atoi(fields[3])
if err != nil || r.uniques < 0 {
return fmt.Errorf("wrong input %q", fields[3])
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
return err
}
return nil
}
// parseStr helps UnmarshalText for string to positive int parsing
func parseStr(name, v string) (int, error) {
n, err := strconv.Atoi(v)
if err != nil || n < 0 {
return 0, fmt.Errorf("result.UnmarshalText %q: %v", name, err)
}
return n, nil
}
// UnmarshalJSON to a *result
func (r *result) UnmarshalJSON(data []byte) error {
var re struct {

View File

@ -37,7 +37,7 @@ func (s *textReport) digest(results iterator) error {
var total result
results.each(func(r result) {
total = total.add(r)
total = total.sum(r)
write(w, "%s\t%s\t%d\t%d\n", r.domain, r.page, r.visits, r.uniques)
})