Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Scan interface Support (sql.Null types) #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/go-martini/martini"
"database/sql"
)

/*
Expand Down Expand Up @@ -249,7 +250,19 @@ func mapForm(formStruct reflect.Value, form map[string][]string,
structField.Set(reflect.Zero(structField.Type()))
}
} else if typeField.Type.Kind() == reflect.Struct {
mapForm(structField, form, formfile, errors)
if _, ok := structField.Addr().Interface().(sql.Scanner); ok {
if inputFieldName := typeField.Tag.Get("form"); inputFieldName != "" {
if !structField.CanSet() {
continue
}
inputValue, exists := form[inputFieldName]
if exists {
setWithProperType(typeField.Type.Kind(), inputValue[0], structField, inputFieldName, errors)
}
}
} else {
mapForm(structField, form, formfile, errors)
}
} else if inputFieldName := typeField.Tag.Get("form"); inputFieldName != "" {
if !structField.CanSet() {
continue
Expand Down Expand Up @@ -320,7 +333,11 @@ func ErrorHandler(errs Errors, resp http.ResponseWriter) {
// same type, so that not all deserialized values have to be strings.
// Supported types are string, int, float, and bool.
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value, nameInTag string, errors Errors) {
switch valueKind {
if scan, ok := structField.Addr().Interface().(sql.Scanner); ok {
scan.Scan(val)
return
}
switch valueKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if val == "" {
val = "0"
Expand Down