Forked from carbocation/gorilla_schema_sql_null_convertors.go
Created
April 24, 2018 05:21
-
-
Save dfang/692c757c195db9716b7c9865489a7a34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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