diff --git a/context/context.go b/context/context.go index 624cb293..46397a10 100644 --- a/context/context.go +++ b/context/context.go @@ -142,20 +142,33 @@ func (r RequestParams) GetDecoded(key string) string { } // GetInt returns the path parameter's value as int, based on its key. +// It checks for all available types of int, including int64, strings etc. +// It will return -1 and a non-nil error if parameter wasn't found. func (r RequestParams) GetInt(key string) (int, error) { return r.store.GetInt(key) } // GetInt64 returns the path paramete's value as int64, based on its key. +// It checks for all available types of int, including int, strings etc. +// It will return -1 and a non-nil error if parameter wasn't found. func (r RequestParams) GetInt64(key string) (int64, error) { return r.store.GetInt64(key) } // GetFloat64 returns a path parameter's value based as float64 on its route's dynamic path key. +// It checks for all available types of int, including float64, int, strings etc. +// It will return -1 and a non-nil error if parameter wasn't found. func (r RequestParams) GetFloat64(key string) (float64, error) { return r.store.GetFloat64(key) } +// GetUint64 returns the path paramete's value as uint64, based on its key. +// It checks for all available types of int, including int, uint64, int64, strings etc. +// It will return 0 and a non-nil error if parameter wasn't found. +func (r RequestParams) GetUint64(key string) (uint64, error) { + return r.store.GetUint64(key) +} + // GetBool returns the path parameter's value as bool, based on its key. // 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". diff --git a/core/memstore/memstore.go b/core/memstore/memstore.go index 51245f79..5b6bbdf2 100644 --- a/core/memstore/memstore.go +++ b/core/memstore/memstore.go @@ -197,6 +197,33 @@ func (e Entry) Float32Default(key string, def float32) (float32, error) { return def, errFindParse.Format("float32", 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) + } + + 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) + } + + 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". @@ -422,6 +449,26 @@ 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) + } + 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 +} + // 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) {