add Context.ReadParams method

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-31 20:30:01 +03:00
parent 2f51845138
commit 477f5e6f9b
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
7 changed files with 91 additions and 11 deletions

View File

@ -600,6 +600,7 @@ New Package-level Variables:
New Context Methods:
- `Context.ReadParams(ptr interface{}) error` to bind dynamic path parameters. [Example](https://github.com/kataras/iris/blob/master/_examples/request-body/read-params/main.go).
- `Context.SaveFormFile(fh *multipart.FileHeader, dest string) (int64, error)` previously unexported. Accepts a result file of `Context.FormFile` and saves it to the disk.
- `Context.URLParamSlice(name string) []string` is a a shortcut of `ctx.Request().URL.Query()[name]`. Like `URLParam` but it returns all values as a string slice instead of a single string separated by commas.
- `Context.PostValueMany(name string) (string, error)` returns the post data of a given key. The returned value is a single string separated by commas on multiple values. It also reports whether the form was empty or when the "name" does not exist or whether the available values are empty. It strips any empty key-values from the slice before return. See `ErrEmptyForm`, `ErrNotFound` and `ErrEmptyFormField` respectfully. The `PostValueInt`, `PostValueInt64`, `PostValueFloat64` and `PostValueBool` now respect the above errors too (the `PostValues` method now returns a second output argument of `error` too, see breaking changes below).

View File

@ -138,6 +138,7 @@
* [Bind YAML](request-body/read-yaml/main.go)
* [Bind Form](request-body/read-form/main.go)
* [Bind Query](request-body/read-query/main.go)
* [Bind Params](request-body/read-params/main.go)
* [Bind Body](request-body/read-body/main.go)
* [Bind Custom per type](request-body/read-custom-per-type/main.go)
* [Bind Custom via Unmarshaler](request-body/read-custom-via-unmarshaler/main.go)

View File

@ -0,0 +1,37 @@
// package main contains an example on how to use the ReadQuery,
// same way you can do the ReadJSON & ReadProtobuf and e.t.c.
package main
import (
"github.com/kataras/iris/v12"
)
type myParams struct {
Name string `param:"name"`
Age int `param:"age"`
Tail []string `param:"tail"`
}
func main() {
app := newApp()
// http://localhost:8080/kataras/27/iris/web/framework
// MyType: main.MyType{Name:"kataras", Age:27, Tail:[]string{"iris", "web", "framework"}}
app.Listen(":8080")
}
func newApp() *iris.Application {
app := iris.New()
app.Get("/{name}/{age:int}/{tail:path}", func(ctx iris.Context) {
var p myParams
if err := ctx.ReadParams(&p); err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Writef("myParams: %#v", p)
})
return app
}

View File

@ -0,0 +1,16 @@
package main
import (
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestReadParams(t *testing.T) {
app := newApp()
e := httptest.New(t, app)
expectedBody := `myParams: main.myParams{Name:"kataras", Age:27, Tail:[]string{"iris", "web", "framework"}}`
e.GET("/kataras/27/iris/web/framework").Expect().Status(httptest.StatusOK).Body().Equal(expectedBody)
}

View File

@ -7,7 +7,7 @@ import (
)
type MyType struct {
Name string `url:"name"`
Name string `url:"name,required"`
Age int `url:"age"`
}

View File

@ -2272,7 +2272,7 @@ func (ctx *Context) ReadForm(formObject interface{}) error {
return ctx.app.Validate(formObject)
}
// ReadQuery binds url query to "ptr". The struct field tag is "url".
// ReadQuery binds URL Query to "ptr". The struct field tag is "url".
//
// Example: https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go
func (ctx *Context) ReadQuery(ptr interface{}) error {
@ -2289,6 +2289,31 @@ func (ctx *Context) ReadQuery(ptr interface{}) error {
return ctx.app.Validate(ptr)
}
// ReadParams binds URI Dynamic Path Parameters to "ptr". The struct field tag is "param".
//
// Example: https://github.com/kataras/iris/blob/master/_examples/request-body/read-params/main.go
func (ctx *Context) ReadParams(ptr interface{}) error {
n := ctx.params.Len()
if n == 0 {
return nil
}
values := make(map[string][]string, n)
ctx.params.Visit(func(key string, value string) {
// []string on path parameter, e.g.
// /.../{tail:path}
// Tail []string `param:"tail"`
values[key] = strings.Split(value, "/")
})
err := schema.DecodeParams(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 {

18
go.mod
View File

@ -8,16 +8,16 @@ require (
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398
github.com/andybalholm/brotli v1.0.1-0.20200619015827-c3da72aa01ed
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible
github.com/dgraph-io/badger/v2 v2.0.3
github.com/dgraph-io/badger/v2 v2.2007.1
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
github.com/fatih/structs v1.1.0
github.com/gomodule/redigo v1.8.2
github.com/google/uuid v1.1.2-0.20200519141726-cb32006e483f
github.com/google/uuid v1.1.2
github.com/hashicorp/go-version v1.2.1
github.com/iris-contrib/httpexpect/v2 v2.0.5
github.com/iris-contrib/jade v1.1.4
github.com/iris-contrib/pongo2 v0.0.1
github.com/iris-contrib/schema v0.0.2
github.com/iris-contrib/schema v0.0.3
github.com/json-iterator/go v1.1.10
github.com/kataras/blocks v0.0.2
github.com/kataras/golog v0.1.2
@ -29,19 +29,19 @@ require (
github.com/mediocregopher/radix/v3 v3.5.2
github.com/microcosm-cc/bluemonday v1.0.4
github.com/russross/blackfriday/v2 v2.0.1
github.com/ryanuber/columnize v2.1.0+incompatible
github.com/ryanuber/columnize v2.1.2+incompatible
github.com/schollz/closestmatch v2.1.0+incompatible
github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693
github.com/tdewolff/minify/v2 v2.8.0
github.com/tdewolff/minify/v2 v2.9.2
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
github.com/yosssi/ace v0.0.5
go.etcd.io/bbolt v1.3.5
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/net v0.0.0-20200822124328-c89045814202
golang.org/x/sys v0.0.0-20200828194041-157a740278f4
golang.org/x/text v0.3.3
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
google.golang.org/protobuf v1.25.0
gopkg.in/ini.v1 v1.58.0
gopkg.in/ini.v1 v1.60.2
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)