From 29501c566ce58234d1e6c3f84e7b24147fa6ca77 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Fri, 4 Oct 2019 14:53:07 +0300 Subject: [PATCH] refactor: marshaler --- interfaces/10-marshaler/list.go | 1 - interfaces/10-marshaler/main.go | 48 ++++++++++++++--------- interfaces/10-marshaler/timestamp.go | 57 +++++++++++----------------- 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/interfaces/10-marshaler/list.go b/interfaces/10-marshaler/list.go index 01b6eaa..46f2b01 100644 --- a/interfaces/10-marshaler/list.go +++ b/interfaces/10-marshaler/list.go @@ -29,7 +29,6 @@ func (l list) String() string { str.WriteString(it.String()) str.WriteRune('\n') } - return str.String() } diff --git a/interfaces/10-marshaler/main.go b/interfaces/10-marshaler/main.go index b9afe64..a206f97 100644 --- a/interfaces/10-marshaler/main.go +++ b/interfaces/10-marshaler/main.go @@ -14,35 +14,47 @@ import ( ) 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{ &book{product{"moby dick", 10}, toTimestamp(118281600)}, &book{product{"odyssey", 15}, toTimestamp("733622400")}, - &book{product{"hobbit", 25}, unknown}, + &book{product{"hobbit", 25}, toTimestamp(nil)}, &puzzle{product{"rubik's cube", 5}}, &game{product{"minecraft", 20}}, &game{product{"tetris", 5}}, &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 { log.Fatalln(err) } - 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) + return out + // fmt.Println(string(out)) } diff --git a/interfaces/10-marshaler/timestamp.go b/interfaces/10-marshaler/timestamp.go index aa8c3d8..04732e6 100644 --- a/interfaces/10-marshaler/timestamp.go +++ b/interfaces/10-marshaler/timestamp.go @@ -12,53 +12,35 @@ import ( "time" ) -// unknown is the zero value of a timestamp. -var unknown = timestamp(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 +type timestamp struct { + time.Time } +// timestamp knows how to decode itself from json. +// // UnmarshalJSON is an implementation of the json.Unmarshaler interface. // json.Unmarshal and json.Decode call this method. func (ts *timestamp) UnmarshalJSON(data []byte) error { - s := 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)) - } - + *ts = toTimestamp(string(data)) return nil } -func (ts timestamp) String() string { - t := time.Time(ts) +// timestamp knows how to encode itself to json. +// +// 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" } // Mon Jan 2 15:04:05 -0700 MST 2006 const layout = "2006/01" - - return t.Format(layout) + return ts.Format(layout) } func toTimestamp(v interface{}) timestamp { @@ -66,12 +48,17 @@ func toTimestamp(v interface{}) timestamp { switch v := v.(type) { case int: + // book{title: "moby dick", price: 10, published: 118281600}, t = v case string: + // book{title: "odyssey", price: 15, published: "733622400"}, t, _ = strconv.Atoi(v) 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), + } }