1
0
mirror of https://github.com/kataras/iris.git synced 2026-03-06 08:25:59 +00:00

add example for #1601

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-22 03:21:44 +03:00
parent e41e861c4c
commit a018ba9b0a
12 changed files with 247 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"sync/atomic"
@@ -1504,8 +1505,12 @@ func (ctx *Context) URLParamBool(name string) (bool, error) {
return strconv.ParseBool(ctx.URLParam(name))
}
// URLParams returns a map of GET query parameters separated by comma if more than one
// it returns an empty map if nothing found.
// URLParams returns a map of URL Query parameters.
// If the value of a URL parameter is a slice,
// then it is joined as one separated by comma.
// It returns an empty map on empty URL query.
//
// See URLParamsSorted too.
func (ctx *Context) URLParams() map[string]string {
q := ctx.request.URL.Query()
values := make(map[string]string, len(q))
@@ -1517,6 +1522,34 @@ func (ctx *Context) URLParams() map[string]string {
return values
}
// URLParamsSorted returns a sorted (by key) slice
// of key-value entries of the URL Query parameters.
func (ctx *Context) URLParamsSorted() []memstore.StringEntry {
q := ctx.request.URL.Query()
n := len(q)
if n == 0 {
return nil
}
keys := make([]string, 0, n)
for key := range q {
keys = append(keys, key)
}
sort.Strings(keys)
entries := make([]memstore.StringEntry, 0, n)
for _, key := range keys {
value := q[key]
entries = append(entries, memstore.StringEntry{
Key: key,
Value: strings.Join(value, ","),
})
}
return entries
}
// No need anymore, net/http checks for the Form already.
// func (ctx *Context) askParseForm() error {
// if ctx.request.Form == nil {

View File

@@ -16,6 +16,18 @@ type RequestParams struct {
memstore.Store
}
// RequestParamsReadOnly is the read-only access type of RequestParams.
type RequestParamsReadOnly interface {
Get(key string) string
GetEntryAt(index int) memstore.Entry
Visit(visitor func(key string, value string))
GetTrim(key string) string
GetEscape(key string) string
GetDecoded(key string) string
} // Note: currently unused.
var _ RequestParamsReadOnly = (*RequestParams)(nil)
// Set inserts a parameter value.
// See `Get` too.
func (r *RequestParams) Set(key, value string) {