From 48bb9c9c118b70250bad510b92ffce86949e90ac Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 15 Nov 2021 15:29:30 +0200 Subject: [PATCH] minor improvements --- HISTORY.md | 4 +++ configuration.go | 1 + context/context.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 1 + 5 files changed, 90 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 7e152467..c28820c7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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. diff --git a/configuration.go b/configuration.go index d677f2f1..14d77a36 100644 --- a/configuration.go +++ b/configuration.go @@ -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. diff --git a/context/context.go b/context/context.go index 6c40fde6..044f7dc1 100644 --- a/context/context.go +++ b/context/context.go @@ -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. diff --git a/go.mod b/go.mod index dc1c209b..5d171d3b 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 3654e062..dfff72af 100644 --- a/go.sum +++ b/go.sum @@ -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=