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
| package cutil
import ( "strconv" "strings"
"github.com/valyala/fasthttp"
"{{{ .Package }}}/app/lib/filter" )
func ParamSetFromRequest(rc *fasthttp.RequestCtx) filter.ParamSet { ret := filter.ParamSet{} args := rc.URI().QueryArgs() args.VisitAll(func(key []byte, value []byte) { qk := string(key) if strings.Contains(qk, ".") { ret = apply(ret, qk, string(args.Peek(qk))) } }) return ret }
func apply(ps filter.ParamSet, qk string, qv string) filter.ParamSet { switch { case strings.HasSuffix(qk, ".o"): curr := getCurr(ps, strings.TrimSuffix(qk, ".o")) asc := true if strings.HasSuffix(qv, ".d") { asc = false qv = qv[0 : len(qv)-2] } curr.Orderings = append(curr.Orderings, &filter.Ordering{Column: qv, Asc: asc}) case strings.HasSuffix(qk, ".l"): curr := getCurr(ps, strings.TrimSuffix(qk, ".l")) li, err := strconv.ParseInt(qv, 10, 32) if err == nil { curr.Limit = int(li) maxCount := 100000 if curr.Limit > maxCount { curr.Limit = maxCount } } case strings.HasSuffix(qk, ".x"): curr := getCurr(ps, strings.TrimSuffix(qk, ".x")) xi, err := strconv.ParseInt(qv, 10, 32) if err == nil { curr.Offset = int(xi) } } return ps }
func getCurr(q filter.ParamSet, key string) *filter.Params { curr, ok := q[key] if !ok { curr = &filter.Params{Key: key} q[key] = curr } return curr }
|