Skip to content

Commit

Permalink
Pointers support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Lukash committed Jun 22, 2016
1 parent 262e031 commit c3fcb76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion binding.go
Expand Up @@ -266,7 +266,13 @@ func mapForm(formStruct reflect.Value, form map[string][]string,
}
formStruct.Field(i).Set(slice)
} else {
setWithProperType(typeField.Type.Kind(), inputValue[0], structField, inputFieldName, errors)
kind := typeField.Type.Kind()
if structField.Kind() == reflect.Ptr {
structField.Set(reflect.New(typeField.Type.Elem()))
structField = structField.Elem()
kind = typeField.Type.Elem().Kind()
}
setWithProperType(kind, inputValue[0], structField, inputFieldName, errors)
}
continue
}
Expand Down
1 change: 1 addition & 0 deletions common_test.go
Expand Up @@ -36,6 +36,7 @@ type (
Coauthor *Person `json:"coauthor"`
HeaderImage *multipart.FileHeader `form:"headerImage"`
Pictures []*multipart.FileHeader `form:"picture"`
IsActive *bool `form:"is_active"`
unexported string `form:"unexported"`
}

Expand Down
10 changes: 10 additions & 0 deletions form_test.go
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/go-martini/martini"
)

var addressableTrue bool = true

var formTestCases = []formTestCase{
{
description: "Happy path",
Expand Down Expand Up @@ -83,6 +85,14 @@ var formTestCases = []formTestCase{
contentType: formContentType,
expected: BlogPost{Post: Post{Title: "Glorious Post Title"}, Id: 1, Author: Person{Name: "Matt Holt"}},
},
{
description: "Bool pointer",
shouldSucceed: true,
payload: `title=Glorious+Post+Title&id=1&name=Matt+Holt&is_active=true`,
contentType: formContentType,
expected: BlogPost{Post: Post{Title: "Glorious Post Title"}, Id: 1, Author: Person{Name: "Matt Holt"}, IsActive: &addressableTrue},
deepEqual: true,
},
{
description: "Query string POST",
shouldSucceed: true,
Expand Down

0 comments on commit c3fcb76

Please sign in to comment.