1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 20:41:57 +00:00
Former-commit-id: fe6305deed00e170bf4d39a12c0644fe686e0a24
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-10-21 19:20:05 +03:00
parent dbba631df4
commit 3962710d3d
109 changed files with 4383 additions and 2658 deletions

View File

@@ -6,6 +6,8 @@
package memstore
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
@@ -14,6 +16,11 @@ import (
)
type (
// ValueSetter is the interface which can be accepted as a generic solution of RequestParams or memstore when Set is the only requirement,
// i.e internally on macro/template/TemplateParam#Eval:paramChanger.
ValueSetter interface {
Set(key string, newValue interface{}) (Entry, bool)
}
// Entry is the entry of the context storage Store - .Values()
Entry struct {
Key string
@@ -25,6 +32,8 @@ type (
Store []Entry
)
var _ ValueSetter = (*Store)(nil)
// GetByKindOrNil will try to get this entry's value of "k" kind,
// if value is not that kind it will NOT try to convert it the "k", instead
// it will return nil, except if boolean; then it will return false
@@ -67,11 +76,19 @@ func (e Entry) GetByKindOrNil(k reflect.Kind) interface{} {
// If not found returns "def".
func (e Entry) StringDefault(def string) string {
v := e.ValueRaw
if v == nil {
return def
}
if vString, ok := v.(string); ok {
return vString
}
val := fmt.Sprintf("%v", v)
if val != "" {
return val
}
return def
}
@@ -94,20 +111,129 @@ func (e Entry) IntDefault(def int) (int, error) {
if v == nil {
return def, errFindParse.Format("int", e.Key)
}
if vint, ok := v.(int); ok {
return vint, nil
} else if vstring, sok := v.(string); sok && vstring != "" {
vint, err := strconv.Atoi(vstring)
switch vv := v.(type) {
case string:
val, err := strconv.Atoi(vv)
if err != nil {
return def, err
}
return vint, nil
return val, nil
case int:
return vv, nil
case int8:
return int(vv), nil
case int16:
return int(vv), nil
case int32:
return int(vv), nil
case int64:
return int(vv), nil
case uint:
return int(vv), nil
case uint8:
return int(vv), nil
case uint16:
return int(vv), nil
case uint32:
return int(vv), nil
case uint64:
return int(vv), nil
}
return def, errFindParse.Format("int", e.Key)
}
// Int8Default returns the entry's value as int8.
// If not found returns "def" and a non-nil error.
func (e Entry) Int8Default(def int8) (int8, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("int8", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseInt(vv, 10, 8)
if err != nil {
return def, err
}
return int8(val), nil
case int:
return int8(vv), nil
case int8:
return vv, nil
case int16:
return int8(vv), nil
case int32:
return int8(vv), nil
case int64:
return int8(vv), nil
}
return def, errFindParse.Format("int8", e.Key)
}
// Int16Default returns the entry's value as int16.
// If not found returns "def" and a non-nil error.
func (e Entry) Int16Default(def int16) (int16, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("int16", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseInt(vv, 10, 16)
if err != nil {
return def, err
}
return int16(val), nil
case int:
return int16(vv), nil
case int8:
return int16(vv), nil
case int16:
return vv, nil
case int32:
return int16(vv), nil
case int64:
return int16(vv), nil
}
return def, errFindParse.Format("int16", e.Key)
}
// Int32Default returns the entry's value as int32.
// If not found returns "def" and a non-nil error.
func (e Entry) Int32Default(def int32) (int32, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("int32", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseInt(vv, 10, 32)
if err != nil {
return def, err
}
return int32(val), nil
case int:
return int32(vv), nil
case int8:
return int32(vv), nil
case int16:
return int32(vv), nil
case int32:
return vv, nil
case int64:
return int32(vv), nil
}
return def, errFindParse.Format("int32", e.Key)
}
// Int64Default returns the entry's value as int64.
// If not found returns "def" and a non-nil error.
func (e Entry) Int64Default(def int64) (int64, error) {
@@ -116,85 +242,208 @@ func (e Entry) Int64Default(def int64) (int64, error) {
return def, errFindParse.Format("int64", e.Key)
}
if vint64, ok := v.(int64); ok {
return vint64, nil
}
if vint, ok := v.(int); ok {
return int64(vint), nil
}
if vstring, sok := v.(string); sok {
return strconv.ParseInt(vstring, 10, 64)
switch vv := v.(type) {
case string:
return strconv.ParseInt(vv, 10, 64)
case int64:
return vv, nil
case int32:
return int64(vv), nil
case int8:
return int64(vv), nil
case int:
return int64(vv), nil
}
return def, errFindParse.Format("int64", e.Key)
}
// Float64Default returns the entry's value as float64.
// UintDefault returns the entry's value as uint.
// If not found returns "def" and a non-nil error.
func (e Entry) Float64Default(def float64) (float64, error) {
func (e Entry) UintDefault(def uint) (uint, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("float64", e.Key)
return def, errFindParse.Format("uint", e.Key)
}
if vfloat32, ok := v.(float32); ok {
return float64(vfloat32), nil
x64 := strconv.IntSize == 64
var maxValue uint = math.MaxUint32
if x64 {
maxValue = math.MaxUint64
}
if vfloat64, ok := v.(float64); ok {
return vfloat64, nil
}
if vint, ok := v.(int); ok {
return float64(vint), nil
}
if vstring, sok := v.(string); sok {
vfloat64, err := strconv.ParseFloat(vstring, 64)
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, strconv.IntSize)
if err != nil {
return def, err
}
return vfloat64, nil
if val > uint64(maxValue) {
return def, errFindParse.Format("uint", e.Key)
}
return uint(val), nil
case uint:
return vv, nil
case uint8:
return uint(vv), nil
case uint16:
return uint(vv), nil
case uint32:
return uint(vv), nil
case uint64:
if vv > uint64(maxValue) {
return def, errFindParse.Format("uint", e.Key)
}
return uint(vv), nil
case int:
if vv < 0 || vv > int(maxValue) {
return def, errFindParse.Format("uint", e.Key)
}
return uint(vv), nil
}
return def, errFindParse.Format("float64", e.Key)
return def, errFindParse.Format("uint", e.Key)
}
// Float32Default returns the entry's value as float32.
// Uint8Default returns the entry's value as uint8.
// If not found returns "def" and a non-nil error.
func (e Entry) Float32Default(key string, def float32) (float32, error) {
func (e Entry) Uint8Default(def uint8) (uint8, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("float32", e.Key)
return def, errFindParse.Format("uint8", e.Key)
}
if vfloat32, ok := v.(float32); ok {
return vfloat32, nil
}
if vfloat64, ok := v.(float64); ok {
return float32(vfloat64), nil
}
if vint, ok := v.(int); ok {
return float32(vint), nil
}
if vstring, sok := v.(string); sok {
vfloat32, err := strconv.ParseFloat(vstring, 32)
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 8)
if err != nil {
return def, err
}
return float32(vfloat32), nil
if val > math.MaxUint8 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(val), nil
case uint:
if vv > math.MaxUint8 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(vv), nil
case uint8:
return vv, nil
case uint16:
if vv > math.MaxUint8 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(vv), nil
case uint32:
if vv > math.MaxUint8 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(vv), nil
case uint64:
if vv > math.MaxUint8 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(vv), nil
case int:
if vv < 0 || vv > math.MaxUint8 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(vv), nil
}
return def, errFindParse.Format("float32", e.Key)
return def, errFindParse.Format("uint8", e.Key)
}
// Uint16Default returns the entry's value as uint16.
// If not found returns "def" and a non-nil error.
func (e Entry) Uint16Default(def uint16) (uint16, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint16", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 16)
if err != nil {
return def, err
}
if val > math.MaxUint16 {
return def, errFindParse.Format("uint16", e.Key)
}
return uint16(val), nil
case uint:
if vv > math.MaxUint16 {
return def, errFindParse.Format("uint16", e.Key)
}
return uint16(vv), nil
case uint8:
return uint16(vv), nil
case uint16:
return vv, nil
case uint32:
if vv > math.MaxUint16 {
return def, errFindParse.Format("uint16", e.Key)
}
return uint16(vv), nil
case uint64:
if vv > math.MaxUint16 {
return def, errFindParse.Format("uint16", e.Key)
}
return uint16(vv), nil
case int:
if vv < 0 || vv > math.MaxUint16 {
return def, errFindParse.Format("uint16", e.Key)
}
return uint16(vv), nil
}
return def, errFindParse.Format("uint16", e.Key)
}
// Uint32Default returns the entry's value as uint32.
// If not found returns "def" and a non-nil error.
func (e Entry) Uint32Default(def uint32) (uint32, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint32", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 32)
if err != nil {
return def, err
}
if val > math.MaxUint32 {
return def, errFindParse.Format("uint32", e.Key)
}
return uint32(val), nil
case uint:
if vv > math.MaxUint32 {
return def, errFindParse.Format("uint32", e.Key)
}
return uint32(vv), nil
case uint8:
return uint32(vv), nil
case uint16:
return uint32(vv), nil
case uint32:
return vv, nil
case uint64:
if vv > math.MaxUint32 {
return def, errFindParse.Format("uint32", e.Key)
}
return uint32(vv), nil
case int:
if vv < 0 || vv > math.MaxUint32 {
return def, errFindParse.Format("uint32", e.Key)
}
return uint32(vv), nil
}
return def, errFindParse.Format("uint32", e.Key)
}
// Uint64Default returns the entry's value as uint64.
@@ -205,25 +454,102 @@ func (e Entry) Uint64Default(def uint64) (uint64, error) {
return def, errFindParse.Format("uint64", e.Key)
}
if vuint64, ok := v.(uint64); ok {
return vuint64, nil
}
if vint64, ok := v.(int64); ok {
return uint64(vint64), nil
}
if vint, ok := v.(int); ok {
return uint64(vint), nil
}
if vstring, sok := v.(string); sok {
return strconv.ParseUint(vstring, 10, 64)
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 64)
if err != nil {
return def, err
}
if val > math.MaxUint64 {
return def, errFindParse.Format("uint64", e.Key)
}
return uint64(val), nil
case uint:
if vv > math.MaxUint64 {
return def, errFindParse.Format("uint64", e.Key)
}
return uint64(vv), nil
case uint8:
return uint64(vv), nil
case uint16:
return uint64(vv), nil
case uint32:
return uint64(vv), nil
case uint64:
return vv, nil
case int64:
return uint64(vv), nil
case int:
return uint64(vv), nil
}
return def, errFindParse.Format("uint64", e.Key)
}
// Float32Default returns the entry's value as float32.
// If not found returns "def" and a non-nil error.
func (e Entry) Float32Default(key string, def float32) (float32, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("float32", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseFloat(vv, 32)
if err != nil {
return def, err
}
if val > math.MaxFloat32 {
return def, errFindParse.Format("float32", e.Key)
}
return float32(val), nil
case float32:
return vv, nil
case float64:
if vv > math.MaxFloat32 {
return def, errFindParse.Format("float32", e.Key)
}
return float32(vv), nil
case int:
return float32(vv), nil
}
return def, errFindParse.Format("float32", e.Key)
}
// Float64Default returns the entry's value as float64.
// If not found returns "def" and a non-nil error.
func (e Entry) Float64Default(def float64) (float64, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("float64", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseFloat(vv, 64)
if err != nil {
return def, err
}
return val, nil
case float32:
return float64(vv), nil
case float64:
return vv, nil
case int:
return float64(vv), nil
case int64:
return float64(vv), nil
case uint:
return float64(vv), nil
case uint64:
return float64(vv), nil
}
return def, errFindParse.Format("float64", e.Key)
}
// BoolDefault returns the user's value as bool.
// a string which is "1" or "t" or "T" or "TRUE" or "true" or "True"
// or "0" or "f" or "F" or "FALSE" or "false" or "False".
@@ -236,20 +562,17 @@ func (e Entry) BoolDefault(def bool) (bool, error) {
return def, errFindParse.Format("bool", e.Key)
}
if vBoolean, ok := v.(bool); ok {
return vBoolean, nil
}
if vString, ok := v.(string); ok {
b, err := strconv.ParseBool(vString)
switch vv := v.(type) {
case string:
val, err := strconv.ParseBool(vv)
if err != nil {
return def, err
}
return b, nil
}
if vInt, ok := v.(int); ok {
if vInt == 1 {
return val, nil
case bool:
return vv, nil
case int:
if vv == 1 {
return true, nil
}
return false, nil
@@ -361,28 +684,39 @@ func (r *Store) SetImmutable(key string, value interface{}) (Entry, bool) {
return r.Save(key, value, true)
}
var emptyEntry Entry
// GetEntry returns a pointer to the "Entry" found with the given "key"
// if nothing found then it returns nil, so be careful with that,
// it's not supposed to be used by end-developers.
func (r *Store) GetEntry(key string) *Entry {
// if nothing found then it returns an empty Entry and false.
func (r *Store) GetEntry(key string) (Entry, bool) {
args := *r
n := len(args)
for i := 0; i < n; i++ {
kv := &args[i]
if kv.Key == key {
return kv
if kv := args[i]; kv.Key == key {
return kv, true
}
}
return nil
return emptyEntry, false
}
// GetEntryAt returns the internal Entry of the memstore based on its index,
// the stored index by the router.
// If not found then it returns a zero Entry and false.
func (r *Store) GetEntryAt(index int) (Entry, bool) {
args := *r
if len(args) > index {
return args[index], true
}
return emptyEntry, false
}
// GetDefault returns the entry's value based on its key.
// If not found returns "def".
// This function checks for immutability as well, the rest don't.
func (r *Store) GetDefault(key string, def interface{}) interface{} {
v := r.GetEntry(key)
if v == nil || v.ValueRaw == nil {
v, ok := r.GetEntry(key)
if !ok || v.ValueRaw == nil {
return def
}
vv := v.Value()
@@ -411,8 +745,8 @@ func (r *Store) Visit(visitor func(key string, value interface{})) {
// GetStringDefault returns the entry's value as string, based on its key.
// If not found returns "def".
func (r *Store) GetStringDefault(key string, def string) string {
v := r.GetEntry(key)
if v == nil {
v, ok := r.GetEntry(key)
if !ok {
return def
}
@@ -432,8 +766,8 @@ func (r *Store) GetStringTrim(name string) string {
// GetInt returns the entry's value as int, based on its key.
// If not found returns -1 and a non-nil error.
func (r *Store) GetInt(key string) (int, error) {
v := r.GetEntry(key)
if v == nil {
v, ok := r.GetEntry(key)
if !ok {
return 0, errFindParse.Format("int", key)
}
return v.IntDefault(-1)
@@ -449,20 +783,60 @@ func (r *Store) GetIntDefault(key string, def int) int {
return def
}
// GetUint64 returns the entry's value as uint64, based on its key.
// If not found returns 0 and a non-nil error.
func (r *Store) GetUint64(key string) (uint64, error) {
v := r.GetEntry(key)
if v == nil {
return 0, errFindParse.Format("uint64", key)
// GetInt8 returns the entry's value as int8, based on its key.
// If not found returns -1 and a non-nil error.
func (r *Store) GetInt8(key string) (int8, error) {
v, ok := r.GetEntry(key)
if !ok {
return -1, errFindParse.Format("int8", key)
}
return v.Uint64Default(0)
return v.Int8Default(-1)
}
// GetUint64Default returns the entry's value as uint64, based on its key.
// GetInt8Default returns the entry's value as int8, based on its key.
// If not found returns "def".
func (r *Store) GetUint64Default(key string, def uint64) uint64 {
if v, err := r.GetUint64(key); err == nil {
func (r *Store) GetInt8Default(key string, def int8) int8 {
if v, err := r.GetInt8(key); err == nil {
return v
}
return def
}
// GetInt16 returns the entry's value as int16, based on its key.
// If not found returns -1 and a non-nil error.
func (r *Store) GetInt16(key string) (int16, error) {
v, ok := r.GetEntry(key)
if !ok {
return -1, errFindParse.Format("int16", key)
}
return v.Int16Default(-1)
}
// GetInt16Default returns the entry's value as int16, based on its key.
// If not found returns "def".
func (r *Store) GetInt16Default(key string, def int16) int16 {
if v, err := r.GetInt16(key); err == nil {
return v
}
return def
}
// GetInt32 returns the entry's value as int32, based on its key.
// If not found returns -1 and a non-nil error.
func (r *Store) GetInt32(key string) (int32, error) {
v, ok := r.GetEntry(key)
if !ok {
return -1, errFindParse.Format("int32", key)
}
return v.Int32Default(-1)
}
// GetInt32Default returns the entry's value as int32, based on its key.
// If not found returns "def".
func (r *Store) GetInt32Default(key string, def int32) int32 {
if v, err := r.GetInt32(key); err == nil {
return v
}
@@ -472,8 +846,8 @@ func (r *Store) GetUint64Default(key string, def uint64) uint64 {
// GetInt64 returns the entry's value as int64, based on its key.
// If not found returns -1 and a non-nil error.
func (r *Store) GetInt64(key string) (int64, error) {
v := r.GetEntry(key)
if v == nil {
v, ok := r.GetEntry(key)
if !ok {
return -1, errFindParse.Format("int64", key)
}
return v.Int64Default(-1)
@@ -489,11 +863,111 @@ func (r *Store) GetInt64Default(key string, def int64) int64 {
return def
}
// GetUint returns the entry's value as uint, based on its key.
// If not found returns 0 and a non-nil error.
func (r *Store) GetUint(key string) (uint, error) {
v, ok := r.GetEntry(key)
if !ok {
return 0, errFindParse.Format("uint", key)
}
return v.UintDefault(0)
}
// GetUintDefault returns the entry's value as uint, based on its key.
// If not found returns "def".
func (r *Store) GetUintDefault(key string, def uint) uint {
if v, err := r.GetUint(key); err == nil {
return v
}
return def
}
// GetUint8 returns the entry's value as uint8, based on its key.
// If not found returns 0 and a non-nil error.
func (r *Store) GetUint8(key string) (uint8, error) {
v, ok := r.GetEntry(key)
if !ok {
return 0, errFindParse.Format("uint8", key)
}
return v.Uint8Default(0)
}
// GetUint8Default returns the entry's value as uint8, based on its key.
// If not found returns "def".
func (r *Store) GetUint8Default(key string, def uint8) uint8 {
if v, err := r.GetUint8(key); err == nil {
return v
}
return def
}
// GetUint16 returns the entry's value as uint16, based on its key.
// If not found returns 0 and a non-nil error.
func (r *Store) GetUint16(key string) (uint16, error) {
v, ok := r.GetEntry(key)
if !ok {
return 0, errFindParse.Format("uint16", key)
}
return v.Uint16Default(0)
}
// GetUint16Default returns the entry's value as uint16, based on its key.
// If not found returns "def".
func (r *Store) GetUint16Default(key string, def uint16) uint16 {
if v, err := r.GetUint16(key); err == nil {
return v
}
return def
}
// GetUint32 returns the entry's value as uint32, based on its key.
// If not found returns 0 and a non-nil error.
func (r *Store) GetUint32(key string) (uint32, error) {
v, ok := r.GetEntry(key)
if !ok {
return 0, errFindParse.Format("uint32", key)
}
return v.Uint32Default(0)
}
// GetUint32Default returns the entry's value as uint32, based on its key.
// If not found returns "def".
func (r *Store) GetUint32Default(key string, def uint32) uint32 {
if v, err := r.GetUint32(key); err == nil {
return v
}
return def
}
// GetUint64 returns the entry's value as uint64, based on its key.
// If not found returns 0 and a non-nil error.
func (r *Store) GetUint64(key string) (uint64, error) {
v, ok := r.GetEntry(key)
if !ok {
return 0, errFindParse.Format("uint64", key)
}
return v.Uint64Default(0)
}
// GetUint64Default returns the entry's value as uint64, based on its key.
// If not found returns "def".
func (r *Store) GetUint64Default(key string, def uint64) uint64 {
if v, err := r.GetUint64(key); err == nil {
return v
}
return def
}
// GetFloat64 returns the entry's value as float64, based on its key.
// If not found returns -1 and a non nil error.
func (r *Store) GetFloat64(key string) (float64, error) {
v := r.GetEntry(key)
if v == nil {
v, ok := r.GetEntry(key)
if !ok {
return -1, errFindParse.Format("float64", key)
}
return v.Float64Default(-1)
@@ -516,8 +990,8 @@ func (r *Store) GetFloat64Default(key string, def float64) float64 {
//
// If not found returns false and a non-nil error.
func (r *Store) GetBool(key string) (bool, error) {
v := r.GetEntry(key)
if v == nil {
v, ok := r.GetEntry(key)
if !ok {
return false, errFindParse.Format("bool", key)
}