refactor: common ifaces

This commit is contained in:
Inanc Gumus
2019-10-17 14:11:51 +03:00
parent 34cf7f7de2
commit c062a46355
119 changed files with 1247 additions and 194 deletions

View File

@@ -0,0 +1,17 @@
[
{
"title": "moby dick",
"price": 10,
"released": 118281600
},
{
"title": "odyssey",
"price": 15,
"released": 733622400
},
{
"title": "hobbit",
"price": 25,
"released": -62135596800
}
]

View File

@@ -0,0 +1,29 @@
// 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"
)
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, err error) {
if err := json.Unmarshal(data, &l); err != nil {
return nil, err
}
return l, nil
}

View File

@@ -0,0 +1,49 @@
// 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"
"sort"
"strings"
)
type list []*product
func (l list) String() string {
if len(l) == 0 {
return "Sorry. We're waiting for delivery 🚚.\n"
}
sort.Sort(l)
var str strings.Builder
for _, p := range l {
fmt.Fprintf(&str, "* %s\n", p)
}
return str.String()
}
func (l list) discount(ratio float64) {
for _, p := range l {
p.discount(ratio)
}
}
// 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 }
// 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)
}

View File

@@ -0,0 +1,21 @@
// 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"
)
func main() {
// removed error handling for brevity (normally you shouldn't do this).
out, _ := encode()
fmt.Println(string(out))
l, _ := decode(out)
fmt.Print(l)
}

View File

@@ -0,0 +1,16 @@
// 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)
}

View File

@@ -0,0 +1,26 @@
// 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,omitempty"`
}
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)
}

View File

@@ -0,0 +1,57 @@
// 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)
}
// 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 {
*ts = toTimestamp(string(data))
return nil
}
// 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 ts.Time.MarshalJSON()
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
}