remove: _old iface
This commit is contained in:
@ -1,46 +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/
|
||||
//
|
||||
|
||||
// In the depths of the Go standard library's fmt package...
|
||||
// Printing functions use the handleMethods method.
|
||||
|
||||
// Example:
|
||||
// var pocket money = 10
|
||||
// fmt.Println(pocket)
|
||||
|
||||
// the argument can be any type of value
|
||||
// stores the pocket variable in the argument variable
|
||||
// ^
|
||||
// |
|
||||
func (p *pp) handleMethods(argument interface{}) (handled bool) {
|
||||
// ...
|
||||
|
||||
// Checks whether a given argument is an error or an fmt.Stringer
|
||||
switch v := argument.(type) {
|
||||
// ...
|
||||
// If the argument is a Stringer, calls its String() method
|
||||
case Stringer:
|
||||
// ...
|
||||
// pocket.String()
|
||||
// ^
|
||||
// |
|
||||
p.fmtString(v.String(), verb)
|
||||
return
|
||||
}
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
/*
|
||||
The original `handleMethods` code is more involved:
|
||||
|
||||
https://github.com/golang/go/blob/6f51082da77a1d4cafd5b7af0db69293943f4066/src/fmt/print.go#L574
|
||||
|
||||
-> 574#handleMethods(..)
|
||||
-> 627#Stringer type check: If `v` is a Stringer, run:
|
||||
-> 630#v.String()
|
||||
*/
|
@ -1,30 +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 book struct {
|
||||
product
|
||||
published timestamp
|
||||
}
|
||||
|
||||
// String method makes the book an fmt.Stringer.
|
||||
// The list was calling the embedded product type's String(),
|
||||
// now it calls the book.String().
|
||||
func (b *book) String() string {
|
||||
// product.String() has a pointer receiver.
|
||||
// Therefore, you need to manually take the product's address.
|
||||
//
|
||||
// If you pass: "b.product", Go would pass it as a copy to `Sprintf`.
|
||||
// In that case, Go can't deference b.product automatically.
|
||||
// It's because: b.product would be a different value, a copy.
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.published)
|
||||
}
|
@ -1,15 +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
|
||||
|
||||
type game struct {
|
||||
// game is an fmt.Stringer
|
||||
// because the product is an fmt.Stringer
|
||||
// and the game embeds the product
|
||||
product
|
||||
}
|
@ -1,60 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
|
||||
// ~~ interface embedding ~~
|
||||
//
|
||||
// item interface embeds the fmt.Stringer interface.
|
||||
//
|
||||
// Go adds the methods of the fmt.Stringer
|
||||
// to this item interface.
|
||||
//
|
||||
// same as this:
|
||||
// String() string
|
||||
// fmt.Stringer
|
||||
}
|
||||
type list []item
|
||||
|
||||
// String method makes the list an fmt.Stringer.
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
// use the strings.Builder when you're combining
|
||||
// a long list of strings together.
|
||||
var str strings.Builder
|
||||
|
||||
for _, it := range l {
|
||||
// the builder.WriteString doesn't know about the stringer interface.
|
||||
// because it takes a string argument.
|
||||
// so, you need to call the String method yourself.
|
||||
// str.WriteString(it.String())
|
||||
// str.WriteRune('\n')
|
||||
|
||||
// or slower way:
|
||||
// Print funcs can detect fmt.Stringer automatically.
|
||||
fmt.Fprintln(&str, it)
|
||||
}
|
||||
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -1,38 +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"
|
||||
|
||||
func main() {
|
||||
// The money type is a stringer.
|
||||
// You don't need to call the String method when printing a value of it.
|
||||
// var pocket money = 10
|
||||
// fmt.Println(pocket)
|
||||
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, toTimestamp(nil)},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
store.discount(.5)
|
||||
|
||||
// The list is a stringer.
|
||||
// The `fmt.Print` function can print the `store`
|
||||
// by calling `store`'s `String()` method.
|
||||
//
|
||||
// Underneath, `fmt.Print` uses a type switch to
|
||||
// detect whether a type is a Stringer:
|
||||
// https://golang.org/src/fmt/print.go#L627
|
||||
fmt.Print(store)
|
||||
}
|
@ -1,17 +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
|
||||
|
||||
// String makes the money an fmt.Stringer
|
||||
func (m money) String() string {
|
||||
return fmt.Sprintf("$%.2f", m)
|
||||
}
|
@ -1,24 +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
|
||||
price money
|
||||
}
|
||||
|
||||
// String makes the product an fmt.Stringer
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.title, p.price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.price *= money(1 - ratio)
|
||||
}
|
@ -1,15 +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
|
||||
|
||||
type puzzle struct {
|
||||
// puzzle is an fmt.Stringer
|
||||
// because the product is an fmt.Stringer
|
||||
// and the puzzle embeds the product
|
||||
product
|
||||
}
|
@ -1,53 +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"
|
||||
)
|
||||
|
||||
// timestamp stores, formats and automatically prints a timestamp: it's a stringer.
|
||||
type timestamp struct {
|
||||
// timestamp embeds a time, therefore it can be used as a time value.
|
||||
// there is no need to convert a time value to a timestamp value.
|
||||
time.Time
|
||||
}
|
||||
|
||||
// String method makes the timestamp an fmt.stringer.
|
||||
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)
|
||||
}
|
||||
|
||||
// toTimestamp returns a timestamp value depending on the type of `v`.
|
||||
// toTimestamp was "book.format()" before.
|
||||
func toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,15 +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
|
||||
|
||||
type toy struct {
|
||||
// toy is an fmt.Stringer
|
||||
// because the product is an fmt.Stringer
|
||||
// and the toy embeds the product
|
||||
product
|
||||
}
|
@ -1,23 +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 book struct {
|
||||
product
|
||||
|
||||
// Published (timestamp) knows how to print, encode and decode itself.
|
||||
Published timestamp
|
||||
}
|
||||
|
||||
func (b *book) String() string {
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.Published)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
@ -1,39 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type list []item
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
var str strings.Builder
|
||||
for _, it := range l {
|
||||
str.WriteString(it.String())
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -1,60 +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"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
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}, toTimestamp(nil)},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
store.discount(.5)
|
||||
// fmt.Print(store)
|
||||
|
||||
out, err := json.MarshalIndent(store[:3], "", "\t")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
return out
|
||||
// fmt.Println(string(out))
|
||||
}
|
@ -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,23 +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
|
||||
Price money
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
@ -1,64 +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
|
||||
}
|
||||
|
||||
// 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 strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
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 toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
@ -1,21 +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 book struct {
|
||||
product
|
||||
Published timestamp
|
||||
}
|
||||
|
||||
func (b *book) String() string {
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.Published)
|
||||
}
|
@ -1,62 +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"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// database is responsible for encoding and decoding a list.
|
||||
type database struct {
|
||||
list *list
|
||||
}
|
||||
|
||||
func (db *database) UnmarshalJSON(data []byte) error {
|
||||
// Provides a concrete data structure that we can use to decode.
|
||||
// It is a slice of structs.
|
||||
var decodables []struct {
|
||||
Type string // The product type.
|
||||
Item json.RawMessage // The raw item data (from database.json)
|
||||
}
|
||||
|
||||
// First, decode the "Type" part.
|
||||
// Leave the "Item" as raw json data.
|
||||
if err := json.Unmarshal(data, &decodables); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Decode the "Item" part for each json object.
|
||||
for _, d := range decodables {
|
||||
it, err := db.newItem(d.Item, d.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*db.list = append(*db.list, it)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// newItem decodes and returns a product type wrapped in an item iface value.
|
||||
func (*database) newItem(data []byte, itemType string) (it item, _ error) {
|
||||
switch itemType {
|
||||
default:
|
||||
return nil, fmt.Errorf("newItem: type (%q) does not exist", itemType)
|
||||
case "book":
|
||||
it = new(book)
|
||||
case "puzzle":
|
||||
it = new(puzzle)
|
||||
case "game":
|
||||
it = new(game)
|
||||
case "toy":
|
||||
it = new(toy)
|
||||
}
|
||||
return it, json.Unmarshal(data, &it)
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
[
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "moby dick",
|
||||
"Price": 10,
|
||||
"Published": 118281600
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "odyssey",
|
||||
"Price": 15,
|
||||
"Published": 733622400
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "hobbit",
|
||||
"Price": 25,
|
||||
"Published": -62135596800
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "puzzle",
|
||||
"Item": {
|
||||
"Title": "rubik's cube",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "minecraft",
|
||||
"Price": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "tetris",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "toy",
|
||||
"Item": {
|
||||
"Title": "yoda",
|
||||
"Price": 150
|
||||
}
|
||||
}
|
||||
]
|
@ -1,12 +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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
@ -1,40 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type list []item
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
var str strings.Builder
|
||||
for _, it := range l {
|
||||
str.WriteString(it.String())
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -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 (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, err := ioutil.ReadFile("database.json")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
var store list
|
||||
|
||||
db := database{list: &store}
|
||||
|
||||
if err := json.Unmarshal(data, &db); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Print(store)
|
||||
|
||||
/*
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, unknown},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(store, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
|
||||
// store.discount(.5)
|
||||
// fmt.Print(store)
|
||||
*/
|
||||
}
|
@ -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,23 +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
|
||||
Price money
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
@ -1,64 +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
|
||||
}
|
||||
|
||||
// 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 strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
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 toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
@ -1,21 +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 book struct {
|
||||
product
|
||||
Published timestamp
|
||||
}
|
||||
|
||||
func (b *book) String() string {
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.Published)
|
||||
}
|
@ -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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type database struct {
|
||||
list *list
|
||||
}
|
||||
|
||||
func (db *database) MarshalJSON() ([]byte, error) {
|
||||
type encodable struct {
|
||||
Type string
|
||||
Item item
|
||||
}
|
||||
|
||||
var e []encodable
|
||||
|
||||
for _, it := range *db.list {
|
||||
// TypeOf -> finds the dynamic type of "it"
|
||||
// Elem -> returns the element type of the pointer
|
||||
// Name -> returns the type name as string
|
||||
t := reflect.TypeOf(it).Elem().Name()
|
||||
e = append(e, encodable{t, it})
|
||||
|
||||
// Another way:
|
||||
//
|
||||
// t := fmt.Sprintf("%T", it)
|
||||
//
|
||||
// Uses: reflect.TypeOf(arg).String()
|
||||
}
|
||||
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
func (db *database) UnmarshalJSON(data []byte) error {
|
||||
var decodables []struct {
|
||||
Type string
|
||||
Item json.RawMessage
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &decodables); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, d := range decodables {
|
||||
it, err := db.newItem(d.Item, d.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*db.list = append(*db.list, it)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// newItem decodes and returns a product type wrapped in an item iface value.
|
||||
func (*database) newItem(data []byte, itemType string) (it item, _ error) {
|
||||
switch itemType {
|
||||
default:
|
||||
return nil, fmt.Errorf("newItem: type (%q) does not exist", itemType)
|
||||
case "book":
|
||||
it = new(book)
|
||||
case "puzzle":
|
||||
it = new(puzzle)
|
||||
case "game":
|
||||
it = new(game)
|
||||
case "toy":
|
||||
it = new(toy)
|
||||
}
|
||||
return it, json.Unmarshal(data, &it)
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
[
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "moby dick",
|
||||
"Price": 10,
|
||||
"Published": 118281600
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "odyssey",
|
||||
"Price": 15,
|
||||
"Published": 733622400
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "hobbit",
|
||||
"Price": 25,
|
||||
"Published": -62135596800
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "puzzle",
|
||||
"Item": {
|
||||
"Title": "rubik's cube",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "minecraft",
|
||||
"Price": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "tetris",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "toy",
|
||||
"Item": {
|
||||
"Title": "yoda",
|
||||
"Price": 150
|
||||
}
|
||||
}
|
||||
]
|
@ -1,12 +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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
@ -1,40 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type list []item
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
var str strings.Builder
|
||||
for _, it := range l {
|
||||
str.WriteString(it.String())
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -1,55 +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"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
/*
|
||||
data, err := ioutil.ReadFile("database.json")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
var store list
|
||||
|
||||
db := database{list: &store}
|
||||
|
||||
if err := json.Unmarshal(data, &db); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Print(store)
|
||||
*/
|
||||
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, unknown},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
db := database{list: &store}
|
||||
|
||||
out, err := json.MarshalIndent(&db, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
|
||||
// store.discount(.5)
|
||||
// fmt.Print(store)
|
||||
}
|
@ -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,23 +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
|
||||
Price money
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
@ -1,64 +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
|
||||
}
|
||||
|
||||
// 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 strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
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 toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
@ -1,21 +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 book struct {
|
||||
product
|
||||
Published timestamp
|
||||
}
|
||||
|
||||
func (b *book) String() string {
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.Published)
|
||||
}
|
@ -1,88 +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"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type database struct {
|
||||
list *list
|
||||
types map[string]item // the registry of the types
|
||||
}
|
||||
|
||||
func (db *database) MarshalJSON() ([]byte, error) {
|
||||
type encodable struct {
|
||||
Type string
|
||||
Item item
|
||||
}
|
||||
|
||||
var e []encodable
|
||||
|
||||
for _, it := range *db.list {
|
||||
// TypeOf -> finds the dynamic type of "it"
|
||||
// Elem -> returns the element type of the pointer
|
||||
// Name -> returns the type name as string
|
||||
t := reflect.TypeOf(it).Elem().Name()
|
||||
e = append(e, encodable{t, it})
|
||||
}
|
||||
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
func (db *database) UnmarshalJSON(data []byte) error {
|
||||
var decodables []struct {
|
||||
Type string
|
||||
Item json.RawMessage
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &decodables); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, d := range decodables {
|
||||
it, err := db.newItem(d.Item, d.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*db.list = append(*db.list, it)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *database) newItem(data []byte, typ string) (item, error) {
|
||||
it, ok := db.types[typ]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("newItem: type (%q) does not exist", typ)
|
||||
}
|
||||
|
||||
// get the dynamic type inside "it" like *book, *game, etc..
|
||||
t := reflect.TypeOf(it)
|
||||
|
||||
// get the pointer type's element type like book, game, etc...
|
||||
e := t.Elem()
|
||||
|
||||
// create a new pointer value of the type like new(book), new(game), etc...
|
||||
v := reflect.New(e)
|
||||
|
||||
// get the interface value and cast it to the item type
|
||||
it = v.Interface().(item)
|
||||
|
||||
return it, json.Unmarshal(data, &it)
|
||||
}
|
||||
|
||||
func (db *database) register(typ string, it item) {
|
||||
if db.types == nil {
|
||||
db.types = make(map[string]item)
|
||||
}
|
||||
db.types[typ] = it
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
[
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "moby dick",
|
||||
"Price": 10,
|
||||
"Published": 118281600
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "odyssey",
|
||||
"Price": 15,
|
||||
"Published": 733622400
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "hobbit",
|
||||
"Price": 25,
|
||||
"Published": -62135596800
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "puzzle",
|
||||
"Item": {
|
||||
"Title": "rubik's cube",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "minecraft",
|
||||
"Price": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "tetris",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "toy",
|
||||
"Item": {
|
||||
"Title": "yoda",
|
||||
"Price": 150
|
||||
}
|
||||
}
|
||||
]
|
@ -1,12 +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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
@ -1,40 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type list []item
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
var str strings.Builder
|
||||
for _, it := range l {
|
||||
str.WriteString(it.String())
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -1,60 +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"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, err := ioutil.ReadFile("database.json")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
var store list
|
||||
|
||||
db := database{list: &store}
|
||||
db.register("book", new(book))
|
||||
db.register("game", new(game))
|
||||
db.register("puzzle", new(puzzle))
|
||||
db.register("toy", new(toy))
|
||||
|
||||
if err := json.Unmarshal(data, &db); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Print(store)
|
||||
|
||||
/*
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, unknown},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
db := database{list: &store}
|
||||
|
||||
out, err := json.MarshalIndent(&db, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
|
||||
// store.discount(.5)
|
||||
// fmt.Print(store)
|
||||
*/
|
||||
}
|
@ -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,23 +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
|
||||
Price money
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
@ -1,64 +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
|
||||
}
|
||||
|
||||
// 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 strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
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 toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
@ -1,21 +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 book struct {
|
||||
product
|
||||
Published timestamp
|
||||
}
|
||||
|
||||
func (b *book) String() string {
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.Published)
|
||||
}
|
@ -1,91 +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"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type database struct {
|
||||
list *list
|
||||
types map[string]item // the registry of the types
|
||||
}
|
||||
|
||||
// load the list by decoding the data from any data source.
|
||||
func (db *database) load(r io.Reader) error {
|
||||
// ReadAll reads from `r` entirely into memory.
|
||||
// `data` contains the entire data in memory.
|
||||
data, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, db)
|
||||
}
|
||||
|
||||
func (db *database) MarshalJSON() ([]byte, error) {
|
||||
type encodable struct {
|
||||
Type string
|
||||
Item item
|
||||
}
|
||||
|
||||
var e []encodable
|
||||
|
||||
for _, it := range *db.list {
|
||||
t := reflect.TypeOf(it).Elem().Name()
|
||||
e = append(e, encodable{t, it})
|
||||
}
|
||||
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
func (db *database) UnmarshalJSON(data []byte) error {
|
||||
var decodables []struct {
|
||||
Type string
|
||||
Item json.RawMessage
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &decodables); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, d := range decodables {
|
||||
it, err := db.newItem(d.Item, d.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*db.list = append(*db.list, it)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *database) newItem(data []byte, typ string) (item, error) {
|
||||
it, ok := db.types[typ]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("newItem: type (%q) does not exist", typ)
|
||||
}
|
||||
|
||||
t := reflect.TypeOf(it)
|
||||
e := t.Elem()
|
||||
v := reflect.New(e)
|
||||
it = v.Interface().(item)
|
||||
|
||||
return it, json.Unmarshal(data, &it)
|
||||
}
|
||||
|
||||
func (db *database) register(typ string, it item) {
|
||||
if db.types == nil {
|
||||
db.types = make(map[string]item)
|
||||
}
|
||||
db.types[typ] = it
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
[
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "moby dick",
|
||||
"Price": 10,
|
||||
"Published": 118281600
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "odyssey",
|
||||
"Price": 15,
|
||||
"Published": 733622400
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "hobbit",
|
||||
"Price": 25,
|
||||
"Published": -62135596800
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "puzzle",
|
||||
"Item": {
|
||||
"Title": "rubik's cube",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "minecraft",
|
||||
"Price": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "tetris",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "toy",
|
||||
"Item": {
|
||||
"Title": "yoda",
|
||||
"Price": 150
|
||||
}
|
||||
}
|
||||
]
|
@ -1,12 +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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
@ -1,40 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type list []item
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
var str strings.Builder
|
||||
for _, it := range l {
|
||||
str.WriteString(it.String())
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -1,66 +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"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
/*
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, unknown},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
db := database{list: &store}
|
||||
|
||||
out, err := json.MarshalIndent(&db, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
|
||||
// store.discount(.5)
|
||||
// fmt.Print(store)
|
||||
*/
|
||||
|
||||
var store list
|
||||
|
||||
db := database{list: &store}
|
||||
db.register("book", new(book))
|
||||
db.register("game", new(game))
|
||||
db.register("puzzle", new(puzzle))
|
||||
db.register("toy", new(toy))
|
||||
|
||||
// load from a file
|
||||
// f, _ := os.Open("database.json")
|
||||
// db.load(f)
|
||||
// f.Close()
|
||||
|
||||
// load from a string
|
||||
// const data = `[
|
||||
// { "Type": "book", "Item": { "Title": "1984", "Price": 8, "Published": -649641600 } },
|
||||
// { "Type": "game", "Item": { "Title": "paperboy", "Price": 20 } }]`
|
||||
// r := strings.NewReader(data)
|
||||
// db.load(r)
|
||||
|
||||
// load from a web server
|
||||
res, _ := http.Get("https://inancgumus.github.io/x/database.json")
|
||||
db.load(res.Body)
|
||||
res.Body.Close()
|
||||
|
||||
fmt.Print(store)
|
||||
}
|
@ -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,23 +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
|
||||
Price money
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
@ -1,64 +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
|
||||
}
|
||||
|
||||
// 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 strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
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 toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
@ -1,21 +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 book struct {
|
||||
product
|
||||
Published timestamp
|
||||
}
|
||||
|
||||
func (b *book) String() string {
|
||||
return fmt.Sprintf("%s - (%s)", &b.product, b.Published)
|
||||
}
|
@ -1,35 +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"
|
||||
|
||||
// counter counts the total number of bytes read.
|
||||
type counter struct {
|
||||
r io.Reader
|
||||
n int
|
||||
}
|
||||
|
||||
// newCounter wraps `r` and returns a *counter.
|
||||
func newCounter(r io.Reader) *counter {
|
||||
return &counter{r: r}
|
||||
}
|
||||
|
||||
// Total bytes read.
|
||||
func (c *counter) total() int {
|
||||
return c.n
|
||||
}
|
||||
|
||||
// Read counts the number of bytes read from the underlying reader.
|
||||
func (c *counter) Read(p []byte) (n int, err error) {
|
||||
n, err = c.r.Read(p) // read from the underlying reader
|
||||
if n > 0 {
|
||||
c.n += n // sum the number of bytes read with the total bytes counted
|
||||
}
|
||||
return n, err // return the number of bytes read and an error
|
||||
}
|
@ -1,112 +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"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type database struct {
|
||||
list *list
|
||||
types map[string]item
|
||||
}
|
||||
|
||||
// load the list by decoding the data from any kind of data source.
|
||||
func (db *database) load(r io.Reader) error {
|
||||
return json.NewDecoder(r).Decode(db)
|
||||
}
|
||||
|
||||
func (db *database) MarshalJSON() ([]byte, error) {
|
||||
type encodable struct {
|
||||
Type string
|
||||
Item item
|
||||
}
|
||||
|
||||
var e []encodable
|
||||
|
||||
for _, it := range *db.list {
|
||||
t := reflect.TypeOf(it).Elem().Name()
|
||||
e = append(e, encodable{t, it})
|
||||
}
|
||||
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
func (db *database) UnmarshalJSON(data []byte) error {
|
||||
var decodables []struct {
|
||||
Type string
|
||||
Item json.RawMessage
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &decodables); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, d := range decodables {
|
||||
it, err := db.newItem(d.Item, d.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*db.list = append(*db.list, it)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *database) newItem(data []byte, typ string) (item, error) {
|
||||
it, ok := db.types[typ]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("newItem: type (%q) does not exist", typ)
|
||||
}
|
||||
|
||||
t := reflect.TypeOf(it)
|
||||
e := t.Elem()
|
||||
v := reflect.New(e)
|
||||
it = v.Interface().(item)
|
||||
|
||||
return it, json.Unmarshal(data, &it)
|
||||
}
|
||||
|
||||
func (db *database) register(typ string, it item) {
|
||||
if db.types == nil {
|
||||
db.types = make(map[string]item)
|
||||
}
|
||||
db.types[typ] = it
|
||||
}
|
||||
|
||||
// I put it here as a reference implementation of manually reading from a reader.
|
||||
// func (db *database) loadX(r io.Reader) error {
|
||||
// buf := make([]byte, 64)
|
||||
//
|
||||
// var (
|
||||
// data []byte
|
||||
// n int
|
||||
// err error
|
||||
// )
|
||||
//
|
||||
// for {
|
||||
// n, err = r.Read(buf)
|
||||
//
|
||||
// if n > 0 {
|
||||
// data = append(data, buf[:n]...)
|
||||
// }
|
||||
//
|
||||
// if err != nil {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if err != io.EOF {
|
||||
// return err
|
||||
// }
|
||||
// return json.Unmarshal(data, db)
|
||||
// }
|
@ -1,54 +0,0 @@
|
||||
[
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "moby dick",
|
||||
"Price": 10,
|
||||
"Published": 118281600
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "odyssey",
|
||||
"Price": 15,
|
||||
"Published": 733622400
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "book",
|
||||
"Item": {
|
||||
"Title": "hobbit",
|
||||
"Price": 25,
|
||||
"Published": -62135596800
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "puzzle",
|
||||
"Item": {
|
||||
"Title": "rubik's cube",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "minecraft",
|
||||
"Price": 20
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "game",
|
||||
"Item": {
|
||||
"Title": "tetris",
|
||||
"Price": 5
|
||||
}
|
||||
},
|
||||
{
|
||||
"Type": "toy",
|
||||
"Item": {
|
||||
"Title": "yoda",
|
||||
"Price": 150
|
||||
}
|
||||
}
|
||||
]
|
Binary file not shown.
@ -1,12 +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
|
||||
|
||||
type game struct {
|
||||
product
|
||||
}
|
@ -1,40 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type item interface {
|
||||
discount(ratio float64)
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type list []item
|
||||
|
||||
func (l list) String() string {
|
||||
if len(l) == 0 {
|
||||
return "Sorry. We're waiting for delivery 🚚."
|
||||
}
|
||||
|
||||
var str strings.Builder
|
||||
for _, it := range l {
|
||||
str.WriteString(it.String())
|
||||
str.WriteRune('\n')
|
||||
}
|
||||
|
||||
return str.String()
|
||||
}
|
||||
|
||||
func (l list) discount(ratio float64) {
|
||||
for _, it := range l {
|
||||
it.discount(ratio)
|
||||
}
|
||||
}
|
@ -1,96 +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 (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
/*
|
||||
store := list{
|
||||
&book{product{"moby dick", 10}, toTimestamp(118281600)},
|
||||
&book{product{"odyssey", 15}, toTimestamp("733622400")},
|
||||
&book{product{"hobbit", 25}, unknown},
|
||||
&puzzle{product{"rubik's cube", 5}},
|
||||
&game{product{"minecraft", 20}},
|
||||
&game{product{"tetris", 5}},
|
||||
&toy{product{"yoda", 150}},
|
||||
}
|
||||
|
||||
db := database{list: &store}
|
||||
|
||||
out, err := json.MarshalIndent(&db, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(out))
|
||||
|
||||
// store.discount(.5)
|
||||
// fmt.Print(store)
|
||||
*/
|
||||
|
||||
var store list
|
||||
|
||||
db := database{list: &store}
|
||||
db.register("book", new(book))
|
||||
db.register("game", new(game))
|
||||
db.register("puzzle", new(puzzle))
|
||||
db.register("toy", new(toy))
|
||||
|
||||
// load from a file
|
||||
// f, _ := os.Open("database.json")
|
||||
// db.load(f)
|
||||
// f.Close()
|
||||
|
||||
// load from a string
|
||||
// const data = `[
|
||||
// { "Type": "book", "Item": { "Title": "1984", "Price": 8, "Published": -649641600 } },
|
||||
// { "Type": "game", "Item": { "Title": "paperboy", "Price": 20 } }]`
|
||||
// r := strings.NewReader(data)
|
||||
// db.load(r)
|
||||
|
||||
// load from a web server
|
||||
// res, _ := http.Get("https://inancgumus.github.io/x/database.json")
|
||||
// db.load(res.Body)
|
||||
// res.Body.Close()
|
||||
|
||||
// decompress the reader while reading from it
|
||||
// res, _ := http.Get("https://inancgumus.github.io/x/database.json.gz")
|
||||
// gr, _ := gzip.NewReader(res.Body)
|
||||
// db.load(gr)
|
||||
// gr.Close()
|
||||
// res.Body.Close()
|
||||
|
||||
// decompress and count the number of compressed bytes read
|
||||
// res, _ := http.Get("https://inancgumus.github.io/x/database.json.gz")
|
||||
// c := newCounter(res.Body) // count the bytes read
|
||||
// gr, _ := gzip.NewReader(c)
|
||||
|
||||
// db.load(gr)
|
||||
// fmt.Printf("%d bytes read.\n\n", c.total())
|
||||
|
||||
// gr.Close()
|
||||
// res.Body.Close()
|
||||
|
||||
// count the number of compressed bytes read from the web server then decompress
|
||||
res, _ := http.Get("https://inancgumus.github.io/x/database.json.gz")
|
||||
gr, _ := gzip.NewReader(res.Body)
|
||||
c := newCounter(gr)
|
||||
|
||||
db.load(c)
|
||||
fmt.Printf("%d bytes read.\n\n", c.total())
|
||||
|
||||
gr.Close()
|
||||
res.Body.Close()
|
||||
|
||||
fmt.Print(store)
|
||||
}
|
@ -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,23 +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
|
||||
Price money
|
||||
}
|
||||
|
||||
func (p *product) String() string {
|
||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
||||
}
|
||||
|
||||
func (p *product) discount(ratio float64) {
|
||||
p.Price *= money(1 - ratio)
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type puzzle struct {
|
||||
product
|
||||
}
|
@ -1,64 +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
|
||||
}
|
||||
|
||||
// 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 strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||
}
|
||||
|
||||
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 toTimestamp(v interface{}) timestamp {
|
||||
var t int
|
||||
|
||||
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:
|
||||
// book{title: "hobbit", price: 25},
|
||||
return timestamp{}
|
||||
}
|
||||
|
||||
return timestamp{
|
||||
Time: time.Unix(int64(t), 0),
|
||||
}
|
||||
}
|
@ -1,12 +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
|
||||
|
||||
type toy struct {
|
||||
product
|
||||
}
|
Reference in New Issue
Block a user