Commit Graph

138 Commits

Author SHA1 Message Date
thinkerou 38eb5acc6b
add v1.8.0 changelog () 2022-05-30 15:16:10 +08:00
thinkerou 4b68a5f12a
chore: update go.mod and remove space from copyright ()
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-05-28 10:42:28 +08:00
Valentine Oragbakosi ed03102ef0
[GIN-001] - Add TOML bining for gin ()
Co-authored-by: GitstartHQ <gitstart@users.noreply.github.com>
2022-05-28 08:34:43 +08:00
Kacper Bąk c706ace929
fix: removed YODA conditions, removed blank identifier from `invalid_obj` ()
* fix: removed YODA conditions, unnecessary binding.binding

* fix: remove BindingBody change
2022-04-23 18:01:03 +08:00
thinkerou 2bde107686
test support go1.18 () 2022-03-21 09:43:17 +08:00
jincheng9 8a0f95cc71
fix: typo () 2021-12-20 17:42:54 +08:00
edebernis 57ede9c95a
Export struct sliceValidateError to allow error casting and rename it as ()
SliceValidationError

Co-authored-by: Emeric de Bernis <emeric.debernis@adevinta.com>
2021-11-21 21:45:03 +08:00
axiaoxin 1a2bc0e7cb
setted typo fix: There’s no such word as `setted`, `set` is set, set, setting () 2021-09-30 10:04:28 +08:00
Matthieu MOREL 71f7087097
golangci(lint) : more linters () 2021-09-21 15:22:21 +08:00
Tevic eab47b5423
fix: check obj type in protobufBinding ()
* fix: check obj type in protobufBinding

* fix: UnitTest for invalid proto obj
2021-09-07 10:10:32 +08:00
Alexander Melentyev 527d950252
Delete unused arg () 2021-08-21 14:59:17 +08:00
Matthieu MOREL 435a76b735
chore(ci): update dependencies ()
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Matthieu MOREL <mmorel-35@users.noreply.github.com>
2021-08-19 15:46:31 +08:00
Helios 9d2883ef47
update the version of validator in the comment ()
Co-authored-by: shangyilong <shangyilong@zuoyebang.com>
2021-07-06 16:36:32 +08:00
voidman 690aa2b1b9
feat(binding): support custom struct tag ()
* feat(binding): support custom struct tag

Add function `binding.MapFormWithTag` ()

* doc: add 'bind form-data with custom struct tag'

Add 'Bind form-data request with custom struct and custom tag' section ()

* test(binding): add test for MapFromWithTag
2021-06-30 00:53:56 +08:00
ziheng e3ee01d185
improve sliceValidateError.Error performance using switch and strings.Builder ()
fix missing nil pointer check

use simpler switch case

add missing tests

use for-loop instead of range

add benchmark test codes
2021-06-28 22:05:29 +08:00
tyltr 34ce2104ca
optimize code and reduce code cyclomatic complexity ()
* optimize code and reduce code cyclomatic complexity

* optimize if-condtion

Co-authored-by: thinkerou <thinkerou@gmail.com>
2021-06-03 20:12:51 +08:00
heige 97a32b1de3
Optimize code adjust ()
* setFormMap error of result

* adjust code for TrySet

* error export for type multipart.FileHeader

* code style adjust

* reflect code maping optimize

* Update form_mapping.go

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: thinkerou <thinkerou@gmail.com>
2021-06-02 07:35:30 +08:00
iamhesir 0cbb30aa94
Update default validator's docs link ()
The default validator has upgraded from v8 to v10, but its docs link didn't.
2021-05-26 18:46:13 +08:00
y-yagi 168edcad80
Check multipart file header size on test ()
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: thinkerou <thinkerou@gmail.com>
2021-05-23 11:44:41 +08:00
Qt 215c9ce231
use errors.New to replace fmt.Errorf will much better () 2021-04-28 18:39:09 +08:00
thinkerou ed6f85c478
build: convert to go:build directives () 2021-03-27 09:09:44 +08:00
wuhuizuo 4bfae4c8c8
Support binding for slice/array obj [Rewrite] ()
Co-authored-by: thinkerou <thinkerou@gmail.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2021-01-03 21:43:34 +08:00
Alessandro (Ale) Segala 65ed60ed13
Allow bind with a map[string]string ()
Co-authored-by: thinkerou <thinkerou@gmail.com>
2020-10-31 07:20:47 +08:00
lantw44 30b5f7e2d7
binding: avoid 2038 problem on 32-bit architectures ()
Function setTimeField calls strconv.ParseInt with bit size 0 when
parsing Unix time, which means it is equivalent to specifying 32 on
32-bit architectures. This causes the function to suffer from the year
2038 problem. To fix it and keep the behavior the same on both 32-bit
and 64-bit architectures, explicitly specify bit size 64.

Co-authored-by: thinkerou <thinkerou@gmail.com>
2020-08-08 17:31:08 +08:00
John Bampton cf8b583db4
Fix spelling () 2020-08-04 07:04:06 +08:00
Andy Pan 982daeb1ec Use zero-copy approach to convert types between string and byte… ()
* Use zero-copy approach to convert types between string and byte slice

* Rename argument to a eligible one

Benchmark:

BenchmarkBytesConvBytesToStrRaw-4   	21003800	        70.9 ns/op	      96 B/op	       1 allocs/op
BenchmarkBytesConvBytesToStr-4      	1000000000	         0.333 ns/op	       0 B/op	       0 allocs/op
BenchmarkBytesConvStrToBytesRaw-4   	18478059	        59.3 ns/op	      96 B/op	       1 allocs/op
BenchmarkBytesConvStrToBytes-4      	1000000000	         0.373 ns/op	       0 B/op	       0 allocs/op


Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2020-01-18 00:32:50 +08:00
Antoine GIRARD fd8a65b252 Add build tag nomsgpack ()
* add build tag nomsgpack

* Update copyright

* Update copyright
2020-01-07 11:31:10 +08:00
thinkerou d5f12ac6d7
use http method constant ()
* use http method constant

* fix typo
2019-11-29 07:50:49 +08:00
Ngalim Siregar 231ff00d1f Refactor redirect request in gin.go ()
* Refactor redirect request in gin.go

* Update http status code
2019-11-26 08:19:30 +08:00
Xudong Cai e90e2ba9b3 upgrade go-validator to v10 ()
* upgrade go-validator to v10

* fix fmt
2019-11-25 14:49:45 +08:00
Dmitry Kutakov db9174ae0c fix ignore walking on form mapping () () 2019-11-01 10:47:40 +08:00
Dmitry Kutakov 393a63f3b0 Fix 'errcheck' linter warnings () 2019-10-27 13:58:59 +08:00
ZhangYunHao 8a1bfcfd3b format errUnknownType () 2019-10-26 14:20:35 +08:00
Dmitry Kutakov f7becac7bc Relocate binding body tests ()
* Relocate binding body tests

Every test file should be related to a tested file.
Remove useless tests.

* Add github.com/stretchr/testify/require package
2019-10-10 16:58:31 +08:00
Krzysztof Szafrański f38c30a0d2 feat(binding): add DisallowUnknownFields() in gin.Context.BindJSON() () 2019-09-06 13:56:59 +08:00
thinkerou 1acb3fb30a
upgrade validator version to v9 ()
* upgrade validator version to v9

* Update vendor.json

* Update go.mod

* Update go.sum

* fix

* fix

* fix bug

* Update binding_test.go

* Update validate_test.go

* Update go.sum

* Update go.mod

* Update go.sum

* Update go.mod

* Update go.sum
2019-09-05 21:39:56 +08:00
guonaihong 502c898d75 binding: support unix time ()
* binding: support unix time

ref:#1979

* binding: support unix time

add test file
modify readme

```golang
package main

import (
        "fmt"
        "github.com/gin-gonic/gin"
        "time"
)

type shareTime struct {
        CreateTime time.Time `form:"createTime" time_format:"unixNano"`
        UnixTime   time.Time `form:"unixTime" time_format:"unix"`
}

func main() {
        r := gin.Default()
        unix := r.Group("/unix")

        testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
        fmt.Printf("%d\n", testCT.UnixNano())

        testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
        fmt.Printf("%d\n", testUT.Unix())

        unix.GET("/nano", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testCT.Equal(s.CreateTime) {
                        c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
                        return
                }

                c.JSON(200, s)
        })

        unix.GET("/sec", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testUT.Equal(s.UnixTime) {
                        c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
                        return
                }

                c.JSON(200, s)

        })

        r.Run()
}

```

* Contraction variable scope
2019-07-10 13:02:40 +08:00
guonaihong f98b339b77 support bind http header param ()
* support bind http header param 

update 
```
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

type testHeader struct {
	Rate   int    `header:"Rate"`
	Domain string `header:"Domain"`
}

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		h := testHeader{}

		if err := c.ShouldBindHeader(&h); err != nil {
			c.JSON(200, err)
		}

		fmt.Printf("%#v\n", h)
		c.JSON(200, gin.H{"Rate": h.Rate, "Domain": h.Domain})
	})

	r.Run()

// client
// curl -H "rate:300" -H "domain:music" 127.0.0.1:8080/
// output
// {"Domain":"music","Rate":300}
}
```

* add unit test

* Modify the code to get the http header

When the http header is obtained in the standard library,
the key value will be modified by the CanonicalMIMEHeaderKey function,
and finally the value of the http header will be obtained from the map.
As follows.
```go
func (h MIMEHeader) Get(key string) string {
        // ...
         v := h[CanonicalMIMEHeaderKey(key)]
        // ...
}
```

This pr also follows this modification

* Thanks to vkd for suggestions, modifying code

* Increase test coverage

env GOPATH=`pwd` go test github.com/gin-gonic/gin/binding -coverprofile=cover.prof
ok  	github.com/gin-gonic/gin/binding	0.015s	coverage: 100.0% of statements

* Rollback check code

* add use case to README.md
2019-06-27 12:47:45 +08:00
Dmitry Kutakov 09a3650c97 binding: add support of multipart multi files () ()
* binding: add support of multipart multi files ()

* update readme: add multipart file binding
2019-06-18 19:49:10 +08:00
Kirill Motkov b1d607a899 Some code improvements ()
* strings.ToLower comparison changed to strings.EqualFold.
* Rewrite switch statement with only one case as if.
2019-05-21 23:08:52 +08:00
guonaihong 8ee9d959a0 Now you can parse the inline lowercase start structure ()
* Now you can parse the inline lowercase start structure

package main

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
)

type appkey struct {
	Appkey string `json:"appkey" form:"appkey"`
}

type Query struct {
	Page int `json:"page" form:"page"`
	Size int `json:"size" form:"size"`
	appkey
}

func main() {

	router := gin.Default()
	router.POST("/login", func(c *gin.Context) {

		var q2 Query

		if c.ShouldBindQuery(&q2) == nil {
			c.JSON(200, &q2)
		}
	})
	router.Run(":8088")
}

http client:

old:
curl -X POST "127.0.0.1:8088/login?appkey=china&page=1&size=10"
{"page":1,"size":10,"appkey":""}

now:
curl -X POST "127.0.0.1:8088/login?appkey=china&page=1&size=10"
{"page":1,"size":10,"appkey":"china"}

* Modify judgment conditions
2019-05-13 10:17:31 +08:00
Dmitry Kutakov 66d2c30c54 binding: move tests of mapping to separate test file ()
* move tests of mapping to separate test file

make 100% coverage of form_mapping.go from form_mapping_test.go file

* fix tests for go 1.6

go 1.6 doesn't support `t.Run(...)` subtests
2019-05-07 19:06:55 +08:00
Dmitry Kutakov 2e915f4e50 refactor(form_mapping.go): mapping multipart request ()
* refactor(form_mapping.go): mapping multipart request

* add checkers for a types to match with the setter interface

* form_mapping.go: rename method name on setter interface, add comments

* fix style of comments
2019-04-02 09:01:34 +08:00
Boyi Wu c16bfa7949 update for supporting file binding ()
update for supporting multipart form and file binding 

example:
```
type PhoptUploadForm struct {
	imgData    *multipart.FileHeader `form:"img_data" binding:"required"`
	ProjectID   string `form:"project_id" binding:"required"`
	Description string `form:"description binding:"required"`
}
```


ref: https://github.com/gin-gonic/gin/issues/1263
2019-03-18 10:16:34 +08:00
Dmitry Kutakov 483f828bce add support arrays on mapping ()
* add support arrays on mapping

* not allow default value on array mapping
2019-03-14 13:34:56 +08:00
田欧 a5dda62cdc
chore: use internal/json () 2019-03-05 06:46:18 +08:00
Dmitry Kutakov 805b2d4904 add support time.Duration on mapping () 2019-03-04 11:37:46 +08:00
Dmitry Kutakov 0d50ce8597 refactor(form_mapping.go): mapping ptr, struct and map ()
* refactor(form_mapping.go): mapping ptr, struct and map

* fix  correct work with ptr - not create value if field is not set
* avoid allocations on strings.Split() - change to strings.Index()
* fix  tag value "-" is mean ignoring field
* struct fields mapped like json.Unmarshal
* map fields mapped like json.Unmarshal

* fix after @thinkerou review
2019-03-03 14:39:43 +08:00
André Bazaglia 48f6c6137c allow ignoring field on form mapping () 2019-02-22 12:23:52 +08:00
Dmitry Kutakov b056a34bdc fix errcheck warnings () 2019-01-18 09:32:53 +08:00