improve: oop log parser err handling
This commit is contained in:
@ -28,6 +28,7 @@ func main() {
|
|||||||
|
|
||||||
pipe := newPipeline(
|
pipe := newPipeline(
|
||||||
newTextLog(os.Stdin),
|
newTextLog(os.Stdin),
|
||||||
|
// newJSONLog(os.Stdin),
|
||||||
newTextReport(),
|
newTextReport(),
|
||||||
filterBy(notUsing(domainExtFilter("com", "io"))),
|
filterBy(notUsing(domainExtFilter("com", "io"))),
|
||||||
groupBy(domainGrouper),
|
groupBy(domainGrouper),
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -37,16 +38,7 @@ func (r *result) UnmarshalText(p []byte) (err error) {
|
|||||||
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
|
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return validate(*r)
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
// UnmarshalJSON to a *result
|
||||||
@ -63,6 +55,28 @@ func (r *result) UnmarshalJSON(data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
*r = result{re.Domain, re.Page, re.Visits, re.Uniques}
|
*r = result{re.Domain, re.Page, re.Visits, re.Uniques}
|
||||||
|
return validate(*r)
|
||||||
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 {
|
||||||
|
return 0, fmt.Errorf("result.UnmarshalText %q: %v", name, err)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validate(r result) (err error) {
|
||||||
|
switch {
|
||||||
|
case r.domain == "":
|
||||||
|
err = errors.New("result.domain cannot be empty")
|
||||||
|
case r.page == "":
|
||||||
|
err = errors.New("result.page cannot be empty")
|
||||||
|
case r.visits < 0:
|
||||||
|
err = errors.New("result.visits cannot be negative")
|
||||||
|
case r.uniques < 0:
|
||||||
|
err = errors.New("result.uniques cannot be negative")
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user