- 
                Notifications
    
You must be signed in to change notification settings  - Fork 8.5k
 
binding:fix empty value error #2169
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
base: master
Are you sure you want to change the base?
Conversation
Here is the code that can report an error
```go
package main
import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
	"time"
)
type header struct {
	Duration   time.Duration `header:"duration"`
	CreateTime time.Time     `header:"createTime" time_format:"unix"`
}
func needFix1() {
	g := gin.Default()
	g.GET("/", func(c *gin.Context) {
		h := header{}
		err := c.ShouldBindHeader(&h)
		if err != nil {
			c.JSON(500, fmt.Sprintf("fail:%s\n", err))
			return
		}
		c.JSON(200, h)
	})
	g.Run(":8081")
}
func needFix2() {
	g := gin.Default()
	g.GET("/", func(c *gin.Context) {
		h := header{}
		err := c.ShouldBindHeader(&h)
		if err != nil {
			c.JSON(500, fmt.Sprintf("fail:%s\n", err))
			return
		}
		c.JSON(200, h)
	})
	g.Run(":8082")
}
func sendNeedFix1() {
	// send to needFix1
	sendBadData("http://127.0.0.1:8081", "duration")
}
func sendNeedFix2() {
	// send to needFix2
	sendBadData("http://127.0.0.1:8082", "createTime")
}
func sendBadData(url, key string) {
	req, err := http.NewRequest("GET", "http://127.0.0.1:8081", nil)
	if err != nil {
		fmt.Printf("err:%s\n", err)
		return
	}
	// Only the key and no value can cause an error
	req.Header.Add(key, "")
	rsp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}
	io.Copy(os.Stdout, rsp.Body)
	rsp.Body.Close()
}
func main() {
	go needFix1()
	go needFix2()
	time.Sleep(time.Second / 1000 * 200) // 200ms
	sendNeedFix1()
	sendNeedFix2()
}
```
    
          Codecov Report
 @@           Coverage Diff           @@
##           master    #2169   +/-   ##
=======================================
  Coverage   98.47%   98.47%           
=======================================
  Files          41       41           
  Lines        2298     2302    +4     
=======================================
+ Hits         2263     2267    +4     
  Misses         20       20           
  Partials       15       15           
 Continue to review full report at Codecov. 
  | 
    
| 
           @vkd @thinkerou Any feedbacks?  | 
    
| 
               | 
          ||
| // ok | ||
| tests := []bindTestData{ | ||
| {need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: formSource{"createTime": []string{" "}}}, | 
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.
The non-empty string does not look to me as a default empty value. For me it looks like a wrong value.
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.
The strconv.ParseInt function in Go will report an error when it encounters data beginning with a space. The function of removing spaces is added so that the function does not report an error.
package main
import (
	"fmt"
	"strconv"
)
func main() {
	fmt.Println(strconv.ParseInt("  5", 10, 0))
	fmt.Println(strconv.ParseInt("5", 10, 0))
}
/*
0 strconv.ParseInt: parsing "  5": invalid syntax
5 <nil>
*/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, and from my point of view the binding shouldn't do this.
| } | ||
| 
               | 
          ||
| func setWithProperType(val string, value reflect.Value, field reflect.StructField) error { | ||
| if value.Kind() != reflect.String { | 
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 change is not explained by the PR description.
Please update PR description with explanation what cases have you fixed and why.
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.
If it is a string type, no spaces are removed, and the user data is not modified here.
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 document your changes in the PR's description. Just add information about what is changed and why.
| 
           Vote for this pr dispute. @appleboy @vkd @thinkerou package main
import (
	"fmt"
	"strconv"
)
func main() {
	fmt.Println(strconv.ParseInt("  5", 10, 0))
	fmt.Println(strconv.ParseInt("5", 10, 0))
}
/*
0 strconv.ParseInt: parsing "  5": invalid syntax
5 <nil>
*/strings.TrimSpace + 1  | 
    
| 
           By my perspective, adding  strings.TrimSpace - 1  | 
    
| func setWithProperType(val string, value reflect.Value, field reflect.StructField) error { | ||
| // If it is a string type, no spaces are removed, and the user data is not modified here | ||
| if value.Kind() != reflect.String { | ||
| val = strings.TrimSpace(val) | 
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.
If val is not string type, can call func TrimSpace(s string) string?
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.
The type of val is string, as explained below.
If the client command is the following code
curl -H 'intkey: v1" -H "strkey: v2" url 
We define the following structure to parse the http header
type needHeader struct {
    IntKey int `header:"intkey"
    StrKey string `header:"strkey"
}Such a data structure.
Before setting into the structure, regardless of the int or string required by the structure, it is of type string before being parsed.
| 
           @thinkerou move to 1.7 milestones?  | 
    
          
 OK, thanks!  | 
    
Here is the code that can report an error