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
Provenance
- Commit
41f951e- Tree
029acab- Author
田欧 <thinkerou@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
814ac94
2 files changed+68 added−1 removed
modified:binding/binding_test.go+52 added
27 unmodified lines28 FooStruct29 Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"`30 }31 32+type FooDefaultBarStruct struct {33+ FooStruct34+ Bar string `msgpack:"bar" json:"bar" form:"bar,default=hello" xml:"bar" binding:"required"`35+}36+37 type FooStructUseNumber struct {38 Foo interface{} `json:"foo" binding:"required"`39 }40 158 unmodified lines199 "/?foo=bar&bar=foo", "/?bar2=foo",200 "", "")201 }202 203+func TestBindingFormDefaultValue(t *testing.T) {204+ testFormBindingDefaultValue(t, "POST",205+ "/", "/",206+ "foo=bar", "bar2=foo")207+}208+209+func TestBindingFormDefaultValue2(t *testing.T) {210+ testFormBindingDefaultValue(t, "GET",211+ "/?foo=bar", "/?bar2=foo",212+ "", "")213+}214+215 func TestBindingFormForTime(t *testing.T) {216 testFormBindingForTime(t, "POST",217 "/", "/",218 "time_foo=2017-11-15&time_bar=", "bar2=foo")204 unmodified lines423 req.Header.Set("Content-Type", MIMEPOSTForm)424 return req425 }426 427+func 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+}432+433 func 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 req35 unmodified lines472 assert.Equal(t, "bar", obj.Foo)473 assert.Equal(t, "foo", obj.Bar)474 }475 476+func TestBindingDefaultValueFormPost(t *testing.T) {477+ req := createDefaultFormPostRequest()478+ var obj FooDefaultBarStruct479+ FormPost.Bind(req, &obj)480+481+ assert.Equal(t, "bar", obj.Foo)482+ assert.Equal(t, "hello", obj.Bar)483+}484+485 func TestBindingFormPostFail(t *testing.T) {486 req := createFormPostRequestFail()487 var obj FooStructForMapType488 err := FormPost.Bind(req, &obj)120 unmodified lines609 err = JSON.Bind(req, &obj)610 assert.Error(t, err)611 }612 613+func testFormBindingDefaultValue(t *testing.T, method, path, badPath, body, badBody string) {614+ b := Form615+ assert.Equal(t, "form", b.Name())616+617+ 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)626+627+ obj = FooDefaultBarStruct{}628+ req = requestWithBody(method, badPath, badBody)629+ err = JSON.Bind(req, &obj)630+ assert.Error(t, err)631+}632+633 func TestFormBindingFail(t *testing.T) {634 b := Form635 assert.Equal(t, "form", b.Name())636 470 unmodified linesmodified:binding/form_mapping.go+16 added-1 removed
6 unmodified lines7 import (8 "errors"9 "reflect"10 "strconv"11+ "strings"12 "time"13 )14 15 func mapForm(ptr interface{}, form map[string][]string) error {7 unmodified lines23 }24 25 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.Name38 39 // if "form" tag is nil, we inspect if the field is a struct.7 unmodified lines47 continue48 }49 }50 inputValue, exists := form[inputFieldName]51+52 if !exists {53+ if defaultValue == "" {42- continue54+ continue55+ }56+ inputValue = make([]string, 1)57+ inputValue[0] = defaultValue58 }59 60 numElems := len(inputValue)61 if structFieldKind == reflect.Slice && numElems > 0 {135 unmodified lines