Skip to content

Commit

Permalink
add Context.Params#GetUint64
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Aug 22, 2018
1 parent 86048ce commit 4467420
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
47 changes: 47 additions & 0 deletions core/memstore/memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4467420

Please sign in to comment.