mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 07:20:35 +01:00
minor improvements
This commit is contained in:
parent
e8dc935f16
commit
48bb9c9c11
|
@ -28,6 +28,10 @@ The codebase for Dependency Injection, Internationalization and localization and
|
|||
|
||||
## Fixes and Improvements
|
||||
|
||||
- `Context.JSON` respects any object implements the `easyjson.Marshaler` interface and renders the result using the [easyjon](https://github.com/mailru/easyjson)'s writer.
|
||||
|
||||
- minor: `Context` structure implements the standard go Context interface now (includes: Deadline, Done, Err and Value methods). Handlers can now just pass the `ctx iris.Context` as a shortcut of `ctx.Request().Context()` when needed.
|
||||
|
||||
- New [x/jsonx](x/jsonx) sub-package for JSON type helpers.
|
||||
|
||||
- New [x/mathx](x/mathx) sub-package for math related functions.
|
||||
|
|
|
@ -832,6 +832,7 @@ type Configuration struct {
|
|||
// "X-Forwarded-For",
|
||||
// "CF-Connecting-IP",
|
||||
// "True-Client-Ip",
|
||||
// "X-Appengine-Remote-Addr",
|
||||
// }
|
||||
//
|
||||
// Look `context.RemoteAddr()` for more.
|
||||
|
|
|
@ -33,6 +33,8 @@ import (
|
|||
"github.com/iris-contrib/schema"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/kataras/golog"
|
||||
"github.com/mailru/easyjson"
|
||||
"github.com/mailru/easyjson/jwriter"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/russross/blackfriday/v2"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
|
@ -3553,6 +3555,12 @@ func WriteJSON(writer io.Writer, v interface{}, options JSON, optimize bool) (in
|
|||
return writer.Write(result)
|
||||
}
|
||||
|
||||
if easyObject, ok := v.(easyjson.Marshaler); ok {
|
||||
jw := jwriter.Writer{NoEscapeHTML: !options.UnescapeHTML}
|
||||
easyObject.MarshalEasyJSON(&jw)
|
||||
return jw.DumpTo(writer)
|
||||
}
|
||||
|
||||
if !optimize && options.Indent == "" {
|
||||
options.Indent = " "
|
||||
}
|
||||
|
@ -5782,6 +5790,81 @@ func (ctx *Context) User() User {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Ensure Iris Context implements the standard Context package, build-time.
|
||||
var _ stdContext.Context = (*Context)(nil)
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Shortcut of Request().Context().Deadline().
|
||||
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
|
||||
return ctx.request.Context().Deadline()
|
||||
}
|
||||
|
||||
// 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.
|
||||
// The close of the Done channel may happen asynchronously,
|
||||
// after the cancel function returns.
|
||||
//
|
||||
// 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 https://blog.golang.org/pipelines for more examples of how to use
|
||||
// a Done channel for cancellation.
|
||||
//
|
||||
// Shortcut of Request().Context().Done().
|
||||
func (ctx *Context) Done() <-chan struct{} {
|
||||
return ctx.request.Context().Done()
|
||||
}
|
||||
|
||||
// If Done is not yet closed, Err returns nil.
|
||||
// If Done is closed, Err returns a non-nil error explaining why:
|
||||
// Canceled if the context was canceled
|
||||
// or DeadlineExceeded if the context's deadline passed.
|
||||
// After Err returns a non-nil error, successive calls to Err return the same error.
|
||||
//
|
||||
// Shortcut of Request().Context().Err().
|
||||
func (ctx *Context) Err() error {
|
||||
return ctx.request.Context().Err()
|
||||
}
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Shortcut of Request().Context().Value(key interface{}) interface{}.
|
||||
func (ctx *Context) Value(key interface{}) interface{} {
|
||||
if keyStr, ok := key.(string); ok { // check if the key is a type of string, which can be retrieved by the mem store.
|
||||
if entry, exists := ctx.values.GetEntry(keyStr); exists {
|
||||
return entry.ValueRaw
|
||||
}
|
||||
}
|
||||
// otherwise return the chained value.
|
||||
return ctx.request.Context().Value(key)
|
||||
}
|
||||
|
||||
const idContextKey = "iris.context.id"
|
||||
|
||||
// SetID sets an ID, any value, to the Request Context.
|
||||
|
|
1
go.mod
1
go.mod
|
@ -69,6 +69,7 @@ require (
|
|||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/imkira/go-interpol v1.1.0 // indirect
|
||||
github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/mediocregopher/radix/v3 v3.8.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
|
|
1
go.sum
generated
1
go.sum
generated
|
@ -111,6 +111,7 @@ github.com/iris-contrib/jade v1.1.4 h1:WoYdfyJFfZIUgqNAeOyRfTNQZOksSlZ6+FnXR3AEp
|
|||
github.com/iris-contrib/jade v1.1.4/go.mod h1:EDqR+ur9piDl6DUgs6qRrlfzmlx/D5UybogqrXvJTBE=
|
||||
github.com/iris-contrib/schema v0.0.6 h1:CPSBLyx2e91H2yJzPuhGuifVRnZBBJ3pCOMbOvPZaTw=
|
||||
github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
|
|
Loading…
Reference in New Issue
Block a user