-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.go
104 lines (81 loc) · 2.59 KB
/
book.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2017 Mattias Pernhult. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goiaf
import (
"encoding/json"
"time"
)
// Book represents the book resources that is returned from the api.
type Book struct {
// The hypermedia URL of this resource.
URL string
// The name of this book.
Name string
// The International Standard Book Number that uniquely identifies this book.
// The format used is ISBN-13.
ISBN string
// An array of names of the authors that wrote this book.
Authors []string
// The number of pages in this book.
NumberOfPages int
// The company that published this book.
Publisher string
// The country which this book was published in.
Country string
// The type of media this book was released in. Possible values are: Hardback,
// Hardcover, GraphicNovel and Paperback.
MediaType string
// The date, in ISO 8601 format, which this book was released.
Released time.Time
// An array of Character ids that has been in this book.
CharacterIds []int
// An array of Character ids that has had a POV-chapter in this book.
PovCharacterIds []int
}
type book struct {
URL string `json:"url"`
Name string `json:"name"`
ISBN string `json:"isbn"`
Authors []string `json:"authors"`
NumberOfPages int `json:"numberOfPages"`
Publisher string `json:"publisher"`
Country string `json:"country"`
MediaType string `json:"mediaType"`
Released DateTime `json:"released"`
Characters urlStringSlice `json:"characters"`
PovCharacters urlStringSlice `json:"povCharacters"`
}
func (b book) Convert() Book {
book := Book{
URL: b.URL,
Name: b.Name,
ISBN: b.ISBN,
Authors: b.Authors,
NumberOfPages: b.NumberOfPages,
Publisher: b.Publisher,
Country: b.Country,
MediaType: b.MediaType,
Released: b.Released.Value(),
CharacterIds: b.Characters.ids(),
PovCharacterIds: b.PovCharacters.ids(),
}
return book
}
type booksResponse struct {
links map[string]string
Books []book
}
func (booksResponse *booksResponse) Link(links map[string]string) {
booksResponse.links = links
}
func (booksResponse *booksResponse) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &booksResponse.Books)
}
func (booksResponse booksResponse) Convert() []Book {
books := []Book{}
for _, bookResponse := range booksResponse.Books {
books = append(books, bookResponse.Convert())
}
return books
}