implement ReadQuery with 'url' struct field tag name strictly, as requested at: #1207

Former-commit-id: dc0c237f62aa6db5a0c1755b2074d8a18dba0d8f
This commit is contained in:
Gerasimos (Makis) Maropoulos 2019-07-24 03:29:42 +03:00
parent 227eda3bcc
commit db0702ca75
8 changed files with 65 additions and 15 deletions

View File

@ -21,6 +21,11 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene
**How to upgrade**: Open your command-line and execute this command: `go get github.com/kataras/iris@v11.2.0`. **How to upgrade**: Open your command-line and execute this command: `go get github.com/kataras/iris@v11.2.0`.
# We, 24 July 2019 | v11.2.1
- https://github.com/kataras/iris/issues/1298
- https://github.com/kataras/iris/issues/1207
# Tu, 23 July 2019 | v11.2.0 # Tu, 23 July 2019 | v11.2.0
Read about the new release at: https://dev.to/kataras/iris-version-11-2-released-22bc Read about the new release at: https://dev.to/kataras/iris-version-11-2-released-22bc

View File

@ -225,6 +225,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
* [Struct Validation](http_request/read-json-struct-validation/main.go) * [Struct Validation](http_request/read-json-struct-validation/main.go)
- [Read XML](http_request/read-xml/main.go) - [Read XML](http_request/read-xml/main.go)
- [Read Form](http_request/read-form/main.go) - [Read Form](http_request/read-form/main.go)
- [Read Query](http_request/read-query/main.go) **NEW**
- [Read Custom per type](http_request/read-custom-per-type/main.go) - [Read Custom per type](http_request/read-custom-per-type/main.go)
- [Read Custom via Unmarshaler](http_request/read-custom-via-unmarshaler/main.go) - [Read Custom via Unmarshaler](http_request/read-custom-via-unmarshaler/main.go)
- [Read Many times](http_request/read-many/main.go) - [Read Many times](http_request/read-many/main.go)

View File

@ -331,6 +331,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
- [读取JSON](http_request/read-json/main.go) - [读取JSON](http_request/read-json/main.go)
- [读取XML](http_request/read-xml/main.go) - [读取XML](http_request/read-xml/main.go)
- [读取Form](http_request/read-form/main.go) - [读取Form](http_request/read-form/main.go)
- [读取Query](http_request/read-query/main.go) **更新**
- [读取每个类型的自定义结果Custom per type](http_request/read-custom-per-type/main.go) - [读取每个类型的自定义结果Custom per type](http_request/read-custom-per-type/main.go)
- [通过Unmarshaler读取Custom](http_request/read-custom-via-unmarshaler/main.go) - [通过Unmarshaler读取Custom](http_request/read-custom-via-unmarshaler/main.go)
- [上传/读取文件Upload/Read File](http_request/upload-file/main.go) - [上传/读取文件Upload/Read File](http_request/upload-file/main.go)

View File

@ -0,0 +1,29 @@
// package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON
package main
import (
"github.com/kataras/iris"
)
type MyType struct {
Name string `url:"name"`
Age int `url:"age"`
}
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
var t MyType
err := ctx.ReadQuery(&t)
if err != nil && !iris.IsErrPath(err) {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString(err.Error())
}
ctx.Writef("MyType: %#v", t)
})
// http://localhost:8080?name=iris&age=3
app.Run(iris.Addr(":8080"))
}

View File

@ -27,7 +27,7 @@ import (
"github.com/Shopify/goreferrer" "github.com/Shopify/goreferrer"
"github.com/fatih/structs" "github.com/fatih/structs"
"github.com/iris-contrib/blackfriday" "github.com/iris-contrib/blackfriday"
formbinder "github.com/iris-contrib/formBinder" "github.com/iris-contrib/schema"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -589,10 +589,14 @@ type Context interface {
// ReadForm binds the formObject with the form data // ReadForm binds the formObject with the form data
// it supports any kind of type, including custom structs. // it supports any kind of type, including custom structs.
// It will return nothing if request data are empty. // It will return nothing if request data are empty.
// The struct field tag is "form".
// //
// Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go // Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go
ReadForm(formObjectPtr interface{}) error ReadForm(formObject interface{}) error
// ReadQuery binds the "ptr" with the url query string. The struct field tag is "url".
//
// Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-query/main.go
ReadQuery(ptr interface{}) error
// +------------------------------------------------------------+ // +------------------------------------------------------------+
// | Body (raw) Writers | // | Body (raw) Writers |
// +------------------------------------------------------------+ // +------------------------------------------------------------+
@ -2389,16 +2393,17 @@ func (ctx *context) ReadXML(xmlObject interface{}) error {
return ctx.UnmarshalBody(xmlObject, UnmarshalerFunc(xml.Unmarshal)) return ctx.UnmarshalBody(xmlObject, UnmarshalerFunc(xml.Unmarshal))
} }
// IsErrPath can be used at `context#ReadForm`. // IsErrPath can be used at `context#ReadForm` and `context#ReadQuery`.
// It reports whether the incoming error // It reports whether the incoming error
// can be ignored when server allows unknown post values to be sent by the client. // can be ignored when server allows unknown post values to be sent by the client.
// //
// A shortcut for the `formbinder#IsErrPath`. // A shortcut for the `schema#IsErrPath`.
var IsErrPath = formbinder.IsErrPath var IsErrPath = schema.IsErrPath
// ReadForm binds the formObject with the form data // ReadForm binds the formObject with the form data
// it supports any kind of type, including custom structs. // it supports any kind of type, including custom structs.
// It will return nothing if request data are empty. // It will return nothing if request data are empty.
// The struct field tag is "form".
// //
// Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go // Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go
func (ctx *context) ReadForm(formObject interface{}) error { func (ctx *context) ReadForm(formObject interface{}) error {
@ -2407,10 +2412,19 @@ func (ctx *context) ReadForm(formObject interface{}) error {
return nil return nil
} }
// or dec := formbinder.NewDecoder(&formbinder.DecoderOptions{TagName: "form"}) return schema.DecodeForm(values, formObject)
// somewhere at the app level. I did change the tagName to "form" }
// inside its source code, so it's not needed for now.
return formbinder.Decode(values, formObject) // ReadQuery binds the "ptr" with the url query string. The struct field tag is "url".
//
// Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-query/main.go
func (ctx *context) ReadQuery(ptr interface{}) error {
values := ctx.request.URL.Query()
if len(values) == 0 {
return nil
}
return schema.DecodeQuery(values, ptr)
} }
// +------------------------------------------------------------+ // +------------------------------------------------------------+

4
doc.go
View File

@ -38,13 +38,13 @@ Source code and other details for the project are available at GitHub:
Current Version Current Version
11.2.0 11.2.1
Installation Installation
The only requirement is the Go Programming Language, at least version 1.12. The only requirement is the Go Programming Language, at least version 1.12.
$ go get github.com/kataras/iris@v11.2.0 $ go get github.com/kataras/iris@v11.2.1
Wiki: Wiki:

2
go.mod
View File

@ -16,7 +16,6 @@ require (
github.com/gavv/httpexpect v2.0.0+incompatible github.com/gavv/httpexpect v2.0.0+incompatible
github.com/hashicorp/go-version v1.2.0 // indirect github.com/hashicorp/go-version v1.2.0 // indirect
github.com/iris-contrib/blackfriday v2.0.0+incompatible github.com/iris-contrib/blackfriday v2.0.0+incompatible
github.com/iris-contrib/formBinder v5.0.0+incompatible
github.com/iris-contrib/go.uuid v2.0.0+incompatible github.com/iris-contrib/go.uuid v2.0.0+incompatible
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0 github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0
github.com/json-iterator/go v1.1.6 github.com/json-iterator/go v1.1.6
@ -26,4 +25,5 @@ require (
github.com/mediocregopher/radix/v3 v3.3.0 github.com/mediocregopher/radix/v3 v3.3.0
github.com/microcosm-cc/bluemonday v1.0.2 github.com/microcosm-cc/bluemonday v1.0.2
github.com/ryanuber/columnize v2.1.0+incompatible github.com/ryanuber/columnize v2.1.0+incompatible
github.com/iris-contrib/schema v0.0.1
) )

4
go.sum
View File

@ -10,8 +10,6 @@ github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nI
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc= github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI= github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI=
github.com/iris-contrib/formBinder v5.0.0+incompatible h1:jL+H+cCSEV8yzLwVbBI+tLRN/PpVatZtUZGK9ldi3bU=
github.com/iris-contrib/formBinder v5.0.0+incompatible/go.mod h1:i8kTYUOEstd/S8TG0ChTXQdf4ermA/e8vJX0+QruD9w=
github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE= github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE=
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI= github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI=
@ -24,3 +22,5 @@ github.com/mediocregopher/radix/v3 v3.3.0/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQ
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
github.com/iris-contrib/schema v0.0.1 h1:10g/WnoRR+U+XXHWKBHeNy/+tZmM2kcAVGLOsz+yaDA=
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=