refactor: marshaler

This commit is contained in:
Inanc Gumus
2019-10-04 14:53:07 +03:00
parent e6eba7314f
commit 29501c566c
3 changed files with 52 additions and 54 deletions

View File

@ -29,7 +29,6 @@ func (l list) String() string {
str.WriteString(it.String()) str.WriteString(it.String())
str.WriteRune('\n') str.WriteRune('\n')
} }
return str.String() return str.String()
} }

View File

@ -14,35 +14,47 @@ import (
) )
func main() { func main() {
out := encode()
decode(out)
}
func decode(data []byte) {
// data := []byte(`[
// { "Title": "moby dick", "Price": 5, "Published": 118281600 },
// { "Title": "odyssey", "Price": 7.5, "Published": 733622400 },
// { "Title": "hobbit", "Price": 12.5, "Published": -62135596800 }
// ]`)
var books []*book
if err := json.Unmarshal(data, &books); err != nil {
log.Fatalln(err)
}
for _, b := range books {
fmt.Println(b)
}
}
func encode() []byte {
store := list{ store := list{
&book{product{"moby dick", 10}, toTimestamp(118281600)}, &book{product{"moby dick", 10}, toTimestamp(118281600)},
&book{product{"odyssey", 15}, toTimestamp("733622400")}, &book{product{"odyssey", 15}, toTimestamp("733622400")},
&book{product{"hobbit", 25}, unknown}, &book{product{"hobbit", 25}, toTimestamp(nil)},
&puzzle{product{"rubik's cube", 5}}, &puzzle{product{"rubik's cube", 5}},
&game{product{"minecraft", 20}}, &game{product{"minecraft", 20}},
&game{product{"tetris", 5}}, &game{product{"tetris", 5}},
&toy{product{"yoda", 150}}, &toy{product{"yoda", 150}},
} }
out, err := json.MarshalIndent(store, "", "\t") store.discount(.5)
// fmt.Print(store)
out, err := json.MarshalIndent(store[:3], "", "\t")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
fmt.Println(string(out)) return out
// fmt.Println(string(out))
// store.discount(.5)
// fmt.Print(store)
// ----
// var ts timestamp
// json.Unmarshal([]byte(`118281600`), &ts)
// fmt.Println(ts)
// json.Unmarshal([]byte(`"18281600"`), &ts)
// fmt.Println(ts)
// json.Unmarshal([]byte(`"incorrect"`), &ts)
// fmt.Println(ts)
} }

View File

@ -12,53 +12,35 @@ import (
"time" "time"
) )
// unknown is the zero value of a timestamp. type timestamp struct {
var unknown = timestamp(time.Time{}) time.Time
// timestamp prints timestamps, it's a stringer.
// It is useful even if it's zero.
type timestamp time.Time
// MarshalJSON is an implementation of the json.Marshaler interface.
// json.Marshal and json.Encode call this method.
func (ts timestamp) MarshalJSON() (out []byte, err error) {
u := time.Time(ts).Unix()
return strconv.AppendInt(out, u, 10), nil
} }
// timestamp knows how to decode itself from json.
//
// UnmarshalJSON is an implementation of the json.Unmarshaler interface. // UnmarshalJSON is an implementation of the json.Unmarshaler interface.
// json.Unmarshal and json.Decode call this method. // json.Unmarshal and json.Decode call this method.
func (ts *timestamp) UnmarshalJSON(data []byte) error { func (ts *timestamp) UnmarshalJSON(data []byte) error {
s := string(data) *ts = toTimestamp(string(data))
// Let the ParseInt parse quoted strings.
us, err := strconv.Unquote(s)
if err == nil {
s = us
}
// Always overwrite the timestamp when decoding.
*ts = unknown
// Handle the numeric case.
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
*ts = timestamp(time.Unix(n, 0))
}
return nil return nil
} }
func (ts timestamp) String() string { // timestamp knows how to encode itself to json.
t := time.Time(ts) //
// MarshalJSON is an implementation of the json.Marshaler interface.
// json.Marshal and json.Encode call this method.
func (ts timestamp) MarshalJSON() (out []byte, err error) {
return strconv.AppendInt(out, ts.Unix(), 10), nil
}
if t.IsZero() { func (ts timestamp) String() string {
if ts.IsZero() {
return "unknown" return "unknown"
} }
// Mon Jan 2 15:04:05 -0700 MST 2006 // Mon Jan 2 15:04:05 -0700 MST 2006
const layout = "2006/01" const layout = "2006/01"
return ts.Format(layout)
return t.Format(layout)
} }
func toTimestamp(v interface{}) timestamp { func toTimestamp(v interface{}) timestamp {
@ -66,12 +48,17 @@ func toTimestamp(v interface{}) timestamp {
switch v := v.(type) { switch v := v.(type) {
case int: case int:
// book{title: "moby dick", price: 10, published: 118281600},
t = v t = v
case string: case string:
// book{title: "odyssey", price: 15, published: "733622400"},
t, _ = strconv.Atoi(v) t, _ = strconv.Atoi(v)
default: default:
return unknown // book{title: "hobbit", price: 25},
return timestamp{}
} }
return timestamp(time.Unix(int64(t), 0)) return timestamp{
Time: time.Unix(int64(t), 0),
}
} }