mirror of
https://github.com/kataras/iris.git
synced 2025-02-09 18:46:19 +01:00
# FAQ ### Looking for free support? http://support.iris-go.com https://kataras.rocket.chat/channel/iris ### Looking for previous versions? https://github.com/kataras/iris#version ### Should I upgrade my Iris? Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready. > Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`. For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework). ### About our new home page http://iris-go.com Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome! [Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him. The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please! Read more at https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
194 lines
4.6 KiB
Go
194 lines
4.6 KiB
Go
package context
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/kataras/iris/core/errors"
|
|
)
|
|
|
|
type (
|
|
// RequestValue is the entry of the context storage RequestValues - .Values()
|
|
RequestValue struct {
|
|
key string
|
|
value interface{}
|
|
}
|
|
|
|
// RequestValues is just a key-value storage which context's request values should implement.
|
|
RequestValues []RequestValue
|
|
)
|
|
|
|
// RequestValuesReadOnly the request values with read-only access.
|
|
type RequestValuesReadOnly struct {
|
|
RequestValues
|
|
}
|
|
|
|
// Set does nothing.
|
|
func (r RequestValuesReadOnly) Set(string, interface{}) {}
|
|
|
|
// Set sets a value to the key-value context storage, can be familiar as "User Values".
|
|
func (r *RequestValues) Set(key string, value interface{}) {
|
|
args := *r
|
|
n := len(args)
|
|
for i := 0; i < n; i++ {
|
|
kv := &args[i]
|
|
if kv.key == key {
|
|
kv.value = value
|
|
return
|
|
}
|
|
}
|
|
|
|
c := cap(args)
|
|
if c > n {
|
|
args = args[:n+1]
|
|
kv := &args[n]
|
|
kv.key = key
|
|
kv.value = value
|
|
*r = args
|
|
return
|
|
}
|
|
|
|
kv := RequestValue{}
|
|
kv.key = key
|
|
kv.value = value
|
|
*r = append(args, kv)
|
|
}
|
|
|
|
// Get returns the user's value based on its key.
|
|
func (r *RequestValues) Get(key string) interface{} {
|
|
args := *r
|
|
n := len(args)
|
|
for i := 0; i < n; i++ {
|
|
kv := &args[i]
|
|
if kv.key == key {
|
|
return kv.value
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Visit accepts a visitor which will be filled
|
|
// by the key-value objects, the caller should not try to change the underline values.
|
|
func (r *RequestValues) Visit(visitor func(key string, value interface{})) {
|
|
args := *r
|
|
for i, n := 0, len(args); i < n; i++ {
|
|
visitor(args[i].key, args[i].value)
|
|
}
|
|
}
|
|
|
|
// GetString returns the user's value as string, based on its key.
|
|
func (r *RequestValues) GetString(key string) string {
|
|
if v, ok := r.Get(key).(string); ok {
|
|
return v
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
var errIntParse = errors.New("unable to find or parse the integer, found: %#v")
|
|
|
|
// GetInt returns the user's value as int, based on its key.
|
|
func (r *RequestValues) GetInt(key string) (int, error) {
|
|
v := r.Get(key)
|
|
if vint, ok := v.(int); ok {
|
|
return vint, nil
|
|
} else if vstring, sok := v.(string); sok {
|
|
return strconv.Atoi(vstring)
|
|
}
|
|
|
|
return -1, errIntParse.Format(v)
|
|
}
|
|
|
|
// GetInt64 returns the user's value as int64, based on its key.
|
|
func (r *RequestValues) GetInt64(key string) (int64, error) {
|
|
return strconv.ParseInt(r.GetString(key), 10, 64)
|
|
}
|
|
|
|
// Reset clears all the request values.
|
|
func (r *RequestValues) Reset() {
|
|
*r = (*r)[0:0]
|
|
}
|
|
|
|
// ReadOnly returns a new request values with read-only access.
|
|
func (r *RequestValues) ReadOnly() RequestValuesReadOnly {
|
|
args := *r
|
|
values := make(RequestValues, len(args))
|
|
copy(values, args)
|
|
return RequestValuesReadOnly{values}
|
|
}
|
|
|
|
// Len returns the full length of the values.
|
|
func (r *RequestValues) Len() int {
|
|
args := *r
|
|
return len(args)
|
|
}
|
|
|
|
// RequestParam is the entry of RequestParams, request's url named parameters are storaged here.
|
|
type RequestParam struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
// RequestParams is a key string - value string storage which context's request params should implement.
|
|
// RequestValues is for communication between middleware, RequestParams cannot be changed, are setted at the routing
|
|
// time, stores the dynamic named parameters, can be empty if the route is static.
|
|
type RequestParams []RequestParam
|
|
|
|
// Get returns the param's value based on its key.
|
|
func (r RequestParams) Get(key string) string {
|
|
for _, p := range r {
|
|
if p.Key == key {
|
|
return p.Value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Visit accepts a visitor which will be filled
|
|
// by the key and value.
|
|
// The caller should not try to change the underline values.
|
|
func (r RequestParams) Visit(visitor func(key string, value string)) {
|
|
for i, n := 0, len(r); i < n; i++ {
|
|
visitor(r[i].Key, r[i].Value)
|
|
}
|
|
}
|
|
|
|
// GetInt returns the user's value as int, based on its key.
|
|
func (r RequestParams) GetInt(key string) (int, error) {
|
|
v := r.Get(key)
|
|
return strconv.Atoi(v)
|
|
}
|
|
|
|
// GetInt64 returns the user's value as int64, based on its key.
|
|
func (r RequestParams) GetInt64(key string) (int64, error) {
|
|
return strconv.ParseInt(r.Get(key), 10, 64)
|
|
}
|
|
|
|
// GetDecoded returns the url-query-decoded user's value based on its key.
|
|
func (r RequestParams) GetDecoded(key string) string {
|
|
return DecodeQuery(DecodeQuery(r.Get(key)))
|
|
}
|
|
|
|
// GetIntUnslashed same as Get but it removes the first slash if found.
|
|
// Usage: Get an id from a wildcard path.
|
|
//
|
|
// Returns -1 with an error if the parameter couldn't be found.
|
|
func (r RequestParams) GetIntUnslashed(key string) (int, error) {
|
|
v := r.Get(key)
|
|
if v != "" {
|
|
if len(v) > 1 {
|
|
if v[0] == '/' {
|
|
v = v[1:]
|
|
}
|
|
}
|
|
return strconv.Atoi(v)
|
|
|
|
}
|
|
|
|
return -1, errIntParse.Format(v)
|
|
}
|
|
|
|
// Len returns the full length of the params.
|
|
func (r RequestParams) Len() int {
|
|
return len(r)
|
|
}
|