refactor: marshaler unmarshaler iface
This commit is contained in:
@ -24,11 +24,11 @@ func encode() ([]byte, error) {
|
||||
return json.MarshalIndent(l, "", "\t")
|
||||
}
|
||||
|
||||
func decode(data []byte) (l list, err error) {
|
||||
func decode(data []byte) (l list, _ error) {
|
||||
if err := json.Unmarshal(data, &l); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l, nil
|
||||
return
|
||||
}
|
||||
|
||||
func decodeFile(path string) (list, error) {
|
||||
|
@ -8,6 +8,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -34,16 +35,20 @@ func (l list) discount(ratio float64) {
|
||||
}
|
||||
}
|
||||
|
||||
// by default `list` sorts by `Title`.
|
||||
// implementation of the sort.Interface:
|
||||
|
||||
// by default `list` sorts by `title`.
|
||||
func (l list) Len() int { return len(l) }
|
||||
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||
func (l list) Less(i, j int) bool { return l[i].Title < l[j].Title }
|
||||
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||
|
||||
// byRelease sorts by product release dates.
|
||||
type byRelease struct {
|
||||
list
|
||||
}
|
||||
type byRelease struct{ list }
|
||||
|
||||
func (bp byRelease) Less(i, j int) bool {
|
||||
return bp.list[i].Released.Before(bp.list[j].Released.Time)
|
||||
}
|
||||
|
||||
func byReleaseDate(l list) sort.Interface {
|
||||
return &byRelease{l}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
type product struct {
|
||||
Title string `json:"title"`
|
||||
Price money `json:"price"`
|
||||
Released timestamp `json:"released,omitempty"`
|
||||
Released timestamp `json:"released"`
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
|
@ -31,7 +31,7 @@ func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts timestamp) MarshalJSON() (out []byte, err error) {
|
||||
func (ts timestamp) MarshalJSON() (out []byte, _ error) {
|
||||
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user