add: explanations to decode to iface
This commit is contained in:
@ -13,20 +13,26 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// database is responsible for encoding and decoding a list.
|
||||||
type database struct {
|
type database struct {
|
||||||
list *list
|
list *list
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *database) UnmarshalJSON(data []byte) error {
|
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 {
|
var decodables []struct {
|
||||||
Type string
|
Type string // The product type.
|
||||||
Item json.RawMessage
|
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 {
|
if err := json.Unmarshal(data, &decodables); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decode the "Item" part for each json object.
|
||||||
for _, d := range decodables {
|
for _, d := range decodables {
|
||||||
it, err := db.newItem(d.Item, d.Type)
|
it, err := db.newItem(d.Item, d.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -39,7 +45,8 @@ func (db *database) UnmarshalJSON(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *database) newItem(data []byte, itemType string) (it item, _ error) {
|
// 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 {
|
switch itemType {
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("newItem: type (%q) does not exist", itemType)
|
return nil, fmt.Errorf("newItem: type (%q) does not exist", itemType)
|
||||||
|
Reference in New Issue
Block a user