Files
learngo/logparser/v5/pipe/record.go

86 lines
1.8 KiB
Go
Raw Normal View History

2019-08-28 23:46:42 +03:00
package pipe
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
)
const fieldsLength = 4
2019-08-29 01:32:25 +03:00
// record stores fields of a log line.
type record struct {
domain string
page string
visits int
uniques int
2019-08-28 23:46:42 +03:00
}
// Sum the numeric fields with another record.
func (r Record) Sum(other Record) Record {
2019-08-29 01:32:25 +03:00
r.visits += other.visits
r.uniques += other.uniques
2019-08-28 23:46:42 +03:00
return r
}
// 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)
}
2019-08-29 01:32:25 +03:00
r.domain, r.page = fields[0], fields[1]
2019-08-28 23:46:42 +03:00
2019-08-29 01:32:25 +03:00
if r.visits, err = parseStr("visits", fields[2]); err != nil {
2019-08-28 23:46:42 +03:00
return err
}
2019-08-29 01:32:25 +03:00
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
2019-08-28 23:46:42 +03:00
return err
}
return validate(*r)
}
// UnmarshalJSON to a *Record.
func (r *Record) UnmarshalJSON(data []byte) error {
// `methodless` doesn't have any methods including UnmarshalJSON.
// This trick prevents the stack-overflow (infinite loop).
type methodless Record
var m methodless
if err := json.Unmarshal(data, &m); err != nil {
return err
}
// Cast back to the Record and save.
*r = Record(m)
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 {
return 0, fmt.Errorf("Record.UnmarshalText %q: %v", name, err)
}
return n, nil
}
// validate whether a parsed record is valid or not.
func validate(r Record) (err error) {
switch {
2019-08-29 01:32:25 +03:00
case r.domain == "":
2019-08-28 23:46:42 +03:00
err = errors.New("record.domain cannot be empty")
2019-08-29 01:32:25 +03:00
case r.page == "":
2019-08-28 23:46:42 +03:00
err = errors.New("record.page cannot be empty")
2019-08-29 01:32:25 +03:00
case r.visits < 0:
2019-08-28 23:46:42 +03:00
err = errors.New("record.visits cannot be negative")
2019-08-29 01:32:25 +03:00
case r.uniques < 0:
2019-08-28 23:46:42 +03:00
err = errors.New("record.uniques cannot be negative")
}
return
}