renamings: methods log parser
This commit is contained in:
@ -20,5 +20,5 @@ func main() {
|
||||
report.update(p.parse(in.Text()))
|
||||
}
|
||||
|
||||
summarize(report, p.lerr, in.Err())
|
||||
summarize(report, p.err(), in.Err())
|
||||
}
|
||||
|
@ -9,8 +9,6 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/inancgumus/learngo/x-tba/2-methods/xxx-log-parser-methods/packaged/metrics"
|
||||
@ -23,7 +21,9 @@ func main() {
|
||||
for in.Scan() {
|
||||
report.Update(parser.Parse(in.Text()))
|
||||
}
|
||||
s, _ := json.Marshal(report)
|
||||
fmt.Println(string(s))
|
||||
|
||||
summarize(report, parser.Err(), in.Err())
|
||||
|
||||
// s, _ := json.Marshal(report)
|
||||
// fmt.Println(string(s))
|
||||
}
|
||||
|
@ -29,24 +29,24 @@ func NewParser() *Parser {
|
||||
}
|
||||
|
||||
// Parse parses a log line and returns a result with an injected error
|
||||
func (l *Parser) Parse(line string) (parsed Parsed) {
|
||||
func (p *Parser) Parse(line string) (parsed Parsed) {
|
||||
// always set the error
|
||||
defer func() { parsed.err = l.lerr }()
|
||||
defer func() { parsed.err = p.lerr }()
|
||||
|
||||
// if there was an error do not continue
|
||||
if l.lerr != nil {
|
||||
if p.lerr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// chain the parser's error to the result's
|
||||
res, err := parse(line)
|
||||
if l.lines++; err != nil {
|
||||
l.lerr = fmt.Errorf("%s: (line #%d)", err, l.lines)
|
||||
if p.lines++; err != nil {
|
||||
p.lerr = fmt.Errorf("%s: (line #%d)", err, p.lines)
|
||||
}
|
||||
return Parsed{result: res}
|
||||
}
|
||||
|
||||
// Err returns the last error encountered
|
||||
func (l *Parser) Err() error {
|
||||
return l.lerr
|
||||
func (p *Parser) Err() error {
|
||||
return p.lerr
|
||||
}
|
||||
|
@ -10,16 +10,16 @@ type parser struct {
|
||||
lerr error // the last error occurred
|
||||
}
|
||||
|
||||
// parserResult wraps a result for generating parser error
|
||||
type parserResult struct {
|
||||
// parsed wraps a result for generating parser error
|
||||
type parsed struct {
|
||||
result // use struct embedding
|
||||
err error // inject an error
|
||||
}
|
||||
|
||||
// parse parses a log line and returns a result with an injected error
|
||||
func (p *parser) parse(line string) (parsed parserResult) {
|
||||
func (p *parser) parse(line string) (pv parsed) {
|
||||
// always set the error
|
||||
defer func() { parsed.err = p.lerr }()
|
||||
defer func() { pv.err = p.lerr }()
|
||||
|
||||
// if there was an error do not continue
|
||||
if p.lerr != nil {
|
||||
@ -31,5 +31,10 @@ func (p *parser) parse(line string) (parsed parserResult) {
|
||||
if p.lines++; err != nil {
|
||||
p.lerr = fmt.Errorf("%s: (line #%d)", err, p.lines)
|
||||
}
|
||||
return parserResult{result: res}
|
||||
return parsed{result: res}
|
||||
}
|
||||
|
||||
// err returns the last error encountered
|
||||
func (p *parser) err() error {
|
||||
return p.lerr
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ func newReport() *report {
|
||||
}
|
||||
|
||||
// update updates the errors for the given parsing result
|
||||
func (r *report) update(parsed parserResult) {
|
||||
func (r *report) update(parsed parsed) {
|
||||
// do not update the report if the result has an error
|
||||
if parsed.err != nil {
|
||||
return
|
||||
|
@ -13,16 +13,16 @@ import (
|
||||
)
|
||||
|
||||
// summarize prints the report and errors if any
|
||||
func summarize(r *report, errs ...error) {
|
||||
func summarize(rep *report, errs ...error) {
|
||||
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
||||
fmt.Println(strings.Repeat("-", 45))
|
||||
|
||||
next, cur := r.iterator()
|
||||
next, cur := rep.iterator()
|
||||
for next() {
|
||||
rec := cur()
|
||||
fmt.Printf("%-30s %10d\n", rec.domain, rec.visits)
|
||||
}
|
||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", r.total.visits)
|
||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", rep.total.visits)
|
||||
|
||||
// only handle the errors once
|
||||
dumpErrs(errs...)
|
||||
|
Reference in New Issue
Block a user