mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:16:28 +01:00
fix https://github.com/kataras/iris/issues/760 and deeper
Former-commit-id: 65e8848933386f1d3253b560bbd160079bf4d876
This commit is contained in:
parent
9ff124d39f
commit
ff8f96d56d
|
@ -364,6 +364,8 @@ type Context interface {
|
||||||
// | Various Request and Post Data |
|
// | Various Request and Post Data |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
|
||||||
|
// URLParamDefault returns the get parameter from a request, if not found then "def" is returned.
|
||||||
|
URLParamDefault(name string, def string) string
|
||||||
// URLParam returns the get parameter from a request , if any.
|
// URLParam returns the get parameter from a request , if any.
|
||||||
URLParam(name string) string
|
URLParam(name string) string
|
||||||
// URLParamTrim returns the url query parameter with trailing white spaces removed from a request,
|
// URLParamTrim returns the url query parameter with trailing white spaces removed from a request,
|
||||||
|
@ -372,13 +374,25 @@ type Context interface {
|
||||||
// URLParamTrim returns the escaped url query parameter from a request,
|
// URLParamTrim returns the escaped url query parameter from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
URLParamEscape(name string) string
|
URLParamEscape(name string) string
|
||||||
|
// URLParamIntDefault returns the url query parameter as int value from a request,
|
||||||
|
// if not found then "def" is returned.
|
||||||
|
// Returns an error if parse failed.
|
||||||
|
URLParamIntDefault(name string, def int) (int, error)
|
||||||
// URLParamInt returns the url query parameter as int value from a request,
|
// URLParamInt returns the url query parameter as int value from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
URLParamInt(name string) (int, error)
|
URLParamInt(name string) (int, error)
|
||||||
|
// URLParamInt64Default returns the url query parameter as int64 value from a request,
|
||||||
|
// if not found then "def" is returned.
|
||||||
|
// Returns an error if parse failed.
|
||||||
|
URLParamInt64Default(name string, def int64) (int64, error)
|
||||||
// URLParamInt64 returns the url query parameter as int64 value from a request,
|
// URLParamInt64 returns the url query parameter as int64 value from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
URLParamInt64(name string) (int64, error)
|
URLParamInt64(name string) (int64, error)
|
||||||
// URLParamInt64 returns the url query parameter as float64 value from a request,
|
// URLParamFloat64Default returns the url query parameter as float64 value from a request,
|
||||||
|
// if not found then "def" is returned.
|
||||||
|
// Returns an error if parse failed.
|
||||||
|
URLParamFloat64Default(name string, def float64) (float64, error)
|
||||||
|
// URLParamFloat64 returns the url query parameter as float64 value from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
URLParamFloat64(name string) (float64, error)
|
URLParamFloat64(name string) (float64, error)
|
||||||
// URLParamBool returns the url query parameter as boolean value from a request,
|
// URLParamBool returns the url query parameter as boolean value from a request,
|
||||||
|
@ -396,6 +410,10 @@ type Context interface {
|
||||||
// NOTE: A check for nil is necessary.
|
// NOTE: A check for nil is necessary.
|
||||||
FormValues() map[string][]string
|
FormValues() map[string][]string
|
||||||
|
|
||||||
|
// PostValueDefault returns a form's only-post value by its name,
|
||||||
|
// if not found then "def" is returned,
|
||||||
|
// same as Request.PostFormValue.
|
||||||
|
PostValueDefault(name string, def string) string
|
||||||
// PostValue returns a form's only-post value by its name,
|
// PostValue returns a form's only-post value by its name,
|
||||||
// same as Request.PostFormValue.
|
// same as Request.PostFormValue.
|
||||||
PostValue(name string) string
|
PostValue(name string) string
|
||||||
|
@ -403,10 +421,19 @@ type Context interface {
|
||||||
PostValueTrim(name string) string
|
PostValueTrim(name string) string
|
||||||
// PostValueEscape returns a form's only-post escaped value by its name.
|
// PostValueEscape returns a form's only-post escaped value by its name.
|
||||||
PostValueEscape(name string) string
|
PostValueEscape(name string) string
|
||||||
|
// PostValueIntDefault returns a form's only-post value as int by its name.
|
||||||
|
// If not found returns "def".
|
||||||
|
PostValueIntDefault(name string, def int) (int, error)
|
||||||
// PostValueInt returns a form's only-post value as int by its name.
|
// PostValueInt returns a form's only-post value as int by its name.
|
||||||
PostValueInt(name string) (int, error)
|
PostValueInt(name string) (int, error)
|
||||||
|
// PostValueInt64Default returns a form's only-post value as int64 by its name.
|
||||||
|
// If not found returns "def".
|
||||||
|
PostValueInt64Default(name string, def int64) (int64, error)
|
||||||
// PostValueInt64 returns a form's only-post value as int64 by its name.
|
// PostValueInt64 returns a form's only-post value as int64 by its name.
|
||||||
PostValueInt64(name string) (int64, error)
|
PostValueInt64(name string) (int64, error)
|
||||||
|
// PostValueFloat64Default returns a form's only-post value as float64 by its name.
|
||||||
|
// If not found returns "def".
|
||||||
|
PostValueFloat64Default(name string, def float64) (float64, error)
|
||||||
// PostValueFloat64 returns a form's only-post value as float64 by its name.
|
// PostValueFloat64 returns a form's only-post value as float64 by its name.
|
||||||
PostValueFloat64(name string) (float64, error)
|
PostValueFloat64(name string) (float64, error)
|
||||||
// PostValue returns a form's only-post value as boolean by its name.
|
// PostValue returns a form's only-post value as boolean by its name.
|
||||||
|
@ -1339,9 +1366,18 @@ func (ctx *context) GetStatusCode() int {
|
||||||
// | Various Request and Post Data |
|
// | Various Request and Post Data |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
|
||||||
|
// URLParamDefault returns the get parameter from a request, if not found then "def" is returned.
|
||||||
|
func (ctx *context) URLParamDefault(name string, def string) string {
|
||||||
|
v := ctx.request.URL.Query().Get(name)
|
||||||
|
if v == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
// URLParam returns the get parameter from a request , if any.
|
// URLParam returns the get parameter from a request , if any.
|
||||||
func (ctx *context) URLParam(name string) string {
|
func (ctx *context) URLParam(name string) string {
|
||||||
return ctx.request.URL.Query().Get(name)
|
return ctx.URLParamDefault(name, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLParamTrim returns the url query parameter with trailing white spaces removed from a request,
|
// URLParamTrim returns the url query parameter with trailing white spaces removed from a request,
|
||||||
|
@ -1356,22 +1392,55 @@ func (ctx *context) URLParamEscape(name string) string {
|
||||||
return DecodeQuery(ctx.URLParam(name))
|
return DecodeQuery(ctx.URLParam(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URLParamIntDefault returns the url query parameter as int value from a request,
|
||||||
|
// if not found then "def" is returned.
|
||||||
|
// Returns an error if parse failed.
|
||||||
|
func (ctx *context) URLParamIntDefault(name string, def int) (int, error) {
|
||||||
|
v := ctx.URLParam(name)
|
||||||
|
if v == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
// URLParamInt returns the url query parameter as int value from a request,
|
// URLParamInt returns the url query parameter as int value from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
func (ctx *context) URLParamInt(name string) (int, error) {
|
func (ctx *context) URLParamInt(name string) (int, error) {
|
||||||
return strconv.Atoi(ctx.URLParam(name))
|
return ctx.URLParamIntDefault(name, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLParamInt64Default returns the url query parameter as int64 value from a request,
|
||||||
|
// if not found then "def" is returned.
|
||||||
|
// Returns an error if parse failed.
|
||||||
|
func (ctx *context) URLParamInt64Default(name string, def int64) (int64, error) {
|
||||||
|
v := ctx.URLParam(name)
|
||||||
|
if v == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(v, 10, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLParamInt64 returns the url query parameter as int64 value from a request,
|
// URLParamInt64 returns the url query parameter as int64 value from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
func (ctx *context) URLParamInt64(name string) (int64, error) {
|
func (ctx *context) URLParamInt64(name string) (int64, error) {
|
||||||
return strconv.ParseInt(ctx.URLParam(name), 10, 64)
|
return ctx.URLParamInt64Default(name, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLParamInt64 returns the url query parameter as float64 value from a request,
|
// URLParamFloat64Default returns the url query parameter as float64 value from a request,
|
||||||
|
// if not found then "def" is returned.
|
||||||
|
// Returns an error if parse failed.
|
||||||
|
func (ctx *context) URLParamFloat64Default(name string, def float64) (float64, error) {
|
||||||
|
v := ctx.URLParam(name)
|
||||||
|
if v == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.ParseFloat(v, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLParamFloat64 returns the url query parameter as float64 value from a request,
|
||||||
// returns an error if parse failed.
|
// returns an error if parse failed.
|
||||||
func (ctx *context) URLParamFloat64(name string) (float64, error) {
|
func (ctx *context) URLParamFloat64(name string) (float64, error) {
|
||||||
return strconv.ParseFloat(ctx.URLParam(name), 64)
|
return ctx.URLParamFloat64Default(name, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLParamBool returns the url query parameter as boolean value from a request,
|
// URLParamBool returns the url query parameter as boolean value from a request,
|
||||||
|
@ -1422,10 +1491,21 @@ func (ctx *context) FormValues() map[string][]string {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostValueDefault returns a form's only-post value by its name,
|
||||||
|
// if not found then "def" is returned,
|
||||||
|
// same as Request.PostFormValue.
|
||||||
|
func (ctx *context) PostValueDefault(name string, def string) string {
|
||||||
|
v := ctx.request.PostFormValue(name)
|
||||||
|
if v == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
// PostValue returns a form's only-post value by its name,
|
// PostValue returns a form's only-post value by its name,
|
||||||
// same as Request.PostFormValue.
|
// same as Request.PostFormValue.
|
||||||
func (ctx *context) PostValue(name string) string {
|
func (ctx *context) PostValue(name string) string {
|
||||||
return ctx.request.PostFormValue(name)
|
return ctx.PostValueDefault(name, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostValueTrim returns a form's only-post value without trailing spaces by its name.
|
// PostValueTrim returns a form's only-post value without trailing spaces by its name.
|
||||||
|
@ -1438,19 +1518,52 @@ func (ctx *context) PostValueEscape(name string) string {
|
||||||
return DecodeQuery(ctx.PostValue(name))
|
return DecodeQuery(ctx.PostValue(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostValueIntDefault returns a form's only-post value as int by its name.
|
||||||
|
// If not found returns "def".
|
||||||
|
func (ctx *context) PostValueIntDefault(name string, def int) (int, error) {
|
||||||
|
v := ctx.PostValue(name)
|
||||||
|
if v == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
// PostValueInt returns a form's only-post value as int by its name.
|
// PostValueInt returns a form's only-post value as int by its name.
|
||||||
|
// If not found returns 0.
|
||||||
func (ctx *context) PostValueInt(name string) (int, error) {
|
func (ctx *context) PostValueInt(name string) (int, error) {
|
||||||
return strconv.Atoi(ctx.PostValue(name))
|
return ctx.PostValueIntDefault(name, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostValueInt64Default returns a form's only-post value as int64 by its name.
|
||||||
|
// If not found returns "def".
|
||||||
|
func (ctx *context) PostValueInt64Default(name string, def int64) (int64, error) {
|
||||||
|
v := ctx.PostValue(name)
|
||||||
|
if v == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(v, 10, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostValueInt64 returns a form's only-post value as int64 by its name.
|
// PostValueInt64 returns a form's only-post value as int64 by its name.
|
||||||
|
// If not found returns 0.0.
|
||||||
func (ctx *context) PostValueInt64(name string) (int64, error) {
|
func (ctx *context) PostValueInt64(name string) (int64, error) {
|
||||||
return strconv.ParseInt(ctx.PostValue(name), 10, 64)
|
return ctx.PostValueInt64Default(name, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostValueFloat64Default returns a form's only-post value as float64 by its name.
|
||||||
|
// If not found returns "def".
|
||||||
|
func (ctx *context) PostValueFloat64Default(name string, def float64) (float64, error) {
|
||||||
|
v := ctx.PostValue(name)
|
||||||
|
if v == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.ParseFloat(v, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostValueFloat64 returns a form's only-post value as float64 by its name.
|
// PostValueFloat64 returns a form's only-post value as float64 by its name.
|
||||||
|
// If not found returns 0.0.
|
||||||
func (ctx *context) PostValueFloat64(name string) (float64, error) {
|
func (ctx *context) PostValueFloat64(name string) (float64, error) {
|
||||||
return strconv.ParseFloat(ctx.PostValue(name), 64)
|
return ctx.PostValueFloat64Default(name, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostValue returns a form's only-post value as boolean by its name.
|
// PostValue returns a form's only-post value as boolean by its name.
|
||||||
|
|
|
@ -8,6 +8,7 @@ package memstore
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/kataras/iris/core/errors"
|
"github.com/kataras/iris/core/errors"
|
||||||
)
|
)
|
||||||
|
@ -127,8 +128,9 @@ func (r *Store) SetImmutable(key string, value interface{}) (Entry, bool) {
|
||||||
return r.Save(key, value, true)
|
return r.Save(key, value, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the entry's value based on its key.
|
// GetDefault returns the entry's value based on its key.
|
||||||
func (r *Store) Get(key string) interface{} {
|
// If not found returns "def".
|
||||||
|
func (r *Store) GetDefault(key string, def interface{}) interface{} {
|
||||||
args := *r
|
args := *r
|
||||||
n := len(args)
|
n := len(args)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
|
@ -138,7 +140,13 @@ func (r *Store) Get(key string) interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the entry's value based on its key.
|
||||||
|
// If not found returns nil.
|
||||||
|
func (r *Store) Get(key string) interface{} {
|
||||||
|
return r.GetDefault(key, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visit accepts a visitor which will be filled
|
// Visit accepts a visitor which will be filled
|
||||||
|
@ -151,34 +159,94 @@ func (r *Store) Visit(visitor func(key string, value interface{})) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetString returns the entry's value as string, based on its key.
|
// GetStringDefault returns the entry's value as string, based on its key.
|
||||||
func (r *Store) GetString(key string) string {
|
// If not found returns "def".
|
||||||
|
func (r *Store) GetStringDefault(key string, def string) string {
|
||||||
if v, ok := r.Get(key).(string); ok {
|
if v, ok := r.Get(key).(string); ok {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetString returns the entry's value as string, based on its key.
|
||||||
|
func (r *Store) GetString(key string) string {
|
||||||
|
return r.GetStringDefault(key, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringTrim returns the entry's string value without trailing spaces.
|
||||||
|
func (r *Store) GetStringTrim(name string) string {
|
||||||
|
return strings.TrimSpace(r.GetString(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrIntParse returns an error message when int parse failed
|
// ErrIntParse returns an error message when int parse failed
|
||||||
// it's not statical error, it depends on the failed value.
|
// it's not statical error, it depends on the failed value.
|
||||||
var ErrIntParse = errors.New("unable to find or parse the integer, found: %#v")
|
var ErrIntParse = errors.New("unable to find or parse the integer, found: %#v")
|
||||||
|
|
||||||
// GetInt returns the entry's value as int, based on its key.
|
// GetIntDefault returns the entry's value as int, based on its key.
|
||||||
func (r *Store) GetInt(key string) (int, error) {
|
// If not found returns "def".
|
||||||
|
func (r *Store) GetIntDefault(key string, def int) (int, error) {
|
||||||
v := r.Get(key)
|
v := r.Get(key)
|
||||||
if vint, ok := v.(int); ok {
|
if vint, ok := v.(int); ok {
|
||||||
return vint, nil
|
return vint, nil
|
||||||
} else if vstring, sok := v.(string); sok {
|
} else if vstring, sok := v.(string); sok {
|
||||||
|
if vstring == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
return strconv.Atoi(vstring)
|
return strconv.Atoi(vstring)
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1, ErrIntParse.Format(v)
|
return def, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt returns the entry's value as int, based on its key.
|
||||||
|
// If not found returns 0.
|
||||||
|
func (r *Store) GetInt(key string) (int, error) {
|
||||||
|
return r.GetIntDefault(key, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt64Default returns the entry's value as int64, based on its key.
|
||||||
|
// If not found returns "def".
|
||||||
|
func (r *Store) GetInt64Default(key string, def int64) (int64, error) {
|
||||||
|
v := r.Get(key)
|
||||||
|
if vint64, ok := v.(int64); ok {
|
||||||
|
return vint64, nil
|
||||||
|
} else if vstring, sok := v.(string); sok {
|
||||||
|
if vstring == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(vstring, 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
return def, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInt64 returns the entry's value as int64, based on its key.
|
// GetInt64 returns the entry's value as int64, based on its key.
|
||||||
|
// If not found returns 0.0.
|
||||||
func (r *Store) GetInt64(key string) (int64, error) {
|
func (r *Store) GetInt64(key string) (int64, error) {
|
||||||
return strconv.ParseInt(r.GetString(key), 10, 64)
|
return r.GetInt64Default(key, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat64Default returns the entry's value as float64, based on its key.
|
||||||
|
// If not found returns "def".
|
||||||
|
func (r *Store) GetFloat64Default(key string, def float64) (float64, error) {
|
||||||
|
v := r.Get(key)
|
||||||
|
if vfloat64, ok := v.(float64); ok {
|
||||||
|
return vfloat64, nil
|
||||||
|
} else if vstring, sok := v.(string); sok {
|
||||||
|
if vstring == "" {
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
return strconv.ParseFloat(vstring, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
return def, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat64 returns the entry's value as float64, based on its key.
|
||||||
|
// If not found returns 0.0.
|
||||||
|
func (r *Store) GetFloat64(key string) (float64, error) {
|
||||||
|
return r.GetFloat64Default(key, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBool returns the user's value as bool, based on its key.
|
// GetBool returns the user's value as bool, based on its key.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user