forked from CrunchyData/pg_featureserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param.go
351 lines (319 loc) · 8.92 KB
/
param.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
/*
Copyright 2019 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/CrunchyData/pg_featureserv/api"
"github.com/CrunchyData/pg_featureserv/conf"
"github.com/CrunchyData/pg_featureserv/data"
)
func parseRequestParams(r *http.Request) (api.RequestParam, error) {
queryValues := r.URL.Query()
paramValues := extractSingleArgs(queryValues)
param := api.RequestParam{
Limit: conf.Configuration.Paging.LimitDefault,
Offset: 0,
Precision: -1,
Values: paramValues,
}
// --- limit parameter
limit, err := parseLimit(paramValues)
if err != nil {
return param, err
}
param.Limit = limit
// --- offset parameter
offset, err := parseInt(paramValues, api.ParamOffset, 0, conf.Configuration.Paging.LimitMax, 0)
if err != nil {
return param, err
}
param.Offset = offset
// --- bbox parameter
bbox, err := parseBbox(paramValues)
if err != nil {
return param, err
}
param.Bbox = bbox
// --- properties parameter
props, err := parseProperties(paramValues)
if err != nil {
return param, err
}
param.Properties = props
// --- orderBy parameter
orderBy, err := parseOrderBy(paramValues)
if err != nil {
return param, err
}
param.OrderBy = orderBy
// --- precision parameter
precision, err := parseInt(paramValues, api.ParamPrecision, 0, 20, -1)
if err != nil {
return param, err
}
param.Precision = precision
// --- transform parameter
param.TransformFuns, err = parseTransform(paramValues)
if err != nil {
return param, err
}
return param, nil
}
func extractSingleArgs(queryArgs url.Values) api.NameValMap {
vals := make(map[string]string)
for keyRaw := range queryArgs {
queryval := queryArgs.Get(keyRaw)
key := strings.ToLower(keyRaw)
vals[key] = queryval
}
return vals
}
func parseInt(values api.NameValMap, key string, minVal int, maxVal int, defaultVal int) (int, error) {
valStr := values[key]
// key not present or missing value
if len(valStr) < 1 {
return defaultVal, nil
}
val, err := strconv.Atoi(valStr)
if err != nil {
return 0, fmt.Errorf(api.ErrMsgInvalidParameterValue, key, valStr)
}
if val < minVal {
val = minVal
}
if val > maxVal {
val = maxVal
}
return val, nil
}
func parseLimit(values api.NameValMap) (int, error) {
val := values[api.ParamLimit]
if len(val) < 1 {
return conf.Configuration.Paging.LimitDefault, nil
}
limit, err := strconv.Atoi(val)
if err != nil {
return 0, fmt.Errorf(api.ErrMsgInvalidParameterValue, api.ParamLimit, val)
}
if limit < 0 || limit > conf.Configuration.Paging.LimitMax {
limit = conf.Configuration.Paging.LimitMax
}
return limit, nil
}
/*
parseBbox parses the bbox query parameter, if present, or nll if not
This has the format bbox=minLon,minLat,maxLon,maxLat.
*/
func parseBbox(values api.NameValMap) (*data.Extent, error) {
val := values[api.ParamBbox]
if len(val) < 1 {
return nil, nil
}
nums := strings.Split(val, ",")
var isErr = false
if len(nums) != 4 {
return nil, fmt.Errorf(api.ErrMsgInvalidParameterValue, api.ParamBbox, val)
}
minLon, err := strconv.ParseFloat(nums[0], 64)
if err != nil {
isErr = true
}
minLat, err := strconv.ParseFloat(nums[1], 64)
if err != nil {
isErr = true
}
maxLon, err := strconv.ParseFloat(nums[2], 64)
if err != nil {
isErr = true
}
maxLat, err := strconv.ParseFloat(nums[3], 64)
if err != nil {
isErr = true
}
if isErr {
return nil, fmt.Errorf(api.ErrMsgInvalidParameterValue, api.ParamBbox, val)
}
var bbox = data.Extent{Minx: minLon, Miny: minLat, Maxx: maxLon, Maxy: maxLat}
return &bbox, nil
}
// parseProperties computes a lower-case, unique list
// of property names to be returned
func parseProperties(values api.NameValMap) ([]string, error) {
val := values[api.ParamProperties]
if len(val) < 1 {
return nil, nil
}
namesRaw := strings.Split(val, ",")
var names []string
nameMap := make(map[string]bool)
for _, name := range namesRaw {
nameLow := strings.ToLower(name)
// if a new name add to list
if _, ok := nameMap[nameLow]; !ok {
names = append(names, nameLow)
nameMap[nameLow] = true
}
}
return names, nil
}
const OrderByDirSep = ":"
const OrderByDirD = "d"
const OrderByDirA = "a"
// parseOrderBy determines an order by array
func parseOrderBy(values api.NameValMap) ([]data.Ordering, error) {
var orderBy []data.Ordering
val := values[api.ParamOrderBy]
if len(val) < 1 {
return orderBy, nil
}
valLow := strings.ToLower(val)
nameDir := strings.Split(valLow, OrderByDirSep)
name := nameDir[0]
isDesc := false
var err error
if len(nameDir) >= 2 {
dirSpec := nameDir[1]
isDesc, err = parseOrderByDir(dirSpec)
if err != nil {
return nil, err
}
}
orderBy = append(orderBy, data.Ordering{Name: name, IsDesc: isDesc})
return orderBy, nil
}
func parseOrderByDir(dir string) (bool, error) {
if dir == OrderByDirD {
return true, nil
}
if dir == OrderByDirA {
return false, nil
}
err := fmt.Errorf(api.ErrMsgInvalidParameterValue, api.ParamOrderBy, dir)
return false, err
}
// normalizePropNames converts the request property name list (if any)
// into a clean list of valid, unique column names
// If the request properties list is empty,
// the full column list is returned
func normalizePropNames(requestNames []string, colNames []string) []string {
// no props given => use all properties
if len(requestNames) == 0 {
return colNames
}
nameSet := toNameSet(requestNames)
// select cols which appear in set
var propNames []string
for _, colName := range colNames {
if _, ok := nameSet[colName]; ok {
propNames = append(propNames, colName)
}
}
return propNames
}
func toNameSet(strs []string) map[string]bool {
set := make(map[string]bool)
for _, s := range strs {
sLow := strings.ToLower(s)
set[sLow] = true
}
return set
}
const transformFunSep = "|"
const transformParamSep = ","
const functionPrefixST = "st_"
var transformFunctionWhitelist map[string]string
func initTransforms(funNames []string) {
transformFunctionWhitelist = make(map[string]string)
for _, name := range funNames {
nameLow := strings.ToLower(name)
transformFunctionWhitelist[nameLow] = name
}
}
// actualFunctionName converts an input function name
// to an actual function name from the whitelist
func actualFunctionName(name string) string {
nameLow := strings.ToLower(name)
if actual, ok := transformFunctionWhitelist[nameLow]; ok {
return actual
}
if !strings.HasPrefix(nameLow, functionPrefixST) {
// supply ST_ prefix if not there and try again
stName := functionPrefixST + nameLow
if actual, ok := transformFunctionWhitelist[stName]; ok {
return actual
}
}
return ""
}
func parseTransform(values api.NameValMap) ([]data.TransformFunction, error) {
val := values[api.ParamTransform]
if len(val) < 1 {
return nil, nil
}
funDefs := strings.Split(val, transformFunSep)
funList := make([]data.TransformFunction, 0)
for _, fun := range funDefs {
tf := parseTransformFun(fun)
actualName := actualFunctionName(tf.Name)
if len(actualName) <= 0 {
err := fmt.Errorf(api.ErrMsgInvalidParameterValue, api.ParamTransform, tf.Name)
return nil, err
}
tf.Name = actualName
if tf.Name != "" {
funList = append(funList, tf)
}
}
return funList, nil
}
func parseTransformFun(def string) data.TransformFunction {
// check for function parameter
atoms := strings.Split(def, transformParamSep)
name := atoms[0]
args := atoms[1:]
// TODO: harden this by checking arg is a valid number
// TODO: have whitelist for function names?
return data.TransformFunction{Name: name, Arg: args}
}
// parseFilter creates a filter list from applicable query parameters
func parseFilter(paramMap map[string]string, colNameMap map[string]string) []*data.FilterCond {
var conds []*data.FilterCond
for name, val := range paramMap {
//log.Debugf("testing request param %v", name)
if api.IsParameterReservedName(name) {
continue
}
if _, ok := colNameMap[name]; ok {
cond := &data.FilterCond{Name: name, Value: val}
conds = append(conds, cond)
//log.Debugf("Adding filter %v = %v ", name, val)
}
}
return conds
}
func createQueryParams(requestParam *api.RequestParam, colNames []string) *data.QueryParam {
param := data.QueryParam{
Limit: requestParam.Limit,
Offset: requestParam.Offset,
Bbox: requestParam.Bbox,
OrderBy: requestParam.OrderBy,
Precision: requestParam.Precision,
TransformFuns: requestParam.TransformFuns,
}
param.Columns = normalizePropNames(requestParam.Properties, colNames)
return ¶m
}