-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_request.go
252 lines (221 loc) · 6.17 KB
/
decode_request.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
package http
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/mmadfox/go-gpsgen"
"github.com/mmadfox/go-gpsgen/curve"
"github.com/mmadfox/go-gpsgen/properties"
"github.com/mmadfox/gpsgend/internal/generator"
"github.com/mmadfox/gpsgend/internal/types"
"github.com/valyala/fasthttp"
)
func decodeID(c *fiber.Ctx, name string) (types.ID, error) {
uid, err := types.ParseID(c.Params(name))
if err != nil {
return types.ID{}, fmt.Errorf("%w: failed to decode tracker.%s: %w",
errBadRequest, name, err)
}
return uid, err
}
func decodeIndex(c *fiber.Ctx, name string) (int, error) {
index, err := strconv.Atoi(c.Params(name))
if err != nil {
return -1, fmt.Errorf("%w: failed to decode tracker.%s: %w",
errBadRequest, name, err)
}
return index, err
}
func decodeAddTrackerRequest(r *fasthttp.Request) (*generator.NewTrackerOptions, error) {
req := new(addTrackerRequest)
if err := json.Unmarshal(r.Body(), &req); err != nil {
return nil, fmt.Errorf("%w: %v", errBadRequest, err)
}
opts := generator.NewTrackerOptions{}
if len(req.Model) > 0 {
model, err := types.ParseModel(req.Model)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.model: %w", err)
}
opts.Model = &model
}
if len(req.Color) > 0 {
color, err := types.ParseColor(req.Color)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.color: %w", err)
}
opts.Color = &color
}
if len(req.CustomID) > 0 {
cid, err := types.ParseCustomID(req.CustomID)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.customID: %w", err)
}
opts.UserID = &cid
}
if len(req.Descr) > 0 {
descr, err := types.ParseDescription(req.Descr)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.description: %w", err)
}
opts.Descr = &descr
}
opts.SkipOffline = req.SkipOffline
elevation, err := types.ParseElevation(
req.Elevation.Min,
req.Elevation.Max,
int(req.Elevation.Amplitude),
curve.CurveMode(req.Elevation.Mode),
)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.elevation: %w", err)
}
opts.Elevation = elevation
offline, err := types.ParseOffline(
int(req.Offline.Min),
int(req.Offline.Max),
)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.offline: %w", err)
}
opts.Offline = offline
battery, err := types.ParseBattery(
req.Battery.Min,
req.Battery.Max,
time.Duration(req.Battery.ChargeTime),
)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.battery: %w", err)
}
opts.Battery = battery
if len(req.Props) > 0 {
opts.Props = (*properties.Properties)(&req.Props)
}
speed, err := types.ParseSpeed(
req.Speed.Min,
req.Speed.Max,
int(req.Speed.Amplitude),
)
if err != nil {
return nil, fmt.Errorf("failed to decode tracker.speed: %w", err)
}
opts.Speed = speed
return &opts, nil
}
func decodeUpdateTrackerRequest(c *fiber.Ctx) (generator.UpdateTrackerOptions, error) {
opts := generator.UpdateTrackerOptions{}
req := updateTrackerRequest{}
if err := json.NewDecoder(c.Request().BodyStream()).Decode(&req); err != nil {
return opts, fmt.Errorf("%w: %v", errBadRequest, err)
}
if len(req.Model) > 0 {
model, err := types.ParseModel(req.Model)
if err != nil {
return opts, fmt.Errorf("%w: failed to decode tracker.model: %v",
errBadRequest, err)
}
opts.Model = &model
}
if len(req.Color) > 0 {
color, err := types.ParseColor(req.Color)
if err != nil {
return opts, fmt.Errorf("%w: failed to decode tracker.color: %v",
errBadRequest, err)
}
opts.Color = &color
}
if len(req.UserID) > 0 {
cid, err := types.ParseCustomID(req.UserID)
if err != nil {
return opts, fmt.Errorf("%w: failed to decode tracker.customID: %v",
errBadRequest, err)
}
opts.UserID = &cid
}
if len(req.Descr) > 0 {
descr, err := types.ParseDescription(req.Descr)
if err != nil {
return opts, fmt.Errorf("%w: failed to decode tracker.description: %v",
errBadRequest, err)
}
opts.Descr = &descr
}
return opts, nil
}
func decodeAddRoutesRequest(c *fiber.Ctx) ([]*gpsgen.Route, error) {
if len(c.Body()) < 60 {
return nil, fmt.Errorf("%w: no data", errBadRequest)
}
var routes []*gpsgen.Route
var err error
data := c.Body()
header := data[:60]
if bytes.Contains(header, []byte("{")) {
routes, err = gpsgen.DecodeGeoJSONRoutes(data)
} else if bytes.Contains(header, []byte("<")) {
routes, err = gpsgen.DecodeGPXRoutes(data)
} else {
err = fmt.Errorf("%w: invalid data format",
errBadRequest)
}
if err != nil {
return nil, fmt.Errorf("%w: %v", errBadRequest, err)
}
return routes, nil
}
func decodeAddSensorRequest(c *fiber.Ctx) (*types.Sensor, error) {
req := sensor{}
if err := json.Unmarshal(c.Body(), &req); err != nil {
return nil, fmt.Errorf("%w: %v", errBadRequest, err)
}
return types.NewSensor(req.Name, req.Min, req.Max, req.Amplitude, req.Mode)
}
func decodeSearchTrackersRequest(c *fiber.Ctx) (generator.Filter, error) {
q := c.Queries()
filter := generator.Filter{}
if limitStr, ok := q["limit"]; ok {
limit, err := strconv.ParseInt(limitStr, 10, 64)
if err != nil {
return filter, fmt.Errorf("%w: failed to decode limit param: %v",
errBadRequest, err)
}
filter.Limit = limit
} else {
filter.Limit = 50
}
if offsetStr, ok := q["offset"]; ok {
offset, err := strconv.ParseInt(offsetStr, 10, 64)
if err != nil {
return filter, fmt.Errorf("%w: failed to decode offset param: %v",
errBadRequest, err)
}
filter.Offset = offset
}
if trackerIDsStr, ok := q["id"]; ok {
rawIDs := strings.Split(trackerIDsStr, ",")
filter.TrackerIDs = make([]string, 0, len(rawIDs))
for i := 0; i < len(rawIDs); i++ {
id := strings.TrimSpace(rawIDs[i])
if len(id) == 0 {
return filter, fmt.Errorf("%w: failed to decode id param", errBadRequest)
}
filter.TrackerIDs = append(filter.TrackerIDs, id)
}
}
if statusStr, ok := q["status"]; ok {
status, err := strconv.Atoi(statusStr)
if err != nil {
return filter, fmt.Errorf("%w: failed to decode status param: %v",
errBadRequest, err)
}
filter.Status = status
}
if term, ok := q["term"]; ok {
filter.Term = strings.TrimSpace(term)
}
return filter, nil
}