1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-31 00:37:05 +00:00

add some more helpers for the parameters and the memstore for num types

Former-commit-id: b96380fa8c8dc9abeaea248f87ea5d70f6bdf650
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-10-01 15:27:45 +03:00
parent a675e8191a
commit 2a1f3d4e43

View File

@@ -7,6 +7,7 @@ package memstore
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
@@ -117,12 +118,13 @@ func (e Entry) IntDefault(def int) (int, error) {
if err != nil {
return def, err
}
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:
@@ -131,6 +133,8 @@ func (e Entry) IntDefault(def int) (int, error) {
return int(vv), nil
case uint8:
return int(vv), nil
case uint16:
return int(vv), nil
case uint32:
return int(vv), nil
case uint64:
@@ -140,6 +144,96 @@ func (e Entry) IntDefault(def int) (int, error) {
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) {
@@ -164,6 +258,266 @@ func (e Entry) Int64Default(def int64) (int64, error) {
return def, errFindParse.Format("int64", e.Key)
}
// UintDefault returns the entry's value as uint.
// If not found returns "def" and a non-nil error.
func (e Entry) UintDefault(def uint) (uint, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint", e.Key)
}
x64 := strconv.IntSize == 64
var maxValue uint = math.MaxUint32
if x64 {
maxValue = math.MaxUint64
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, strconv.IntSize)
if err != nil {
return def, err
}
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("uint", e.Key)
}
// Uint8Default returns the entry's value as uint8.
// If not found returns "def" and a non-nil error.
func (e Entry) Uint8Default(def uint8) (uint8, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint8", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 8)
if err != nil {
return def, err
}
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("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.
// If not found returns "def" and a non-nil error.
func (e Entry) Uint64Default(def uint64) (uint64, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint64", e.Key)
}
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) {
@@ -196,85 +550,6 @@ func (e Entry) Float64Default(def float64) (float64, error) {
return def, errFindParse.Format("float64", 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
}
return float32(val), nil
case float32:
return vv, nil
case float64:
return float32(vv), nil
case int:
return float32(vv), nil
}
return def, errFindParse.Format("float32", e.Key)
}
// Uint8Default returns the entry's value as uint8.
// If not found returns "def" and a non-nil error.
func (e Entry) Uint8Default(def uint8) (uint8, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint8", e.Key)
}
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 8)
if err != nil {
return def, err
}
if val > 255 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(val), nil
case uint8:
return vv, nil
case int:
if vv < 0 || vv > 255 {
return def, errFindParse.Format("uint8", e.Key)
}
return uint8(vv), nil
}
return def, errFindParse.Format("uint8", e.Key)
}
// Uint64Default returns the entry's value as uint64.
// If not found returns "def" and a non-nil error.
func (e Entry) Uint64Default(def uint64) (uint64, error) {
v := e.ValueRaw
if v == nil {
return def, errFindParse.Format("uint64", e.Key)
}
switch vv := v.(type) {
case string:
return strconv.ParseUint(vv, 10, 64)
case uint64:
return vv, nil
case int64:
return uint64(vv), nil
case int:
return uint64(vv), nil
}
return def, errFindParse.Format("uint64", 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".
@@ -508,40 +783,60 @@ func (r *Store) GetIntDefault(key string, def int) int {
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) {
// 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 0, errFindParse.Format("uint8", key)
return -1, errFindParse.Format("int8", key)
}
return v.Uint8Default(0)
return v.Int8Default(-1)
}
// GetUint8Default returns the entry's value as uint8, based on its key.
// GetInt8Default returns the entry's value as int8, 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 {
func (r *Store) GetInt8Default(key string, def int8) int8 {
if v, err := r.GetInt8(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) {
// 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 0, errFindParse.Format("uint64", key)
return -1, errFindParse.Format("int16", key)
}
return v.Uint64Default(0)
return v.Int16Default(-1)
}
// GetUint64Default returns the entry's value as uint64, based on its key.
// GetInt16Default returns the entry's value as int16, 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) 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
}
@@ -568,6 +863,106 @@ 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) {