mirror of
https://github.com/rancher/norman.git
synced 2025-09-04 16:50:41 +00:00
Have URLParser parse the query too
This commit is contained in:
@@ -21,34 +21,34 @@ func QueryOptions(apiContext *types.APIContext, schema *types.Schema) types.Quer
|
||||
|
||||
result := &types.QueryOptions{}
|
||||
|
||||
result.Sort = parseSort(schema, req)
|
||||
result.Pagination = parsePagination(req)
|
||||
result.Conditions = parseFilters(schema, req)
|
||||
result.Sort = parseSort(schema, apiContext)
|
||||
result.Pagination = parsePagination(apiContext)
|
||||
result.Conditions = parseFilters(schema, apiContext)
|
||||
|
||||
return *result
|
||||
}
|
||||
|
||||
func parseOrder(req *http.Request) types.SortOrder {
|
||||
order := req.URL.Query().Get("order")
|
||||
func parseOrder(apiContext *types.APIContext) types.SortOrder {
|
||||
order := apiContext.Query.Get("order")
|
||||
if types.SortOrder(order) == types.DESC {
|
||||
return types.DESC
|
||||
}
|
||||
return types.ASC
|
||||
}
|
||||
|
||||
func parseSort(schema *types.Schema, req *http.Request) types.Sort {
|
||||
sortField := req.URL.Query().Get("sort")
|
||||
func parseSort(schema *types.Schema, apiContext *types.APIContext) types.Sort {
|
||||
sortField := apiContext.Query.Get("sort")
|
||||
if _, ok := schema.CollectionFilters[sortField]; !ok {
|
||||
sortField = ""
|
||||
}
|
||||
return types.Sort{
|
||||
Order: parseOrder(req),
|
||||
Order: parseOrder(apiContext),
|
||||
Name: sortField,
|
||||
}
|
||||
}
|
||||
|
||||
func parsePagination(req *http.Request) *types.Pagination {
|
||||
q := req.URL.Query()
|
||||
func parsePagination(apiContext *types.APIContext) *types.Pagination {
|
||||
q := apiContext.Query
|
||||
limit := q.Get("limit")
|
||||
marker := q.Get("marker")
|
||||
|
||||
@@ -86,9 +86,9 @@ func parseNameAndOp(value string) (string, types.ModifierType) {
|
||||
return name, types.ModifierType(op)
|
||||
}
|
||||
|
||||
func parseFilters(schema *types.Schema, req *http.Request) []*types.QueryCondition {
|
||||
func parseFilters(schema *types.Schema, apiContext *types.APIContext) []*types.QueryCondition {
|
||||
var conditions []*types.QueryCondition
|
||||
for key, values := range req.URL.Query() {
|
||||
for key, values := range apiContext.Query {
|
||||
name, op := parseNameAndOp(key)
|
||||
filter, ok := schema.CollectionFilters[name]
|
||||
if !ok {
|
||||
|
@@ -32,6 +32,7 @@ type ParsedURL struct {
|
||||
Action string
|
||||
SubContext map[string]string
|
||||
SubContextPrefix string
|
||||
Query url.Values
|
||||
}
|
||||
|
||||
type ResolverFunc func(typeName string, context *types.APIContext) error
|
||||
@@ -56,6 +57,7 @@ func DefaultURLParser(schemas *types.Schemas, url *url.URL) (ParsedURL, error) {
|
||||
result.SubContext = subContext
|
||||
result.SubContextPrefix = prefix
|
||||
result.Action, result.Method = parseAction(url)
|
||||
result.Query = url.Query()
|
||||
|
||||
result.Type = safeIndex(parts, 1)
|
||||
result.ID = safeIndex(parts, 2)
|
||||
@@ -86,6 +88,7 @@ func Parse(rw http.ResponseWriter, req *http.Request, schemas *types.Schemas, ur
|
||||
result.ID = parsedURL.ID
|
||||
result.Link = parsedURL.Link
|
||||
result.Action = parsedURL.Action
|
||||
result.Query = parsedURL.Query
|
||||
if parsedURL.Method != "" {
|
||||
result.Method = parsedURL.Method
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package types
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type ValuesMap struct {
|
||||
@@ -79,6 +80,7 @@ type APIContext struct {
|
||||
Schema *Schema
|
||||
Schemas *Schemas
|
||||
Version *APIVersion
|
||||
Query url.Values
|
||||
ResponseFormat string
|
||||
ReferenceValidator ReferenceValidator
|
||||
ResponseWriter ResponseWriter
|
||||
|
Reference in New Issue
Block a user