Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

guonaihong
Copy link
Contributor

@guonaihong guonaihong commented Aug 13, 2019

env GOPATH=`pwd` go run form1.go
  • form1.go
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="
}
*/

* 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
Copy link

codecov bot commented Aug 13, 2019

Codecov Report

Merging #2015 into master will decrease coverage by 0.07%.
The diff coverage is 87.5%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
binding/form_mapping.go 100% <100%> (ø) ⬆️
binding/multipart_form_mapping.go 95.55% <81.81%> (-4.45%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5612cad...24943ac. Read the comment docs.

defer fd.Close()
c, err := ioutil.ReadAll(fd)

if ConstructionFailure {
Copy link
Contributor

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.

Copy link
Contributor Author

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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vkd

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.

Copy link
Contributor

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) {
Copy link
Contributor

@vkd vkd Aug 19, 2019

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.

Copy link
Contributor Author

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])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this one.

Copy link
Contributor Author

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.

@appleboy appleboy added this to the 1.5 milestone Sep 3, 2019
@thinkerou thinkerou modified the milestones: 1.5, 1.6 Oct 31, 2019
@thinkerou thinkerou modified the milestones: 1.6, 1.7 Feb 28, 2020
@thinkerou thinkerou modified the milestones: 1.7, v1.8 Nov 8, 2020
@thinkerou thinkerou modified the milestones: v1.8, v1.9 May 28, 2022
@thinkerou thinkerou modified the milestones: v1.9, v1.10 Jan 17, 2023
@appleboy
Copy link
Member

Please fix the conflicts and move to 1.11

@appleboy appleboy modified the milestones: v1.10, v1.11 Mar 21, 2024
@guonaihong
Copy link
Contributor Author

Okay, I'll set aside time on Saturday to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants