Skip to content
Myr

support default value for form (#1138)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by 田欧 · committed by Bo-Yi Wu · parent 814ac94 · Browse files · 2 files+68 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+68 added−1 removed

  • other:+68 added−1 removed

2 files+68 added−1 removed

modified:binding/binding_test.go+52 added
27 unmodified lines
28 FooStruct29 Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`30}3132type FooDefaultBarStruct struct {33 FooStruct34 Bar string `msgpack:"bar" json:"bar" form:"bar,default=hello" xml:"bar" binding:"required"`35}3637type FooStructUseNumber struct {38 Foo interface{} `json:"foo" binding:"required"`39}40
158 unmodified lines
199 "/?foo=bar&bar=foo", "/?bar2=foo",200 "", "")201}202203func TestBindingFormDefaultValue(t *testing.T) {204 testFormBindingDefaultValue(t, "POST",205 "/", "/",206 "foo=bar", "bar2=foo")207}208209func TestBindingFormDefaultValue2(t *testing.T) {210 testFormBindingDefaultValue(t, "GET",211 "/?foo=bar", "/?bar2=foo",212 "", "")213}214215func TestBindingFormForTime(t *testing.T) {216 testFormBindingForTime(t, "POST",217 "/", "/",218 "time_foo=2017-11-15&time_bar=", "bar2=foo")
204 unmodified lines
423 req.Header.Set("Content-Type", MIMEPOSTForm)424 return req425}426427func createDefaultFormPostRequest() *http.Request {428 req, _ := http.NewRequest("POST", "/?foo=getfoo&bar=getbar", bytes.NewBufferString("foo=bar"))429 req.Header.Set("Content-Type", MIMEPOSTForm)430 return req431}432433func createFormPostRequestFail() *http.Request {434 req, _ := http.NewRequest("POST", "/?map_foo=getfoo", bytes.NewBufferString("map_foo=bar"))435 req.Header.Set("Content-Type", MIMEPOSTForm)436 return req
35 unmodified lines
472 assert.Equal(t, "bar", obj.Foo)473 assert.Equal(t, "foo", obj.Bar)474}475476func TestBindingDefaultValueFormPost(t *testing.T) {477 req := createDefaultFormPostRequest()478 var obj FooDefaultBarStruct479 FormPost.Bind(req, &obj)480481 assert.Equal(t, "bar", obj.Foo)482 assert.Equal(t, "hello", obj.Bar)483}484485func TestBindingFormPostFail(t *testing.T) {486 req := createFormPostRequestFail()487 var obj FooStructForMapType488 err := FormPost.Bind(req, &obj)
120 unmodified lines
609 err = JSON.Bind(req, &obj)610 assert.Error(t, err)611}612613func testFormBindingDefaultValue(t *testing.T, method, path, badPath, body, badBody string) {614 b := Form615 assert.Equal(t, "form", b.Name())616617 obj := FooDefaultBarStruct{}618 req := requestWithBody(method, path, body)619 if method == "POST" {620 req.Header.Add("Content-Type", MIMEPOSTForm)621 }622 err := b.Bind(req, &obj)623 assert.NoError(t, err)624 assert.Equal(t, "bar", obj.Foo)625 assert.Equal(t, "hello", obj.Bar)626627 obj = FooDefaultBarStruct{}628 req = requestWithBody(method, badPath, badBody)629 err = JSON.Bind(req, &obj)630 assert.Error(t, err)631}632633func TestFormBindingFail(t *testing.T) {634 b := Form635 assert.Equal(t, "form", b.Name())636
470 unmodified lines
modified:binding/form_mapping.go+16 added-1 removed
6 unmodified lines
7import (8 "errors"9 "reflect"10 "strconv"11 "strings"12 "time"13)1415func mapForm(ptr interface{}, form map[string][]string) error {
7 unmodified lines
23 }2425 structFieldKind := structField.Kind()26 inputFieldName := typeField.Tag.Get("form")27 inputFieldNameList := strings.Split(inputFieldName, ",")28 inputFieldName = inputFieldNameList[0]29 var defaultValue string30 if len(inputFieldNameList) > 1 {31 defaultList := strings.SplitN(inputFieldNameList[1], "=", 2)32 if defaultList[0] == "default" {33 defaultValue = defaultList[1]34 }35 }36 if inputFieldName == "" {37 inputFieldName = typeField.Name3839 // if "form" tag is nil, we inspect if the field is a struct.
7 unmodified lines
47 continue48 }49 }50 inputValue, exists := form[inputFieldName]5152 if !exists {53 if defaultValue == "" {42 continue54 continue55 }56 inputValue = make([]string, 1)57 inputValue[0] = defaultValue58 }5960 numElems := len(inputValue)61 if structFieldKind == reflect.Slice && numElems > 0 {
135 unmodified lines