Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,53 @@ Test it with:
$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15&createTime=1562400033000000123&unixTime=1562400033"
```

### Bind Query String or Post Data and assign default value(s)

```go
package main

import (
"log"
"time"

"github.com/gin-gonic/gin"
)

type Workplace struct {
Employee string `form:"employee"`
Location string `form:"address,default=headquarter"`
Peripherals []string `form:"peripherals,multiple_default,default=monitor,mouse,keyboard`

}

func main() {
route := gin.Default()
route.GET("/testing", startPage)
route.Run(":8085")
}

func startPage(c *gin.Context) {
var workplace Workplace
// If `GET`, only `Form` binding engine (`query`) used.
// If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).
// See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L48
// If default value defined, and the Request does not contain the key, the default value will be used. Empty value from request overrides that behaviour
// If multiple_default is in front of default, the 'default' value is returned as a slice, the position of multiple_default is important
if c.ShouldBind(&workplace) == nil {
log.Println(workplace.Employee)
log.Println(workplace.Location)
log.Println(workplace.Peripherals)
}

c.String(200, "Success")
}
```

Test it with:
```sh
$ curl -X GET "localhost:8085/testing?employee=andre"
```

### Bind Uri

See the [detail information](https://github.com/gin-gonic/gin/issues/846).
Expand Down
10 changes: 8 additions & 2 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter
var opt string
for len(opts) > 0 {
opt, opts = head(opts, ",")
if k, _ := head(opt, "="); k == "multiple_default" {
_, values := head(opts, "=")
setOpt.isDefaultExists = true
setOpt.defaultValue = values
break
}

if k, v := head(opt, "="); k == "default" {
setOpt.isDefaultExists = true
Expand All @@ -161,12 +167,12 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
switch value.Kind() {
case reflect.Slice:
if !ok {
vs = []string{opt.defaultValue}
vs = strings.Split(opt.defaultValue, ",")
}
return true, setSlice(vs, value, field)
case reflect.Array:
if !ok {
vs = []string{opt.defaultValue}
vs = strings.Split(opt.defaultValue, ",")
}
if len(vs) != value.Len() {
return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())
Expand Down
14 changes: 14 additions & 0 deletions binding/form_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ func TestMappingDefault(t *testing.T) {
assert.Equal(t, [1]int{9}, s.Array)
}

func TestMappingMultipleDefault(t *testing.T) {
var s struct {
Int int `form:",multiple_default,default=9"`
Slice []int `form:",multiple_default,default=9"`
Array [1]int `form:",multiple_default,default=9"`
}
err := mappingByPtr(&s, formSource{}, "form")
assert.NoError(t, err)

assert.Equal(t, 9, s.Int)
assert.Equal(t, []int{9}, s.Slice)
assert.Equal(t, [1]int{9}, s.Array)
}

func TestMappingSkipField(t *testing.T) {
var s struct {
A int
Expand Down