remove: io iface
This commit is contained in:
@ -1,17 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"title": "moby dick",
|
|
||||||
"price": 10,
|
|
||||||
"released": 118281600
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "odyssey",
|
|
||||||
"price": 15,
|
|
||||||
"released": 733622400
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "hobbit",
|
|
||||||
"price": 25,
|
|
||||||
"released": -62135596800
|
|
||||||
}
|
|
||||||
]
|
|
@ -1,59 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func encode() ([]byte, error) {
|
|
||||||
l := list{
|
|
||||||
{Title: "moby dick", Price: 10, Released: toTimestamp(118281600)},
|
|
||||||
{Title: "odyssey", Price: 15, Released: toTimestamp("733622400")},
|
|
||||||
{Title: "hobbit", Price: 25},
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.MarshalIndent(l, "", "\t")
|
|
||||||
}
|
|
||||||
|
|
||||||
func decode(data []byte) (l list, _ error) {
|
|
||||||
if err := json.Unmarshal(data, &l); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeFile(path string) (list, error) {
|
|
||||||
// ReadAll reads entire bytes from a file to memory.
|
|
||||||
data, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return decode(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeFileObject(f *os.File) (list, error) {
|
|
||||||
data, err := ioutil.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return decode(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeReader(r io.Reader) (l list, err error) {
|
|
||||||
// data, err := ioutil.ReadAll(r)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return decode(data)
|
|
||||||
|
|
||||||
return l, json.NewDecoder(r).Decode(&l)
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type list []*product
|
|
||||||
|
|
||||||
func (l list) String() string {
|
|
||||||
if len(l) == 0 {
|
|
||||||
return "Sorry. We're waiting for delivery 🚚.\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
var str strings.Builder
|
|
||||||
for _, p := range l {
|
|
||||||
// fmt.Printf("* %s\n", p)
|
|
||||||
str.WriteString("* ")
|
|
||||||
str.WriteString(p.String())
|
|
||||||
str.WriteRune('\n')
|
|
||||||
}
|
|
||||||
return str.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l list) discount(ratio float64) {
|
|
||||||
for _, p := range l {
|
|
||||||
p.discount(ratio)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// implementation of the sort.Interface:
|
|
||||||
|
|
||||||
// by default `list` sorts by `title`.
|
|
||||||
func (l list) Len() int { return len(l) }
|
|
||||||
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 }
|
|
||||||
|
|
||||||
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}
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// removed error handling for brevity (normally you shouldn't do this).
|
|
||||||
|
|
||||||
// encode to memory (to a byte slice)
|
|
||||||
// out, _ := encode()
|
|
||||||
// fmt.Println(string(out))
|
|
||||||
|
|
||||||
// read from memory (from a byte slice):
|
|
||||||
// l, _ := decode(out)
|
|
||||||
|
|
||||||
// read from a file by name:
|
|
||||||
// l, _ := decodeFile("database.json")
|
|
||||||
|
|
||||||
// read from a file #1 (not good):
|
|
||||||
// f, _ := os.Open("database.json")
|
|
||||||
// l, _ := decodeFileObject(f)
|
|
||||||
// f.Close()
|
|
||||||
|
|
||||||
// read from a file #2 (better):
|
|
||||||
// f, _ := os.Open("database.json")
|
|
||||||
// l, _ := decodeReader(f)
|
|
||||||
// f.Close()
|
|
||||||
|
|
||||||
// read from memory (from a string):
|
|
||||||
// const data = `[
|
|
||||||
// { "title": "moby dick", "price": 10, "released": 118281600 },
|
|
||||||
// { "title": "odyssey", "price": 15, "released": 733622400 },
|
|
||||||
// { "title": "hobbit", "price": 25, "released": -62135596800 }
|
|
||||||
// ]`
|
|
||||||
// l, _ := decodeReader(strings.NewReader(data))
|
|
||||||
|
|
||||||
// read from inanc's web server:
|
|
||||||
// res, _ := http.Get("https://inancgumus.github.io/x/database.json")
|
|
||||||
// l, _ := decodeReader(res.Body)
|
|
||||||
// res.Body.Close()
|
|
||||||
|
|
||||||
// fmt.Print(l)
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type money float64
|
|
||||||
|
|
||||||
func (m money) String() string {
|
|
||||||
return fmt.Sprintf("$%.2f", m)
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type product struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Price money `json:"price"`
|
|
||||||
Released timestamp `json:"released"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) String() string {
|
|
||||||
return fmt.Sprintf("%s: %s (%s)", p.Title, p.Price, p.Released)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) discount(ratio float64) {
|
|
||||||
p.Price *= money(1 - ratio)
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type timestamp struct {
|
|
||||||
time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts timestamp) String() string {
|
|
||||||
if ts.IsZero() {
|
|
||||||
return "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
|
||||||
const layout = "2006/01"
|
|
||||||
return ts.Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
|
||||||
*ts = toTimestamp(string(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts timestamp) MarshalJSON() (out []byte, _ error) {
|
|
||||||
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func toTimestamp(v interface{}) (ts timestamp) {
|
|
||||||
var t int
|
|
||||||
|
|
||||||
switch v := v.(type) {
|
|
||||||
case int:
|
|
||||||
t = v
|
|
||||||
case string:
|
|
||||||
t, _ = strconv.Atoi(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
ts.Time = time.Unix(int64(t), 0)
|
|
||||||
return ts
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"title": "moby dick",
|
|
||||||
"price": 10,
|
|
||||||
"released": 118281600
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "odyssey",
|
|
||||||
"price": 15,
|
|
||||||
"released": 733622400
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "hobbit",
|
|
||||||
"price": 25,
|
|
||||||
"released": -62135596800
|
|
||||||
}
|
|
||||||
]
|
|
Binary file not shown.
@ -1,59 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func encode() ([]byte, error) {
|
|
||||||
l := list{
|
|
||||||
{Title: "moby dick", Price: 10, Released: toTimestamp(118281600)},
|
|
||||||
{Title: "odyssey", Price: 15, Released: toTimestamp("733622400")},
|
|
||||||
{Title: "hobbit", Price: 25},
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.MarshalIndent(l, "", "\t")
|
|
||||||
}
|
|
||||||
|
|
||||||
func decode(data []byte) (l list, _ error) {
|
|
||||||
if err := json.Unmarshal(data, &l); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeFile(path string) (list, error) {
|
|
||||||
// ReadAll reads entire bytes from a file to memory.
|
|
||||||
data, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return decode(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeFileObject(f *os.File) (list, error) {
|
|
||||||
data, err := ioutil.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return decode(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeReader(r io.Reader) (l list, err error) {
|
|
||||||
// data, err := ioutil.ReadAll(r)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return decode(data)
|
|
||||||
|
|
||||||
return l, json.NewDecoder(r).Decode(&l)
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type list []*product
|
|
||||||
|
|
||||||
func (l list) String() string {
|
|
||||||
if len(l) == 0 {
|
|
||||||
return "Sorry. We're waiting for delivery 🚚.\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
var str strings.Builder
|
|
||||||
for _, p := range l {
|
|
||||||
// fmt.Printf("* %s\n", p)
|
|
||||||
str.WriteString("* ")
|
|
||||||
str.WriteString(p.String())
|
|
||||||
str.WriteRune('\n')
|
|
||||||
}
|
|
||||||
return str.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l list) discount(ratio float64) {
|
|
||||||
for _, p := range l {
|
|
||||||
p.discount(ratio)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// implementation of the sort.Interface:
|
|
||||||
|
|
||||||
// by default `list` sorts by `title`.
|
|
||||||
func (l list) Len() int { return len(l) }
|
|
||||||
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 }
|
|
||||||
|
|
||||||
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}
|
|
||||||
}
|
|
@ -1,82 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
// compress in bash:
|
|
||||||
// gzip -k database.json
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// removed error handling and x.Close() calls for brevity (normally you shouldn't do this).
|
|
||||||
// normally: fr.Close(), gr.Close() should also be called to release the resources.
|
|
||||||
|
|
||||||
// data, _ := ioutil.ReadFile("database.json")
|
|
||||||
// l, _ := decode(data)
|
|
||||||
|
|
||||||
// fmt.Print(string(data))
|
|
||||||
// fmt.Print(l)
|
|
||||||
|
|
||||||
// ---
|
|
||||||
|
|
||||||
// fr, _ := os.Open("database.json")
|
|
||||||
// tr := TeeReader(fr, os.Stdout)
|
|
||||||
// l, _ := decodeReader(tr)
|
|
||||||
// fmt.Print(l)
|
|
||||||
|
|
||||||
// ---
|
|
||||||
|
|
||||||
// fr, _ := os.Open("database.json.gz")
|
|
||||||
// gr, _ := gzip.NewReader(fr)
|
|
||||||
// tr := TeeReader(gr, os.Stdout)
|
|
||||||
// l, _ := decodeReader(tr)
|
|
||||||
// fmt.Print(l)
|
|
||||||
|
|
||||||
// ---
|
|
||||||
|
|
||||||
// TODO: FIX
|
|
||||||
// l, err := decodeGzip("database.json.gz", os.Stdout)
|
|
||||||
// l, err := decodeGzip("database.json.gz", ioutil.Discard)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatalln(err)
|
|
||||||
// }
|
|
||||||
// fmt.Print("Products:\n", l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// func decodeGzipFile(path string) (list, error) {
|
|
||||||
// fr, err := os.Open("database.json.gz")
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// defer fr.Close()
|
|
||||||
|
|
||||||
// return decodeGzip(fr)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func decodeGzip(r io.Reader, w io.Writer) (list, error) {
|
|
||||||
// fr, err := os.Open("database.json.gz")
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// gr, err := gzip.NewReader(fr)
|
|
||||||
// if err != nil {
|
|
||||||
// fr.Close()
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tr := TeeReader(gr, w)
|
|
||||||
// l, err := decodeReader(tr)
|
|
||||||
// if err != nil {
|
|
||||||
// fr.Close()
|
|
||||||
// gr.Close()
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fr.Close()
|
|
||||||
// gr.Close()
|
|
||||||
// return l, nil
|
|
||||||
// }
|
|
@ -1,16 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type money float64
|
|
||||||
|
|
||||||
func (m money) String() string {
|
|
||||||
return fmt.Sprintf("$%.2f", m)
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type product struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Price money `json:"price"`
|
|
||||||
Released timestamp `json:"released"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) String() string {
|
|
||||||
return fmt.Sprintf("%s: %s (%s)", p.Title, p.Price, p.Released)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) discount(ratio float64) {
|
|
||||||
p.Price *= money(1 - ratio)
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
// TeeReader returns a Reader that writes to w what it reads from r.
|
|
||||||
// All reads from r performed through it are matched with
|
|
||||||
// corresponding writes to w. There is no internal buffering -
|
|
||||||
// the write must complete before the read completes.
|
|
||||||
// Any error encountered while writing is reported as a read error.
|
|
||||||
func TeeReader(r io.Reader, w io.Writer) io.Reader {
|
|
||||||
return &teeReader{r, w}
|
|
||||||
}
|
|
||||||
|
|
||||||
type teeReader struct {
|
|
||||||
r io.Reader
|
|
||||||
w io.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *teeReader) Read(p []byte) (n int, err error) {
|
|
||||||
n, err = t.r.Read(p)
|
|
||||||
if n > 0 {
|
|
||||||
if n, err := t.w.Write(p[:n]); err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type timestamp struct {
|
|
||||||
time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts timestamp) String() string {
|
|
||||||
if ts.IsZero() {
|
|
||||||
return "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
|
||||||
const layout = "2006/01"
|
|
||||||
return ts.Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
|
||||||
*ts = toTimestamp(string(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts timestamp) MarshalJSON() (out []byte, _ error) {
|
|
||||||
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func toTimestamp(v interface{}) (ts timestamp) {
|
|
||||||
var t int
|
|
||||||
|
|
||||||
switch v := v.(type) {
|
|
||||||
case int:
|
|
||||||
t = v
|
|
||||||
case string:
|
|
||||||
t, _ = strconv.Atoi(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
ts.Time = time.Unix(int64(t), 0)
|
|
||||||
return ts
|
|
||||||
}
|
|
Reference in New Issue
Block a user