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