remove: reflection decoder
This commit is contained in:
@ -1,33 +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"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func fromFile(f *os.File) (list, error) {
|
|
||||||
return fromReader(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromReader(r io.Reader) (list, error) {
|
|
||||||
data, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var store list
|
|
||||||
|
|
||||||
if err := json.Unmarshal(data, &store); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return store, nil
|
|
||||||
}
|
|
@ -1,42 +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"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
dec := make(decoder)
|
|
||||||
dec["book"] = book{}
|
|
||||||
dec["game"] = game{}
|
|
||||||
dec["puzzle"] = puzzle{}
|
|
||||||
dec["toy"] = toy{}
|
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(os.Stdin)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
store, err := dec.decode(data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(store)
|
|
||||||
|
|
||||||
// store.discount(.5)
|
|
||||||
// fmt.Print(store)
|
|
||||||
|
|
||||||
// ----
|
|
||||||
// data, err := save(store)
|
|
||||||
// fmt.Println(string(data), err)
|
|
||||||
}
|
|
@ -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"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
dec := decoder{
|
|
||||||
"book": book{},
|
|
||||||
"game": game{},
|
|
||||||
"puzzle": puzzle{},
|
|
||||||
"toy": toy{},
|
|
||||||
}
|
|
||||||
|
|
||||||
store, err := fromReader(os.Stdin)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(store)
|
|
||||||
}
|
|
@ -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,107 +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"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
type database struct {
|
|
||||||
list *list
|
|
||||||
types map[string]item
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *database) save() error {
|
|
||||||
data, err := json.MarshalIndent(db, "", "\t")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ioutil.WriteFile("database.json", data, os.ModePerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *database) load() error {
|
|
||||||
data, err := ioutil.ReadFile("database.json")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = json.Unmarshal(data, db); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *database) MarshalJSON() ([]byte, error) {
|
|
||||||
type encodable struct {
|
|
||||||
Category 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
|
|
||||||
cat := reflect.TypeOf(it).Elem().Name()
|
|
||||||
e = append(e, encodable{cat, it})
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Marshal(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *database) UnmarshalJSON(data []byte) error {
|
|
||||||
var decodables []struct {
|
|
||||||
Category string
|
|
||||||
Item json.RawMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(data, &decodables); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range decodables {
|
|
||||||
it, err := db.newItem(d.Category)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(d.Item, &it); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*db.list = append(*db.list, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *database) register(category string, t item) {
|
|
||||||
if db.types == nil {
|
|
||||||
db.types = make(map[string]item)
|
|
||||||
}
|
|
||||||
db.types[category] = t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *database) newItem(category string) (item, error) {
|
|
||||||
it, ok := db.types[category]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("register %q within database before decoding", category)
|
|
||||||
}
|
|
||||||
|
|
||||||
t := reflect.TypeOf(it).Elem()
|
|
||||||
v := reflect.New(t)
|
|
||||||
return v.Interface().(item), nil
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"Category": "book",
|
|
||||||
"Item": {
|
|
||||||
"Title": "moby dick",
|
|
||||||
"Price": 10,
|
|
||||||
"Published": 118281600
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Category": "book",
|
|
||||||
"Item": {
|
|
||||||
"Title": "odyssey",
|
|
||||||
"Price": 15,
|
|
||||||
"Published": 733622400
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Category": "book",
|
|
||||||
"Item": {
|
|
||||||
"Title": "hobbit",
|
|
||||||
"Price": 25,
|
|
||||||
"Published": -62135596800
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Category": "puzzle",
|
|
||||||
"Item": {
|
|
||||||
"Title": "rubik's cube",
|
|
||||||
"Price": 5
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Category": "game",
|
|
||||||
"Item": {
|
|
||||||
"Title": "minecraft",
|
|
||||||
"Price": 20
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Category": "game",
|
|
||||||
"Item": {
|
|
||||||
"Title": "tetris",
|
|
||||||
"Price": 5
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Category": "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,41 +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(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,63 +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() {
|
|
||||||
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 := db.load(); err != nil {
|
|
||||||
// log.Fatalln(err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile("database.json")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = json.Unmarshal(data, &store); 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}
|
|
||||||
if err := db.save(); err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// out, err := json.MarshalIndent(db, "", "\t")
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatalln(err)
|
|
||||||
// }
|
|
||||||
// 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,77 +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"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Mon Jan 2 15:04:05 -0700 MST 2006
|
|
||||||
const layout = "2006/01"
|
|
||||||
|
|
||||||
// unknown is the zero value of a timestamp.
|
|
||||||
var unknown = timestamp(time.Time{})
|
|
||||||
|
|
||||||
// timestamp prints timestamps, it's a stringer.
|
|
||||||
// It is useful even if it's zero.
|
|
||||||
type timestamp time.Time
|
|
||||||
|
|
||||||
// MarshalJSON is an implementation of the json.Marshaler interface.
|
|
||||||
// json.Marshal and json.Encode call this method.
|
|
||||||
func (ts timestamp) MarshalJSON() (out []byte, err error) {
|
|
||||||
u := time.Time(ts).Unix()
|
|
||||||
return strconv.AppendInt(out, u, 10), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON is an implementation of the json.Unmarshaler interface.
|
|
||||||
// json.Unmarshal and json.Decode call this method.
|
|
||||||
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
|
||||||
s := string(data)
|
|
||||||
|
|
||||||
// Let the ParseInt parse quoted strings.
|
|
||||||
us, err := strconv.Unquote(s)
|
|
||||||
if err == nil {
|
|
||||||
s = us
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always overwrite the timestamp when decoding.
|
|
||||||
*ts = unknown
|
|
||||||
|
|
||||||
// Handle the numeric case.
|
|
||||||
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
|
|
||||||
*ts = timestamp(time.Unix(n, 0))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts timestamp) String() string {
|
|
||||||
t := time.Time(ts)
|
|
||||||
|
|
||||||
if t.IsZero() {
|
|
||||||
return "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
func toTimestamp(v interface{}) timestamp {
|
|
||||||
var t int
|
|
||||||
|
|
||||||
switch v := v.(type) {
|
|
||||||
case int:
|
|
||||||
t = v
|
|
||||||
case string:
|
|
||||||
t, _ = strconv.Atoi(v)
|
|
||||||
default:
|
|
||||||
return unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
return timestamp(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