Skip to content

Instantly share code, notes, and snippets.

@carbocation
Created January 2, 2015 13:35
Show Gist options
  • Save carbocation/51b55297702c7d30d3ef to your computer and use it in GitHub Desktop.
Save carbocation/51b55297702c7d30d3ef to your computer and use it in GitHub Desktop.
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)
}
@max107
Copy link

max107 commented Sep 9, 2015

Thanks!

@dfang
Copy link

dfang commented Apr 25, 2018

how to convert null.time ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment