-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
192 lines (167 loc) · 3.04 KB
/
utils.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
package main
import (
"encoding/json"
"io"
"net/http"
"os"
"strings"
)
type Map map[string]any
func (m Map)Get(k string)(any){
return m[k]
}
func (m Map)GetBool(k string)(v bool, ok bool){
v, ok = m[k].(bool)
return
}
func (m Map)GetInt(k string)(v int, ok bool){
if v, ok = m[k].(int); !ok {
var v0 float64
if v0, ok = m[k].(float64); ok {
v = (int)(v0)
}
}
return
}
func (m Map)GetInt64(k string)(v int64, ok bool){
if v, ok = m[k].(int64); !ok {
var (
v0 int
v1 float64
)
if v0, ok = m[k].(int); ok {
v = (int64)(v0)
}else if v1, ok = m[k].(float64); ok {
v = (int64)(v1)
}
}
return
}
func (m Map)GetFloat(k string)(v float64, ok bool){
v, ok = m[k].(float64)
return
}
func (m Map)GetString(k string)(v string, ok bool){
v, ok = m[k].(string)
return
}
func (m Map)GetList(k string)(v List, ok bool){
var v0 []any
if v0, ok = m[k].([]any); ok {
v = (List)(v0)
}
return
}
func (m Map)GetMap(k string)(v Map, ok bool){
var v0 map[string]any
if v0, ok = m[k].(map[string]any); ok {
v = (Map)(v0)
}
return
}
type List []any
func (l List)Get(i int)(any){
if i >= len(l) {
return nil
}
return l[i]
}
func (l List)GetBool(i int)(v bool, ok bool){
if i >= len(l) {
return
}
v, ok = l[i].(bool)
return
}
func (l List)GetInt(i int)(v int, ok bool){
if i >= len(l) {
return
}
if v, ok = l[i].(int); !ok {
var v0 float64
if v0, ok = l[i].(float64); ok {
v = (int)(v0)
}
}
return
}
func (l List)GetFloat(i int)(v float64, ok bool){
if i >= len(l) {
return
}
v, ok = l[i].(float64)
return
}
func (l List)GetString(i int)(v string, ok bool){
if i >= len(l) {
return
}
v, ok = l[i].(string)
return
}
func (l List)GetList(i int)(v List, ok bool){
if i >= len(l) {
return
}
var v0 []any
if v0, ok = l[i].([]any); ok {
v = (List)(v0)
}
return
}
func (l List)GetMap(i int)(v Map, ok bool){
if i >= len(l) {
return
}
var v0 map[string]any
if v0, ok = l[i].(map[string]any); ok {
v = (Map)(v0)
}
return
}
func inRange(n, max int)(int){
n %= max
if n < 0 {
return max + n
}
return n
}
func writeJson(rw http.ResponseWriter, code int, data any)(err error){
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(code)
return json.NewEncoder(rw).Encode(data)
}
func readJsonBody(req *http.Request, ptr any)(err error){
defer req.Body.Close()
return json.NewDecoder(req.Body).Decode(ptr)
}
func splitByte(str string, b byte)(left, right string){
i := strings.IndexByte(str, b)
if i < 0 {
return str, ""
}
return str[:i], str[i + 1:]
}
func splitByteR(str string, b byte)(left, right string){
i := strings.LastIndexByte(str, b)
if i < 0 {
return "", str
}
return str[:i], str[i + 1:]
}
func safeDownload(reader io.Reader, path string)(err error){
var fd *os.File
if fd, err = os.OpenFile(path + ".downloading", os.O_RDWR | os.O_CREATE | os.O_TRUNC, 0644); err != nil {
return
}
defer os.Remove(fd.Name())
_, err = io.Copy(fd, reader)
fd.Close()
if err != nil {
return
}
if err = os.Rename(fd.Name(), path); err != nil {
return
}
return nil
}