refactor: logparser v5 funcs

This commit is contained in:
Inanc Gumus
2019-08-29 16:08:46 +03:00
parent d28c6d54d2
commit 661e018258
10 changed files with 48 additions and 53 deletions

View File

@ -36,6 +36,7 @@ func (r record) sum(other record) record {
// UnmarshalText to a *record.
func (r *record) UnmarshalText(p []byte) (err error) {
fields := strings.Fields(string(p))
if len(fields) != fieldsLength {
return fmt.Errorf("wrong number of fields %q", fields)
}
@ -48,6 +49,7 @@ func (r *record) UnmarshalText(p []byte) (err error) {
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
return err
}
return validate(*r)
}
@ -60,12 +62,14 @@ func (r *record) UnmarshalJSON(data []byte) error {
}
*r = record{rj.Domain, rj.Page, rj.Visits, rj.Uniques}
return validate(*r)
}
// MarshalJSON of a *record.
func (r *record) MarshalJSON() ([]byte, error) {
return json.Marshal(recordJSON{r.domain, r.page, r.visits, r.uniques})
rj := recordJSON{r.domain, r.page, r.visits, r.uniques}
return json.Marshal(rj)
}
// parseStr helps UnmarshalText for string to positive int parsing.