Add nil checks for memstore's GetInt, GetInt64, GetFloat64, GetBool and remove the golang/net/context's interface completion from Context

Former-commit-id: f79940acf96d44df5bae8eaaaf20086fdc5e251e
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-09-29 03:54:05 +03:00
parent 95fe7c2853
commit 1a57504431
3 changed files with 50 additions and 209 deletions

View File

@ -1,8 +1,8 @@
# ![Logo created by @santoshanand](logo_white_35_24.png) Iris
<!-- # ![Logo created by @santoshanand](logo_white_35_24.png) Iris -->
<!-- <p align="center">
<p align="center">
<img src="iris_the_new_machine.gif">
</p> -->
</p>
Iris is a fast, simple and efficient micro web framework for Go. It provides a beautifully expressive and easy to use foundation for your next website, API, or distributed app.
@ -36,9 +36,17 @@ Iris was built on top of the the [net/http](https://golang.org/pkg/net/http/) pa
<!-- has issues, the img.shields.io's API is problematic for this resource: [![issue stats](https://img.shields.io/issuestats/i/github/kataras/iris.svg?style=flat-square)](https://github.com/kataras/iris/issues?q=is%3Aissue+is%3Aclosed) -->
<!-- [![godocs](https://img.shields.io/badge/godocs-8.x.x-0366d6.svg?style=flat-square)](https://godoc.org/github.com/kataras/iris) -->
<p>
<img src="https://raw.githubusercontent.com/smallnest/go-web-framework-benchmark/4db507a22c964c9bc9774c5b31afdc199a0fe8b7/benchmark.png" alt="Third-party source for transparency." />
</p>
![Iris vs .NET Core(C#)](https://iris-go.com/images/benchmark-iris-vs-netcore.png)
_Updated at: [Friday, 29 September 2017](_examples)_
<details>
<summary>Benchmarks from third-party source over the rest web frameworks</summary>
![Comparison with other frameworks](https://raw.githubusercontent.com/smallnest/go-web-framework-benchmark/4db507a22c964c9bc9774c5b31afdc199a0fe8b7/benchmark.png)
</details>
### 📑 Table of contents

View File

@ -745,99 +745,6 @@ type Context interface {
// to be executed at serve-time. The full app's fields
// and methods are not available here for the developer's safety.
Application() Application
// +--------------------------------------------------------------+
// | https://github.com/golang/net/blob/master/context/context.go | |
// +--------------------------------------------------------------+
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
Deadline() (deadline time.Time, ok bool)
// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
//
// WithCancel arranges for Done to be closed when cancel is called;
// WithDeadline arranges for Done to be closed when the deadline
// expires; WithTimeout arranges for Done to be closed when the timeout
// elapses.
//
// Done is provided for use in select statements:
//
// // Stream generates values with DoSomething and sends them to out
// // until DoSomething returns an error or ctx.Done is closed.
// func Stream(ctx context.Context, out chan<- Value) error {
// for {
// v, err := DoSomething(ctx)
// if err != nil {
// return err
// }
// select {
// case <-ctx.Done():
// return ctx.Err()
// case out <- v:
// }
// }
// }
//
// See http://blog.golang.org/pipelines for more examples of how to use
// a Done channel for cancelation.
Done() <-chan struct{}
// Err returns a non-nil error value after Done is closed. Err returns
// Canceled if the context was canceled or DeadlineExceeded if the
// context's deadline passed. No other values for Err are defined.
// After Done is closed, successive calls to Err return the same value.
Err() error
// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
//
// Use context values only for request-scoped data that transits
// processes and API boundaries, not for passing optional parameters to
// functions.
//
// A key identifies a specific value in a Context. Functions that wish
// to store values in Context typically allocate a key in a global
// variable then use that key as the argument to context.WithValue and
// Context.Value. A key can be any type that supports equality;
// packages should define keys as an unexported type to avoid
// collisions.
//
// Packages that define a Context key should provide type-safe accessors
// for the values stores using that key:
//
// // Package user defines a User type that's stored in Contexts.
// package user
//
// import "golang.org/x/net/context"
//
// // User is the type of value stored in the Contexts.
// type User struct {...}
//
// // key is an unexported type for keys defined in this package.
// // This prevents collisions with keys defined in other packages.
// type key int
//
// // userKey is the key for user.User values in Contexts. It is
// // unexported; clients use user.NewContext and user.FromContext
// // instead of using this key directly.
// var userKey key = 0
//
// // NewContext returns a new Context that carries value u.
// func NewContext(ctx context.Context, u *User) context.Context {
// return context.WithValue(ctx, userKey, u)
// }
//
// // FromContext returns the User value stored in ctx, if any.
// func FromContext(ctx context.Context) (*User, bool) {
// u, ok := ctx.Value(userKey).(*User)
// return u, ok
// }
Value(key interface{}) interface{}
}
// Next calls all the next handler from the handlers chain,
@ -2673,110 +2580,3 @@ func (ctx *context) Exec(method string, path string) {
func (ctx *context) Application() Application {
return ctx.app
}
// +--------------------------------------------------------------+
// | https://github.com/golang/net/blob/master/context/context.go | |
// +--------------------------------------------------------------+
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
func (ctx *context) Deadline() (deadline time.Time, ok bool) {
return
}
// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
//
// WithCancel arranges for Done to be closed when cancel is called;
// WithDeadline arranges for Done to be closed when the deadline
// expires; WithTimeout arranges for Done to be closed when the timeout
// elapses.
//
// Done is provided for use in select statements:
//
// // Stream generates values with DoSomething and sends them to out
// // until DoSomething returns an error or ctx.Done is closed.
// func Stream(ctx context.Context, out chan<- Value) error {
// for {
// v, err := DoSomething(ctx)
// if err != nil {
// return err
// }
// select {
// case <-ctx.Done():
// return ctx.Err()
// case out <- v:
// }
// }
// }
//
// See http://blog.golang.org/pipelines for more examples of how to use
// a Done channel for cancelation.
func (ctx *context) Done() <-chan struct{} {
return nil
}
// Err returns a non-nil error value after Done is closed. Err returns
// Canceled if the context was canceled or DeadlineExceeded if the
// context's deadline passed. No other values for Err are defined.
// After Done is closed, successive calls to Err return the same value.
func (ctx *context) Err() error {
return nil
}
// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
//
// Use context values only for request-scoped data that transits
// processes and API boundaries, not for passing optional parameters to
// functions.
//
// A key indentifies a specific value in a context. Functions that wish
// to store values in context typically allocate a key in a global
// variable then use that key as the argument to context.WithValue and
// context.Value. A key can be any type that supports equality;
// packages should define keys as an unexported type to avoid
// collisions.
//
// Packages that define a context key should provide type-safe accessors
// for the values stores using that key:
//
// // Package user defines a User type that's stored in Contexts.
// package user
//
// import "golang.org/x/net/context"
//
// // User is the type of value stored in the Contexts.
// type User struct {...}
//
// // key is an unexported type for keys defined in this package.
// // This prevents collisions with keys defined in other packages.
// type key int
//
// // userKey is the key for user.User values in Contexts. It is
// // unexported; clients use user.NewContext and user.FromContext
// // instead of using this key directly.
// var userKey key = 0
//
// // NewContext returns a new context that carries value u.
// func NewContext(ctx context.Context, u *User) context.Context {
// return context.WithValue(ctx, userKey, u)
// }
//
// // FromContext returns the User value stored in ctx, if any.
// func FromContext(ctx context.Context) (*User, bool) {
// u, ok := ctx.Value(userKey).(*User)
// return u, ok
// }
func (ctx *context) Value(key interface{}) interface{} {
if key == 0 {
return ctx.request
}
if k, ok := key.(string); ok {
return ctx.values.GetString(k)
}
return nil
}

View File

@ -162,8 +162,13 @@ 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 {
if v, ok := r.Get(key).(string); ok {
return v
v := r.Get(key)
if v == nil {
return def
}
if vString, ok := v.(string); ok {
return vString
}
return def
@ -187,6 +192,9 @@ var ErrIntParse = errors.New("unable to find or parse the integer, found: %#v")
// If not found returns "def".
func (r *Store) GetIntDefault(key string, def int) (int, error) {
v := r.Get(key)
if v == nil {
return def, nil
}
if vint, ok := v.(int); ok {
return vint, nil
} else if vstring, sok := v.(string); sok {
@ -209,6 +217,9 @@ func (r *Store) GetInt(key string) (int, error) {
// If not found returns "def".
func (r *Store) GetInt64Default(key string, def int64) (int64, error) {
v := r.Get(key)
if v == nil {
return def, nil
}
if vint64, ok := v.(int64); ok {
return vint64, nil
} else if vstring, sok := v.(string); sok {
@ -231,6 +242,9 @@ func (r *Store) GetInt64(key string) (int64, error) {
// If not found returns "def".
func (r *Store) GetFloat64Default(key string, def float64) (float64, error) {
v := r.Get(key)
if v == nil {
return def, nil
}
if vfloat64, ok := v.(float64); ok {
return vfloat64, nil
} else if vstring, sok := v.(string); sok {
@ -249,12 +263,31 @@ func (r *Store) GetFloat64(key string) (float64, error) {
return r.GetFloat64Default(key, 0.0)
}
// GetBoolDefault returns the user'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".
// Any other value returns an error.
//
// If not found returns "def".
func (r *Store) GetBoolDefault(key string, def bool) (bool, error) {
v := r.Get(key)
if v == nil {
return def, nil
}
if vString, ok := v.(string); ok {
return strconv.ParseBool(vString)
}
return def, nil
}
// GetBool returns the user'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".
// Any other value returns an error.
//
// If not found returns false.
func (r *Store) GetBool(key string) (bool, error) {
return strconv.ParseBool(key)
return r.GetBoolDefault(key, false)
}
// Remove deletes an entry linked to that "key",