2019-10-30 19:34:44 +03:00
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
// recordJSON is used for marshaling and unmarshaling JSON.
|
|
|
|
type recordJSON struct {
|
|
|
|
Domain string
|
|
|
|
Page string
|
|
|
|
Visits int
|
|
|
|
Uniques int
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
// UnmarshalText to a *record.
|
|
|
|
func (r *record) UnmarshalText(p []byte) (err error) {
|
2019-08-28 23:46:42 +03:00
|
|
|
fields := strings.Fields(string(p))
|
2019-08-29 16:08:46 +03:00
|
|
|
|
2019-08-28 23:46:42 +03:00
|
|
|
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
|
|
|
|
}
|
2019-08-29 16:08:46 +03:00
|
|
|
|
2019-08-28 23:46:42 +03:00
|
|
|
return validate(*r)
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
// UnmarshalJSON to a *record.
|
|
|
|
func (r *record) UnmarshalJSON(data []byte) error {
|
|
|
|
var rj recordJSON
|
2019-08-28 23:46:42 +03:00
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
if err := json.Unmarshal(data, &rj); err != nil {
|
2019-08-28 23:46:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
*r = record{rj.Domain, rj.Page, rj.Visits, rj.Uniques}
|
2019-08-29 16:08:46 +03:00
|
|
|
|
2019-08-28 23:46:42 +03:00
|
|
|
return validate(*r)
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
// MarshalJSON of a *record.
|
|
|
|
func (r *record) MarshalJSON() ([]byte, error) {
|
2019-08-29 16:08:46 +03:00
|
|
|
rj := recordJSON{r.domain, r.page, r.visits, r.uniques}
|
|
|
|
return json.Marshal(rj)
|
2019-08-29 01:50:04 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 23:46:42 +03:00
|
|
|
// 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.
|
2019-08-29 01:50:04 +03:00
|
|
|
func validate(r record) (err error) {
|
2019-08-28 23:46:42 +03:00
|
|
|
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
|
|
|
|
}
|