-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #216: Enable to call binding multiple times in some formats #1341
Fix #216: Enable to call binding multiple times in some formats #1341
Conversation
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.
|
@thinkerou Can you also help to review this PR? Thanks. |
@appleboy OK~ |
context.go
Outdated
@@ -31,6 +31,7 @@ const ( | |||
MIMEPlain = binding.MIMEPlain | |||
MIMEPOSTForm = binding.MIMEPOSTForm | |||
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm | |||
BodyBytesKey = "github.com/gin-gonic/gin/bodyBytes" |
There was a problem hiding this comment.
Choose a reason for 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. fixed.
There was a problem hiding this comment.
Choose a reason for 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?
There was a problem hiding this comment.
Choose a reason for 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good, agreed!
Hi, @delphinus , the PR not need to add |
context.go
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not need named return.
There was a problem hiding this comment.
Choose a reason for 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, got it! Thanks~
context.go
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use two spaces, thanks.
binding/binding.go
Outdated
@@ -29,6 +29,13 @@ type Binding interface { | |||
Bind(*http.Request, interface{}) error | |||
} | |||
|
|||
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use two spaces, thanks.
There was a problem hiding this comment.
Choose a reason for 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...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh....so sorry, one space!
Hi, @delphinus @appleboy Except for comments, need to add unit test and README, it's OK then LGTM to me. Thanks! |
No, it is not needed.
I will add the tests and try to confirm this. |
This reverts commit 2c82901.
Please add some testing and example in readme. |
fd12128
to
1cecbd3
Compare
Added tests & README. Review again @appleboy 👍 And I wrote gist for https://gist.github.com/delphinus/9db93c9ece3dc0bf093986a8e11c3fb7 |
gin-gonic/gin#1341 Signed-off-by: Toby Yan <me@tobyan.com>
gin-gonic/gin#1341 Signed-off-by: Toby Yan <me@tobyan.com>
gin-gonic/gin#1341 Signed-off-by: Toby Yan <me@tobyan.com>
gin-gonic/gin#1341 Signed-off-by: Toby Yan <me@tobyan.com>
out of curiosity, what is the benefit of adding the |
close #216
Reported problems
#216 says some formats in requests' body cannot be bound into different structs by calling multiple times.
Proposal to solve this
This PR solves this by adding a new interface
binding.BindingBody
& a new methodc.ShouldBindBodyWith
.And you can mix different formats.
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
req.Form (url.Values)
instead ofreq.Body
.Question
bind.BindingBody
,c.ShouldBindBodyWith
. I'm happy if you give better names.