-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload.go
91 lines (78 loc) · 1.63 KB
/
payload.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
package tohru
import (
"encoding/json"
"fmt"
)
type JsonPayload map[string]interface{}
func (p JsonPayload) WithOrder(o order) error {
if err := o.valid(); err != nil {
return err
}
p["_order_by"] = string(o)
return nil
}
func (p JsonPayload) WithOffset(offset int) error {
if offset < 0 {
return fmt.Errorf("negative Offset")
}
p["_offset"] = offset
return nil
}
func (p JsonPayload) WithLimit(limit int) error {
if limit <= 0 {
return fmt.Errorf("negative limit or zero")
}
p["_limit"] = limit
return nil
}
func (p JsonPayload) WithListType(l listType) error {
if err := l.valid(); err != nil {
return err
}
p["list_type"] = string(l)
return nil
}
func (p JsonPayload) WithJustInfo(info string) error {
switch info {
case "Yes", "No":
p["just_info"] = info
return nil
default:
return fmt.Errorf("value must be Yes or No")
}
}
func (p JsonPayload) WithName(name string) {
p["anime_name"] = name
}
func (p JsonPayload) WithSeason(s season) error {
if err := s.valid(); err != nil {
return err
}
p["anime_season"] = string(s)
return nil
}
func (p JsonPayload) WithReleaseYear(year int) error {
if year <= 0 {
return fmt.Errorf("year must be positive")
}
p["anime_release_years"] = year
return nil
}
func (p JsonPayload) WithAnimeId(id int) error {
if id <= 0 {
return fmt.Errorf("anime id must be positive")
}
p["anime_id"] = id
return nil
}
func (p JsonPayload) WithEpisodeId(id int) error {
if id <= 0 {
return fmt.Errorf("episode id must be positive")
}
p["episode_id"] = id
return nil
}
func (p JsonPayload) ToJson() (string, error) {
json, err := json.Marshal(p)
return string(json), err
}