Skip to content

Commit

Permalink
implement ReadQuery with 'url' struct field tag name strictly, as req…
Browse files Browse the repository at this point in the history
…uested at: kataras#1207

Former-commit-id: dc0c237f62aa6db5a0c1755b2074d8a18dba0d8f
  • Loading branch information
kataras committed Jul 24, 2019
1 parent 227eda3 commit db0702c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

# 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

Read about the new release at: https://dev.to/kataras/iris-version-11-2-released-22bc
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [Read XML](http_request/read-xml/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 via Unmarshaler](http_request/read-custom-via-unmarshaler/main.go)
- [Read Many times](http_request/read-many/main.go)
Expand Down
1 change: 1 addition & 0 deletions _examples/README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
- [读取JSON](http_request/read-json/main.go)
- [读取XML](http_request/read-xml/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)
- [通过Unmarshaler读取Custom](http_request/read-custom-via-unmarshaler/main.go)
- [上传/读取文件Upload/Read File](http_request/upload-file/main.go)
Expand Down
29 changes: 29 additions & 0 deletions _examples/http_request/read-query/main.go
Original file line number Diff line number Diff line change
@@ -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"))
}
34 changes: 24 additions & 10 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/Shopify/goreferrer"
"github.com/fatih/structs"
"github.com/iris-contrib/blackfriday"
formbinder "github.com/iris-contrib/formBinder"
"github.com/iris-contrib/schema"
jsoniter "github.com/json-iterator/go"
"github.com/microcosm-cc/bluemonday"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -589,10 +589,14 @@ type Context interface {
// ReadForm binds the formObject with the form data
// it supports any kind of type, including custom structs.
// 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
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 |
// +------------------------------------------------------------+
Expand Down Expand Up @@ -2389,16 +2393,17 @@ func (ctx *context) ReadXML(xmlObject interface{}) error {
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
// can be ignored when server allows unknown post values to be sent by the client.
//
// A shortcut for the `formbinder#IsErrPath`.
var IsErrPath = formbinder.IsErrPath
// A shortcut for the `schema#IsErrPath`.
var IsErrPath = schema.IsErrPath

// ReadForm binds the formObject with the form data
// it supports any kind of type, including custom structs.
// 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
func (ctx *context) ReadForm(formObject interface{}) error {
Expand All @@ -2407,10 +2412,19 @@ func (ctx *context) ReadForm(formObject interface{}) error {
return nil
}

// or dec := formbinder.NewDecoder(&formbinder.DecoderOptions{TagName: "form"})
// 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)
return schema.DecodeForm(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)
}

// +------------------------------------------------------------+
Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ Source code and other details for the project are available at GitHub:
Current Version
11.2.0
11.2.1
Installation
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:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/hashicorp/go-version v1.2.0 // indirect
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/i18n v0.0.0-20171121225848-987a633949d0
github.com/json-iterator/go v1.1.6
Expand All @@ -26,4 +25,5 @@ require (
github.com/mediocregopher/radix/v3 v3.3.0
github.com/microcosm-cc/bluemonday v1.0.2
github.com/ryanuber/columnize v2.1.0+incompatible
github.com/iris-contrib/schema v0.0.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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/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/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/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI=
Expand All @@ -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/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=
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=

0 comments on commit db0702c

Please sign in to comment.