Commit Graph

161 Commits

Author SHA1 Message Date
thinkerou
d5f12ac6d7
use http method constant (#2155)
* use http method constant

* fix typo
2019-11-29 07:50:49 +08:00
Ngalim Siregar
231ff00d1f Refactor redirect request in gin.go (#1970)
* 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 (#2149)
* upgrade go-validator to v10

* fix fmt
2019-11-25 14:49:45 +08:00
Dmitry Kutakov
db9174ae0c fix ignore walking on form mapping (#1942) (#1943) 2019-11-01 10:47:40 +08:00
Dmitry Kutakov
393a63f3b0 Fix 'errcheck' linter warnings (#2093) 2019-10-27 13:58:59 +08:00
ZhangYunHao
8a1bfcfd3b format errUnknownType (#2103) 2019-10-26 14:20:35 +08:00
Dmitry Kutakov
f7becac7bc Relocate binding body tests (#2086)
* 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() (#2028) 2019-09-06 13:56:59 +08:00
thinkerou
1acb3fb30a
upgrade validator version to v9 (#1015)
* 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 (#1980)
* 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 #1956 (#1957)
* support bind http header param #1956

update #1956
```
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 (#1878) (#1949)
* binding: add support of multipart multi files (#1878)

* update readme: add multipart file binding
2019-06-18 19:49:10 +08:00
Kirill Motkov
b1d607a899 Some code improvements (#1909)
* 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 (#1893)
* 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 (#1842)
* 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 (#1829)
* 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 (#1264)
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 (#1797)
* 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 (#1791) 2019-03-05 06:46:18 +08:00
Dmitry Kutakov
805b2d4904 add support time.Duration on mapping (#1794) 2019-03-04 11:37:46 +08:00
Dmitry Kutakov
0d50ce8597 refactor(form_mapping.go): mapping ptr, struct and map (#1749)
* refactor(form_mapping.go): mapping ptr, struct and map

* fix #1672 correct work with ptr - not create value if field is not set
* avoid allocations on strings.Split() - change to strings.Index()
* fix #610 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 (#1733) 2019-02-22 12:23:52 +08:00
Dmitry Kutakov
b056a34bdc fix errcheck warnings (#1739) 2019-01-18 09:32:53 +08:00
Dmitry Kutakov
49e4b0c60c fix mapping inner structs with correct tag (#1718) 2018-12-28 09:57:09 +08:00
thinkerou
f463d847c2
chore: fix test fail (#1669)
* chore: fix test fail

* fix binduri test fail
2018-12-05 05:58:35 +08:00
mllu
64457fbca7 handle nil body for JSON binding (#1638) 2018-11-22 09:55:51 +08:00
thinkerou
521d06c81d support bind uri param (#1612)
* support bind uri (1)

* uri binding successful run

* fix vet warning: github.com/gin-gonic/gin/internal.Param composite literal uses unkeyed fields

* fix code style

* update function name

* fix test function signature

* add test for CanSet

* update readme and add test case

* remove internal.Params

* add coverage

* fix warning
2018-11-22 09:29:48 +08:00
Gordon Tyler
8cb390f8fe Yaml binding (#1618)
* Add YAML binding for application/x-yaml.

* Add YAML binding methods to Context.

* Review fixes.

* Revert accidentally removed import.
2018-11-06 09:49:45 +08:00
Barnabus
7a374f9a47 Fix typos (#1626) 2018-11-05 14:17:04 +08:00
田欧
72db8acd99
add internal package which includes json package (#1504) 2018-08-30 19:04:03 +08:00
田欧
85f3e78abc chore: remove else instead of return/continue (#1502)
As[ Effective Go](https://golang.org/doc/effective_go.html?#if) about `if` said, remove else statement instead of return/continue statement.
2018-08-20 21:49:24 +08:00
Alexander Lokhman
7eb0f74b89 Set default time format in form binding (#1487) 2018-08-17 09:41:56 +08:00
田欧
6159213462 unify test data (#1417)
mkdir a test data dir.
2018-08-12 23:38:31 +08:00
田欧
cdd02fa9d6 update error(err) to err (#1416)
the pull request update `return error(err)` to `return err`, and remove `kindOfData`.
2018-07-01 21:10:48 +08:00
chainhelen
5636afe02d fix bug, return err when failed binding bool (#1350)
* fix bug, return err when failed binding bool

* add test, return err when failed binding bool
2018-05-11 22:40:33 +08:00
JINNOUCHI Yasushi
995fa8e9ce Fix #216: Enable to call binding multiple times in some formats (#1341)
* Add interface to read body bytes in binding

* Add BindingBody implementation for some binding

* Fix to use `BindBodyBytesKey` for key

* Revert "Fix to use `BindBodyBytesKey` for key"

This reverts commit 2c82901ceab6ae53730a3cfcd9839bee11a08f13.

* Use private-like key for body bytes

* Add tests for BindingBody & ShouldBindBodyWith

* Add note for README

* Remove redundant space between sentences
2018-05-11 10:33:33 +08:00
田欧
bd4f73af67 support struct pointer (#1342)
* support struct pointer

* add readme
2018-05-01 14:24:18 +08:00
Alexander Lokhman
2282be059b Add support of pointers in form binding (#1336)
* Add support of pointers in form binding

* Add tests for pointer form binding
2018-04-26 22:09:34 +08:00
田欧
41f951e0cd support default value for form (#1138)
* support default value for form

* fix bug for nil interface

* use SplitN and optimization code

* add test case

* add test cases for form(own default value)

* fix invalid code

* fix code indent

* assert order
2018-04-25 16:24:03 +08:00
田欧
dfe37ea6f1 unify assert.Equal usage (#1327)
* unify assert.Equal usage

* fix typo
2018-04-20 10:27:44 +08:00
Suhas Karanth
6d913fc343 fix(binding): Expose validator engine used by the default Validator (#1277)
* fix(binding): Expose validator engine used by the default Validator

- Add func ValidatorEngine for returning the underlying validator engine used
  in the default StructValidator implementation.
- Remove the function RegisterValidation from the StructValidator interface
  which made it immpossible to use a StructValidator implementation without the
  validator.v8 library.
- Update and rename test for registering validation
  Test{RegisterValidation => ValidatorEngine}.
- Update readme and example for registering custom validation.
- Add example for registering struct level validation.
- Add documentation for the following binding funcs/types:
  - Binding interface
  - StructValidator interface
  - Validator instance
  - Binding implementations
  - Default func

* fix(binding): Move validator engine getter inside interface

* docs: rm date cmd from custom validation demo
2018-03-29 14:33:07 +08:00
田欧
783c7ee9c1 Add some test cases and run test cases on binding/render dir (#1168)
* Travis run test cases on binding and render dir and add some test cases for binding and render
2018-01-26 11:46:11 +08:00
Boris Borshevsky
6f94fd05c9 Linting and optimizing struct memory signature. (#1184)
* fix cleanPath spell (#969)

* linter and optimize structs
2017-11-29 10:50:14 +08:00
田欧
b7e8a6b9b0 style(import): not use aliase when import package (#1146) 2017-10-29 13:12:22 +01:00
delphinus
a8c53949e5 Support time location on form binding (#1117) 2017-09-28 22:23:18 +08:00
George Kirilenko
cdf26f994b 32 << 10 != 32 Mb (#1094) 2017-09-04 09:15:50 +08:00
Suhas Karanth
26c3f42095 feat(binding): add support for custom validator / validation tags (#1068)
* feat(binding): Add support for custom validation tags

* docs: Add example for custom validation tag

* test(binding): Add test for registering custom validation
2017-08-27 09:37:39 +02:00
Eason Lin
c19aa0598b feat(context): add BindQuery func (#1029)
* feat(context): add BindQuery func, only parse/bind the query string params.

* docs(readme): add BindQuery section.

* docs(readme): fix import.

* docs(readme): separate import
2017-07-19 09:50:05 +02:00
Bo-Yi Wu
ce670a6497 refactor(json): make jsonite optional with build tags (#1026)
* refactor(json): Restore gin support for app engine

Create new folder to support multiple json package.
restore gin support for app engine (disable jsonite through tags)

use jsoniter

$ go build -tags=jsoniter .

use default json

$ go build .

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* rename json file.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* docs(json): add build tags document.

* fix(docs): markdown format.

* fix(json): missing space.
2017-07-18 23:01:29 +02:00
Eason Lin
8f861946b0 style(msgpack): remove redundant comments (#1027) 2017-07-17 05:57:59 -05:00
whirosan
fb7448f081 feat(binding): add UseNumber() in gin.Context.BindJSON() (#997) close #368
* resolve #368 add option to UseNumber() in gin.Context.BindJSON()

* add test
2017-07-10 10:33:35 +02:00
Bo-Yi Wu
e23842ecab
fix json sort the map keys
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-07-08 18:19:09 +08:00
Bo-Yi Wu
12508320c2
feat: change json lib to jsoniter
A high-performance 100% compatible drop-in replacement of "encoding/json"

https://github.com/json-iterator/go

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-07-08 16:51:36 +08:00
Bo-Yi Wu
1e1e4fc867 Add Makefile to check the following thing (#947)
* Add Makefile to check the following thing.

* vet check
* fmt check
* embedmd check
* misspell check

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* remove unused variable.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2017-06-12 21:50:42 -05:00
田欧
4a2b55037f delete else keyword (#945) 2017-06-12 21:36:37 -05:00
田欧
4ad3baf44e Add license for some files (#940) 2017-06-12 01:04:52 -05:00
田欧
d922143bc5 lint code for import (#939) 2017-06-11 21:40:15 -05:00
Andrey Nering
6a3a8ae61b Fix time.Time binding (#904)
If a empty string is given(`""`), them time should be zero.
2017-05-04 09:22:00 +08:00
Harindu Perera
5be2123c1a Added support for MessagePack binding and rendering (#808)
Added deps to vendor.json and fixed rendering bug
2017-02-23 22:08:37 +08:00
Andrey Nering
863248034b Support time.Time on form binding (#801) 2017-02-17 21:32:36 +08:00
Javier Provecho Fernandez
9e930b9bdd lint code 2016-04-15 01:16:46 +02:00
Javier Provecho Fernandez
a6ce7dd84a Merge branch 'bind_test' of https://github.com/honteng/gin into honteng-bind_test 2016-02-24 19:20:20 +01:00
Manu Mtz.-Almeida
ffb5c0412a Updates Validator + unit tests 2016-01-26 19:28:26 +01:00
Naoki Takano
66c4b81579 Added exits check tests for binding 2015-12-30 22:49:38 -08:00
Javier Provecho Fernandez
e5339a3f4d Merge branch 'new-binding-validator' of https://github.com/zhing/gin into zhing-new-binding-validator
Conflicts:
	binding/binding.go
	binding/binding_test.go
2015-10-02 10:42:58 +02:00
zhing
04917e8307 correct the mime type to x-protobuf 2015-07-18 15:18:01 +08:00
zhing
9d644d22e0 add protobuf binding for gin 2015-07-12 17:42:39 +08:00
Manu Mtz-Almeida
0494e1b66a Removes unused underscore 2015-07-10 13:06:01 +02:00
Manu Mtz-Almeida
0873992f38 More unit tests for form binding 2015-07-08 04:26:37 +02:00
Manu Mtz-Almeida
4194adce4c Adds additional bindings for multipart and form 2015-07-03 04:20:00 +02:00
Manu Mtz-Almeida
0c9f086b74 Renames Validate() to validate() 2015-05-31 16:30:00 +02:00
Manu Mtz-Almeida
fecde9fed6 Refactors binding validation 2015-05-31 16:18:50 +02:00
Manu Mtz-Almeida
9584e4ea5c Fix for #310 2015-05-29 20:34:41 +02:00
Manu Mtz-Almeida
ec1ce34d32 Merge branch 'multipart-form-data-fix' of https://github.com/konjoot/gin
- the merge was manually modified before committing.
2015-05-26 16:47:10 +02:00
Manu Mtz-Almeida
af8e099dfd Fixes multipart integration 2015-05-26 16:31:05 +02:00
Maksimov Sergey
e46f4980b9 Restored support of multipart/form-data 2015-05-26 15:21:35 +03:00
Manu Mtz-Almeida
967e62337a form mapping optimisation 2015-05-24 03:56:11 +02:00
Manu Mtz-Almeida
208e1f5569 Merge branch 'master' of https://github.com/remerge/gin
Conflicts:
	binding/binding.go
2015-05-24 03:33:21 +02:00
Manu Mtz-Almeida
8549810e2e Using "validator.v5" 2015-05-22 16:46:21 +02:00
Manu Mtz-Almeida
f414648384 - More unit tests
- Improves HTML debug render
- InputHolder removed
- More debug logs
2015-05-05 15:06:38 +02:00
Manu Mtz-Almeida
0a192fb0fa Tons of unit tests 2015-04-09 12:15:02 +02:00
Manu Mtz-Almeida
8b26264574 Merge branch 'develop' into performance
Conflicts:
	context.go
	context_test.go
	gin_test.go
	recovery_test.go
	utils.go
2015-04-08 13:37:25 +02:00
Manu Mtz-Almeida
ac0ad2fed8 Improves unit tests 2015-04-08 02:58:35 +02:00
Manu Mtz-Almeida
a4eadceb45 Merge branch 'develop' into performance
Conflicts:
	binding/form_mapping.go
	context_test.go
2015-04-07 19:59:43 +02:00
Manu Mtz-Almeida
9828435f70 Fixes failing unit test 2015-04-07 18:14:33 +02:00
Manu Mtz-Almeida
a887e395f3 Fixes integration with "go-validate-yourself"
http://stackoverflow.com/questions/29138591/hiding-nil-values-understanding-why-golang-fails-here
2015-04-07 16:06:53 +02:00
Manu Mtz-Almeida
ee3b67eda1 Experimenting with new validation library!!! 2015-04-07 12:30:16 +02:00
Manu Mtz-Almeida
1f6304ca25 Cleaning up performance branch 2015-04-07 12:22:38 +02:00
Manu Mtz-Almeida
d4413b6e91 Refactors binding module 2015-03-31 17:51:10 +02:00
Martin Karlsch
0569c5fb95 add support for embedded struct to Bind 2015-03-27 08:31:27 -07:00
Manu Mtz-Almeida
3e3ced70d4 Using log.Panic instead 2015-03-23 05:50:10 +01:00
Javier Provecho Fernandez
51599d3c0a Merge branch 'develop' of github.com:gin-gonic/gin into develop 2015-03-08 15:53:06 +01:00
Aleksandr Didenko
0fb7bed1c0 Added support multipart/form-data #109 2015-03-08 15:43:37 +01:00
Javier Provecho Fernandez
1333a65081 Merge pull request #217 from ethankan/develop
Added support for unsigned integers in binding parameters of form posts....
2015-03-08 14:36:21 +01:00
Javier Provecho Fernandez
0f46ae2b81 Merge pull request #224 from zazab/bind-check-sub-structs
Add validating sub structures
2015-03-08 14:33:22 +01:00
Evgeny Persienko
b537c5d15e Add slice elements check for not required slice 2015-02-20 14:33:50 +06:00
Evgeny Persienko
f145e435c7 Add validating sub structures 2015-02-20 11:46:24 +06:00
Ethan Kan
70f280f880 Added support for unsigned integers in binding parameters of form posts. Also changed parsing of integer fields to take into account the size of the fields. 2015-02-09 15:13:05 -08:00
Javier Provecho Fernandez
5dad47bca2 Merge pull request #188 from gin-gonic/fix-binding
Fix unexported field detection
2015-02-07 14:38:40 +01:00
Javier Provecho Fernandez
d936320e0e Sync master into develop
- Add 265faff4ba
- Update "github.com/julienschmidt/httprouter" version in Godeps
- Add 28b9ff9e34
2015-02-04 12:56:19 +01:00