From c3fcb7699856e49501ea4ae9189e90d6bc952242 Mon Sep 17 00:00:00 2001 From: Yegor Lukash Date: Wed, 22 Jun 2016 15:10:02 +0000 Subject: [PATCH] Pointers support --- binding.go | 8 +++++++- common_test.go | 1 + form_test.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/binding.go b/binding.go index 1ac3a44..b97913e 100644 --- a/binding.go +++ b/binding.go @@ -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 } diff --git a/common_test.go b/common_test.go index 268b5e4..771fa35 100644 --- a/common_test.go +++ b/common_test.go @@ -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"` } diff --git a/form_test.go b/form_test.go index 9387f4e..f88ccec 100644 --- a/form_test.go +++ b/form_test.go @@ -11,6 +11,8 @@ import ( "github.com/go-martini/martini" ) +var addressableTrue bool = true + var formTestCases = []formTestCase{ { description: "Happy path", @@ -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,