diff --git a/HISTORY.md b/HISTORY.md index 940c51cd..7e885aac 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -310,6 +310,7 @@ var dirOpts = iris.DirOptions{ ## New Context Methods +- `Context.ReadURL(ptr interface{}) error` shortcut of `ReadParams` and `ReadQuery`. Binds URL dynamic path parameters and URL query parameters to the given "ptr" pointer of a struct value. - `Context.SetUser(User)` and `Context.User() User` to store and retrieve an authenticated client. Read more [here](https://github.com/iris-contrib/middleware/issues/63). - `Context.SetLogoutFunc(fn interface{}, persistenceArgs ...interface{})` and `Logout(args ...interface{}) error` methods to allow different kind of auth middlewares to be able to set a "logout" a user/client feature with a single function, the route handler may not be aware of the implementation of the authentication used. - `Context.SetFunc(name string, fn interface{}, persistenceArgs ...interface{})` and `Context.CallFunc(name string, args ...interface{}) ([]reflect.Value, error)` to allow middlewares to share functions dynamically when the type of the function is not predictable, see the [example](https://github.com/kataras/iris/tree/master/_examples/routing/writing-a-middleware/share-funcs) for more. diff --git a/README.md b/README.md index d764dbf4..15ff2bc2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ With your help, we can improve Open Source web development for everyone! > Donations from **China** are now accepted!
+
diff --git a/context/context.go b/context/context.go
index f4352ba8..5eaca26d 100644
--- a/context/context.go
+++ b/context/context.go
@@ -2359,6 +2359,30 @@ func (ctx *Context) ReadParams(ptr interface{}) error {
return ctx.app.Validate(ptr)
}
+// ReadURL is a shortcut of ReadParams and ReadQuery.
+// It binds dynamic path parameters and URL query parameters
+// to the "ptr" pointer struct value.
+// The struct fields may contain "url" or "param" binding tags.
+// If a validator exists then it validates the result too.
+func (ctx *Context) ReadURL(ptr interface{}) error {
+ values := make(map[string][]string, ctx.params.Len())
+ ctx.params.Visit(func(key string, value string) {
+ values[key] = strings.Split(value, "/")
+ })
+
+ for k, v := range ctx.getQuery() {
+ values[k] = append(values[k], v...)
+ }
+
+ // Decode using all available binding tags (url, header, param).
+ err := schema.Decode(values, ptr)
+ if err != nil {
+ return err
+ }
+
+ return ctx.app.Validate(ptr)
+}
+
// ReadProtobuf binds the body to the "ptr" of a proto Message and returns any error.
// Look `ReadJSONProtobuf` too.
func (ctx *Context) ReadProtobuf(ptr proto.Message) error {