-
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
binding: support []byte #2015
base: master
Are you sure you want to change the base?
binding: support []byte #2015
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package binding | |
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"mime/multipart" | ||
"net/http" | ||
"reflect" | ||
|
@@ -14,6 +15,7 @@ import ( | |
type multipartRequest http.Request | ||
|
||
var _ setter = (*multipartRequest)(nil) | ||
var ConstructionFailure = false | ||
|
||
// TrySet tries to set a value by the multipart request with the binding a form file | ||
func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSetted bool, err error) { | ||
|
@@ -39,6 +41,27 @@ func setByMultipartFormFile(value reflect.Value, field reflect.StructField, file | |
return true, nil | ||
} | ||
case reflect.Slice: | ||
switch value.Interface().(type) { | ||
case []byte: | ||
fd, err := files[0].Open() | ||
if err != nil { | ||
return false, err | ||
} | ||
defer fd.Close() | ||
c, err := ioutil.ReadAll(fd) | ||
|
||
if ConstructionFailure { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, it is a bad practice when the code depends on the implementation of the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok,I will find a way. @vkd There are other ways to bypass the cover detection ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/golang/go/blob/master/src/context/context.go#L243 Add some test code to the official code, someone has done this, such as rsc, in the standard library. So, I don't think this piece needs to be modified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In your example, there is the different case, because the behavior of a method does not change. |
||
err = errors.New("test use") | ||
} | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
value.Set(reflect.ValueOf(c)) | ||
return true, nil | ||
} | ||
|
||
slice := reflect.MakeSlice(value.Type(), len(files), len(files)) | ||
isSetted, err = setArrayOfMultipartFormFiles(slice, field, files) | ||
if err != nil || !isSetted { | ||
|
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 think better approach is to do it like in go default json.Decode. For reason to save the same behaviour.
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.
@vkd Is this code?
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.
Yes, this one.
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.
This code may not be used. In json, []byte data is encoded by base64, so it needs to be decoded by base64.StdEncoding.Decode function. But http is not encoded in base64.