Created
January 2, 2015 13:35
-
-
Save carbocation/51b55297702c7d30d3ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package poison | |
import ( | |
"database/sql" | |
"reflect" | |
"github.com/gorilla/schema" | |
) | |
// Convertors for sql.Null* types so that they can be | |
// used with gorilla/schema | |
func init() { | |
SchemaRegisterSQLNulls(Decoder) | |
} | |
func SchemaRegisterSQLNulls(d *schema.Decoder) { | |
nullString, nullBool, nullInt64, nullFloat64 := sql.NullString{}, sql.NullBool{}, sql.NullInt64{}, sql.NullFloat64{} | |
d.RegisterConverter(nullString, ConvertSQLNullString) | |
d.RegisterConverter(nullBool, ConvertSQLNullBool) | |
d.RegisterConverter(nullInt64, ConvertSQLNullInt64) | |
d.RegisterConverter(nullFloat64, ConvertSQLNullFloat64) | |
} | |
func ConvertSQLNullString(value string) reflect.Value { | |
v := sql.NullString{} | |
if err := v.Scan(value); err != nil { | |
return reflect.Value{} | |
} | |
return reflect.ValueOf(v) | |
} | |
func ConvertSQLNullBool(value string) reflect.Value { | |
v := sql.NullBool{} | |
if err := v.Scan(value); err != nil { | |
return reflect.Value{} | |
} | |
return reflect.ValueOf(v) | |
} | |
func ConvertSQLNullInt64(value string) reflect.Value { | |
v := sql.NullInt64{} | |
if err := v.Scan(value); err != nil { | |
return reflect.Value{} | |
} | |
return reflect.ValueOf(v) | |
} | |
func ConvertSQLNullFloat64(value string) reflect.Value { | |
v := sql.NullFloat64{} | |
if err := v.Scan(value); err != nil { | |
return reflect.Value{} | |
} | |
return reflect.ValueOf(v) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!