1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-22 21:47:32 +00:00
norman/parse/collection.go

113 lines
2.3 KiB
Go
Raw Permalink Normal View History

2017-11-11 04:44:02 +00:00
package parse
import (
"net/http"
"strconv"
"strings"
"github.com/rancher/norman/types"
)
var (
2017-11-21 20:46:30 +00:00
defaultLimit = int64(1000)
maxLimit = int64(10000)
2017-11-11 04:44:02 +00:00
)
2017-11-21 20:46:30 +00:00
func QueryOptions(apiContext *types.APIContext, schema *types.Schema) types.QueryOptions {
req := apiContext.Request
2017-11-11 04:44:02 +00:00
if req.Method != http.MethodGet {
2017-11-21 20:46:30 +00:00
return types.QueryOptions{}
2017-11-11 04:44:02 +00:00
}
result := &types.QueryOptions{}
2017-12-23 06:13:13 +00:00
result.Sort = parseSort(schema, apiContext)
result.Pagination = parsePagination(apiContext)
result.Conditions = parseFilters(schema, apiContext)
2017-11-11 04:44:02 +00:00
2017-11-21 20:46:30 +00:00
return *result
2017-11-11 04:44:02 +00:00
}
2017-12-23 06:13:13 +00:00
func parseOrder(apiContext *types.APIContext) types.SortOrder {
order := apiContext.Query.Get("order")
2017-11-11 04:44:02 +00:00
if types.SortOrder(order) == types.DESC {
return types.DESC
}
return types.ASC
}
2017-12-23 06:13:13 +00:00
func parseSort(schema *types.Schema, apiContext *types.APIContext) types.Sort {
sortField := apiContext.Query.Get("sort")
2017-11-11 04:44:02 +00:00
if _, ok := schema.CollectionFilters[sortField]; !ok {
sortField = ""
}
return types.Sort{
2017-12-23 06:13:13 +00:00
Order: parseOrder(apiContext),
2017-11-11 04:44:02 +00:00
Name: sortField,
}
}
2017-12-23 06:13:13 +00:00
func parsePagination(apiContext *types.APIContext) *types.Pagination {
if apiContext.Pagination != nil {
return apiContext.Pagination
}
2017-12-23 06:13:13 +00:00
q := apiContext.Query
2017-11-11 04:44:02 +00:00
limit := q.Get("limit")
marker := q.Get("marker")
result := &types.Pagination{
Limit: &defaultLimit,
Marker: marker,
}
if limit != "" {
limitInt, err := strconv.ParseInt(limit, 10, 64)
if err != nil {
return result
}
if limitInt > maxLimit || limitInt == -1 {
2017-11-11 04:44:02 +00:00
result.Limit = &maxLimit
2017-11-21 20:46:30 +00:00
} else if limitInt >= 0 {
2017-11-11 04:44:02 +00:00
result.Limit = &limitInt
}
}
return result
}
2017-11-21 20:46:30 +00:00
func parseNameAndOp(value string) (string, types.ModifierType) {
2017-11-11 04:44:02 +00:00
name := value
op := "eq"
idx := strings.LastIndex(value, "_")
if idx > 0 {
op = value[idx+1:]
name = value[0:idx]
}
2017-11-21 20:46:30 +00:00
return name, types.ModifierType(op)
2017-11-11 04:44:02 +00:00
}
2017-12-23 06:13:13 +00:00
func parseFilters(schema *types.Schema, apiContext *types.APIContext) []*types.QueryCondition {
2017-11-21 20:46:30 +00:00
var conditions []*types.QueryCondition
2017-12-23 06:13:13 +00:00
for key, values := range apiContext.Query {
2017-11-11 04:44:02 +00:00
name, op := parseNameAndOp(key)
filter, ok := schema.CollectionFilters[name]
if !ok {
continue
}
for _, mod := range filter.Modifiers {
if op != mod || !types.ValidMod(op) {
continue
}
2017-11-21 20:46:30 +00:00
conditions = append(conditions, types.NewConditionFromString(name, mod, values...))
2017-11-11 04:44:02 +00:00
}
}
return conditions
}