-
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
Conversation
* form1.go ```golang package main import ( "fmt" "github.com/gin-gonic/gin" ) type testForm struct { Mode string `form:"mode"` Text string `form:"text"` Voice []byte `form:"voice"` Voice2 []byte `form:"voice2"` } func main() { router := gin.Default() router.POST("/test.form", func(c *gin.Context) { t2 := testForm{} if err := c.ShouldBind(&t2); err != nil { fmt.Printf("err = %s:%v\n", err, t2) c.JSON(500, gin.H{"err": err.Error()}) return } c.JSON(200, t2) }) router.Run() } //client /* curl -F mode=A -F text="test" -F voice=@form1.go -F voice2="voice" 127.0.0.1:8080/test.form|jq { "Mode": "A", "Text": "test", "Voice": "cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJnaXRodWIuY29tL2dpbi1nb25pYy9naW4iCikKCnR5cGUgdGVzdEZvcm0gc3RydWN0IHsKCU1vZGUgICBzdHJpbmcgYGZvcm06Im1vZGUiYAoJVGV4dCAgIHN0cmluZyBgZm9ybToidGV4dCJgCglWb2ljZSAgW11ieXRlIGBmb3JtOiJ2b2ljZSJgCglWb2ljZTIgW11ieXRlIGBmb3JtOiJ2b2ljZTIiYAp9CgpmdW5jIG1haW4oKSB7Cglyb3V0ZXIgOj0gZ2luLkRlZmF1bHQoKQoJcm91dGVyLlBPU1QoIi90ZXN0LmZvcm0iLCBmdW5jKGMgKmdpbi5Db250ZXh0KSB7CgoJCXQyIDo9IHRlc3RGb3Jte30KCQlpZiBlcnIgOj0gYy5TaG91bGRCaW5kKCZ0Mik7IGVyciAhPSBuaWwgewoJCQlmbXQuUHJpbnRmKCJlcnIgPSAlczoldlxuIiwgZXJyLCB0MikKCQkJYy5KU09OKDUwMCwgZ2luLkh7ImVyciI6IGVyci5FcnJvcigpfSkKCQkJcmV0dXJuCgkJfQoJCWMuSlNPTigyMDAsIHQyKQoJfSkKCglyb3V0ZXIuUnVuKCkKfQo=", "Voice2": "dm9pY2U=" } */ ```
Codecov Report
@@ Coverage Diff @@
## master #2015 +/- ##
==========================================
- Coverage 98.69% 98.61% -0.08%
==========================================
Files 40 40
Lines 2225 2241 +16
==========================================
+ Hits 2196 2210 +14
- Misses 16 17 +1
- Partials 13 14 +1
Continue to review full report at Codecov.
|
defer fd.Close() | ||
c, err := ioutil.ReadAll(fd) | ||
|
||
if ConstructionFailure { |
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.
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 comment
The 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 comment
The 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 comment
The 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.
@@ -146,6 +146,15 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][ | |||
if !ok { | |||
vs = []string{opt.defaultValue} | |||
} | |||
|
|||
switch value.Interface().(type) { |
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?
case reflect.Slice:
if v.Type().Elem().Kind() != reflect.Uint8 {
d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
break
}
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
n, err := base64.StdEncoding.Decode(b, s)
if err != nil {
d.saveError(err)
break
}
v.SetBytes(b[:n])
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.
Please fix the conflicts and move to 1.11 |
Okay, I'll set aside time on Saturday to fix it. |
env GOPATH=`pwd` go run form1.go