GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Fix #216: Enable to call binding multiple times in some formats by delphinus · Pull Request #1341 · gin-gonic/gin Conversationclose #216 Reported problems#216 says some formats in requests' body cannot be bound into different structs by calling multiple times. type modelA struct { Foo string `form:"foo" json:"foo" binding:"required"` Bar int64 `form:"bar" json:"bar" binding:"required"` } type modelB struct { Hoge string `form:"hoge" json:"hoge" xml:"hoge" binding:"required"` Fuga int64 `form:"fuga" json:"fuga" xml:"fuga" binding:"required"` } func main() { r := gin.New() r.POST("/json", func(c *gin.Context) { fa, fb := modelA{}, modelB{} if aerr := c.ShouldBindWith(&fa, binding.JSON); aerr == nil { c.JSON(http.StatusOK, gin.H{"form": fa}) } else if berr := c.ShouldBindWith(&fb, binding.JSON); berr == nil { c.JSON(http.StatusOK, gin.H{"form": fb}) } else { c.AbortWithStatusJSON( http.StatusBadRequest, gin.H{ "errors": gin.H{ "modelA": aerr.Error(), "modelB": berr.Error(), }, }, ) } }) r.Run("0:8080") } # modelA can be bound successfully $ curl -X POST -d '{"foo":"AAA","bar":123}' 0:8080/json | jq { "form": { "foo": "AAA", "bar": 123 } } # but modelB cannot $ curl -X POST -d '{"hoge":"BBB","fuga":123}' 0:8080/json | jq { "errors": { "modelA": "Key: 'modelA.Bar' Error:Field validation for 'Bar' failed on the 'req ired' tag\nKey: 'modelA.Foo' Error:Field validation for 'Foo' failed on the 'require ' tag", "modelB": "EOF" } } Proposal to solve thisThis PR solves this by adding a new interface ... // use ShouldBindBodyWith if aerr := c.ShouldBindBodyWith(&fa, binding.JSON); aerr == nil { c.JSON(http.StatusOK, gin.H{"form": fa}) } else if berr := c.ShouldBindBodyWith(&fb, binding.JSON); berr == nil { c.JSON(http.StatusOK, gin.H{"form": fb}) } else { ... # successfully modelB bound $ curl -X POST -d '{"hoge":"BBB","fuga":123}' 0:8080/json | jq { "form": { "hoge": "BBB", "fuga": 123 } } And you can mix different formats. ... if aerr := c.ShouldBindBodyWith(&fa, binding.JSON); aerr == nil { c.JSON(http.StatusOK, gin.H{"form": fa}) } else if berr := c.ShouldBindBodyWith(&fb, binding.XML); berr == nil { c.JSON(http.StatusOK, gin.H{"form": fb}) } else { ... $ curl -X POST -d '<?xml version="1.0" encoding="UTF-8"?>\n<root>\n<fuga>123</fuga>\n<hoge>BBB</hoge>\n</root>' 0:8080/json | jq { "form": { "hoge": "BBB", "fuga": 123 } } I added implementations for JSON, XML, msgpack and protobuf. Other formats, query & forms, are already available to be called multiple times, because they uses parsed Question
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
Codecov Report
@@ Coverage Diff @@ ## master #1341 +/- ## ========================================== - Coverage 98.27% 98.19% -0.09% ========================================== Files 34 34 Lines 1800 1826 +26 ========================================== + Hits 1769 1793 +24 - Misses 25 26 +1 - Partials 6 7 +1
Continue to review full report at Codecov.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@thinkerou Can you also help to review this PR? Thanks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@appleboy OK~ |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| MIMEPlain = binding.MIMEPlain | ||
| MIMEPOSTForm = binding.MIMEPOSTForm | ||
| MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm | ||
| BodyBytesKey = "github.com/gin-gonic/gin/bodyBytes" |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand it's mean, a little surprising.
Ohh, understand, but the value maybe BindBodyBytesKey better?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sure. fixed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I mean: BodyBytesKey = "BindBodyBytesKey", actually I see github.com/gin-gonic/gin/bodyBytes at first feel like import, do you think?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, but it is a bit dangerous. c.Get(key)'s key is only a string and this is easy to conflict with user-defined keys. I meant the full import-pathish strings as a private key.
I grepped sources and found BindKey = "_gin-gonic/gin/bindkey" (here). Following this, I want to use BodyBytesKey = "_gin-gonic/gin/bodybyteskey", how about this?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good, agreed!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hi, @delphinus , the PR not need to add |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // ShouldBindWith for better performance if you need to call only once. | ||
| func (c *Context) ShouldBindBodyWith( | ||
| obj interface{}, bb binding.BindingBody, | ||
| ) (err error) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe not need named return.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Without this, I should add an explicit declaration var err error because this is needed for reusing body in L527.
// if you use unnamed return value... func (...) ShouldBindBodyWith(...) error { ... if body == nil { // you need explicit declaration here. var err error body, err = ioutil.ReadAll(...) if err != nil { return err } ... } return ... }
I want to simplify the code and use this named return variable.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
OK, got it! Thanks~
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request | ||
| // body into the context, and reuse when it is called again. | ||
| // | ||
| // NOTE: This method reads the body before binding. So you should use |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please use two spaces, thanks.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Bind(*http.Request, interface{}) error | ||
| } | ||
|
|
||
| // BindingBody adds BindBody method to Binding. BindBody is similar with Bind, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please use two spaces, thanks.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You mean “one space”, right? mean to replace ...˽Binding.˽˽BindBody˽is... into ...˽Binding.˽BindBody˽is... ?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh....so sorry, one space!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hi, @delphinus @appleboy Except for comments, need to add unit test and README, it's OK then LGTM to me. Thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
No, it is not needed.
I will add the tests and try to confirm this. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
This reverts commit 2c82901.
There was an error while loading. Please reload this page.
|
Please add some testing and example in readme. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
fd12128 to
1cecbd3
Compare
|
Added tests & README. Review again @appleboy 👍 And I wrote gist for https://gist.github.com/delphinus/9db93c9ece3dc0bf093986a8e11c3fb7 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
gin-gonic/gin#1341 Signed-off-by: Toby Yan <[email protected]>
gin-gonic/gin#1341 Signed-off-by: Toby Yan <[email protected]>
gin-gonic/gin#1341 Signed-off-by: Toby Yan <[email protected]>
gin-gonic/gin#1341 Signed-off-by: Toby Yan <[email protected]>
|
out of curiosity, what is the benefit of adding the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Calling bind multiple times
There was an error while loading. Please reload this page.