-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
286 lines (218 loc) · 6.08 KB
/
types.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2015, Sgt. Kabukiman | MIT licensed
package srapi
import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
// Version is the package version.
const Version = "1.0"
// requestable describes anything that can turn itself into a request.
type requestable interface {
exists() bool
request(filter, *Sorting, string) request
}
// Link represent a generic link, most often used for API links.
type Link struct {
Relation string `json:"rel"`
URI string `json:"uri"`
}
// checks if the link exists
func (l *Link) exists() bool {
return l != nil
}
// request turns a link into a GET request.
func (l *Link) request(filter filter, sort *Sorting, embeds string) request {
relURL := l.URI[len(BaseURL):]
return request{"GET", relURL, filter, sort, nil, embeds}
}
// AssetLink is a link pointing to an image, having width and height values.
type AssetLink struct {
Link
Width int
Height int
}
// Pagination contains information on how to navigate through multiple pages
// of results.
type Pagination struct {
Offset int
Max int
Size int
Links []Link
}
// for the 'hasLinks' interface
func (p *Pagination) links() []Link {
return p.Links
}
// filter describes anything that can apply itself to a URL.
type filter interface {
applyToURL(*url.URL)
}
// Cursor represents the current position in a collection.
type Cursor struct {
Offset int
Max int
}
// applyToURL merged the filter into a URL.
func (c *Cursor) applyToURL(u *url.URL) {
if c == nil {
return
}
values := u.Query()
if c.Offset > 0 {
values.Set("offset", strconv.Itoa(c.Offset))
}
if c.Max > 0 {
values.Set("max", strconv.Itoa(c.Max))
}
u.RawQuery = values.Encode()
}
// Direction is a sorting order
type Direction int
const (
// Ascending sorts a...z
Ascending Direction = iota
// Descending sorts z...a
Descending
)
// NoEmbeds can be used to denote no embeds.
const NoEmbeds = ""
// Sorting represents the sorting options when requesting a list of items from the API.
type Sorting struct {
OrderBy string
Direction Direction
}
// applyToURL merged the filter into a URL.
func (s *Sorting) applyToURL(u *url.URL) {
if s == nil {
return
}
values := u.Query()
dir := "asc"
if s.Direction == Descending {
dir = "desc"
}
values.Set("orderby", s.OrderBy)
values.Set("direction", dir)
u.RawQuery = values.Encode()
}
// OptionalFlag represents a tri-state of true, false and undefined and is used for
// flags in collection filters.
type OptionalFlag int
const (
// Undefined represents an unset flag
Undefined OptionalFlag = iota
// Yes is true.
Yes
// No is false.
No
)
// applyToURL sets the flag in a query string if it's not Undefined.
func (f OptionalFlag) applyToQuery(name string, values *url.Values) {
if f == Yes || f == No {
values.Set(name, f.String())
}
}
// String returns a string representation.
func (f OptionalFlag) String() string {
switch f {
case Yes:
return "yes"
case No:
return "no"
default:
return ""
}
}
// TimingMethod specifies what time was measured for a run.
type TimingMethod string
const (
// TimingRealtime is realtime with loading times.
TimingRealtime TimingMethod = "realtime"
// TimingRealtimeWithoutLoads is realtime without loads.
TimingRealtimeWithoutLoads TimingMethod = "realtime_noloads"
// TimingIngameTime is using the in-game timer.
TimingIngameTime TimingMethod = "ingame"
)
// GameModLevel determines the power level of a moderator.
type GameModLevel string
const (
// NormalModerator users can do game-related things.
NormalModerator GameModLevel = "moderator"
// SuperModerator users can appoint other moderators.
SuperModerator GameModLevel = "super-moderator"
// UnknownModLevel is used for when moderators have been embedded and there
// is no information available about their actual level.
UnknownModLevel GameModLevel = "unknown"
)
// dateLayout describes the format for ISO 8601 dates
var dateLayout = "2006-01-02"
// ErrParseDate is an error that occurs when a JSON string is not a valid date
var ErrParseDate = errors.New(`ErrParseDate: should be a string formatted as "2006-01-02"`)
// Date is a custom time.Time wrapper that allows dates without times in JSON
// documents.
type Date struct {
time.Time
}
// MarshalJSON implements the json.Marshaler interface
func (d Date) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.Format(dateLayout) + `"`), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (d *Date) UnmarshalJSON(b []byte) error {
s := string(b)
if len(s) != len(`"2006-01-02"`) {
return ErrParseDate
}
ret, err := time.Parse(dateLayout, s[1:11])
if err != nil {
return err
}
d.Time = ret
return nil
}
// ErrParseDuration is an error that occurs when a JSON value is not a valid float value
var ErrParseDuration = errors.New(`ErrParseDuration: value should be a valid float`)
// Duration is a custom time.Time wrapper that allows dates without times in JSON
// documents.
type Duration struct {
time.Duration
}
// MarshalJSON implements the json.Marshaler interface
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%.3f", d.Seconds())), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (d *Duration) UnmarshalJSON(b []byte) error {
parsed, err := strconv.ParseFloat(string(b), 32)
if err != nil {
return ErrParseDuration
}
d.Duration = time.Duration(parsed * float64(time.Second))
return nil
}
// Format returns a human readable time in the form of "[[HH:]MM:]SS[.MS]".
func (d *Duration) Format() string {
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
seconds := int(d.Seconds()) % 60
milli := (d.Seconds() - float64(int(d.Seconds())))
var list []string
if hours > 0 {
list = append(list, fmt.Sprintf("%02d", hours))
}
if len(list) > 0 || minutes > 0 {
list = append(list, fmt.Sprintf("%02d", minutes))
}
if len(list) > 0 || seconds > 0 {
list = append(list, fmt.Sprintf("%02d", seconds))
}
formatted := strings.TrimPrefix(strings.Join(list, ":"), "0")
if milli >= 0.001 {
formatted += fmt.Sprintf(".%02d", int(milli*1000+0.5)) // +0.5 for easy rounding
}
return formatted
}