diff --git a/converter/json.go b/converter/json.go index 6f4f682..0d97346 100644 --- a/converter/json.go +++ b/converter/json.go @@ -1,82 +1,103 @@ package converter import ( "database/sql" "encoding/json" "reflect" ) type NullJSONBool struct { sql.NullBool } func JSONNullBool(value string) reflect.Value { v := NullJSONBool{} if value == "on" || value == "off" { return reflect.ValueOf(NullJSONBool{sql.NullBool{Bool: value == "on", Valid: true}}) } if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) } func (v NullJSONBool) MarshalJSON() ([]byte, error) { if v.Valid { return json.Marshal(v.Bool) } else { return json.Marshal(nil) } } func (v *NullJSONBool) UnmarshalJSON(data []byte) error { // Unmarshalling into a pointer will let us detect null var x *bool if err := json.Unmarshal(data, &x); err != nil { return err } if x != nil { v.Valid = true v.Bool = *x } else { v.Valid = false } return nil } type NullJSONString struct { sql.NullString } func JSONNullString(value string) reflect.Value { v := NullJSONString{} if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) } func (v NullJSONString) MarshalJSON() ([]byte, error) { if v.Valid { return json.Marshal(v.String) } else { return json.Marshal(nil) } } func (v *NullJSONString) UnmarshalJSON(data []byte) error { // Unmarshalling into a pointer will let us detect null var x *string if err := json.Unmarshal(data, &x); err != nil { return err } if x != nil { v.Valid = true v.String = *x } else { v.Valid = false } return nil } + +func ConvertJSONNullString(value string) reflect.Value { + v := NullJSONString{} + if err := v.Scan(value); err != nil { + return reflect.Value{} + } + + return reflect.ValueOf(v) +} + +func ConvertJSONNullBool(value string) reflect.Value { + v := NullJSONBool{} + + if value == "on" || value == "off" { + return reflect.ValueOf(NullJSONBool{sql.NullBool{Bool: value == "on", Valid: true}}) + } + if err := v.Scan(value); err != nil { + return reflect.Value{} + } + return reflect.ValueOf(v) +} diff --git a/converter/sql.go b/converter/sql.go index 1992a81..5075106 100644 --- a/converter/sql.go +++ b/converter/sql.go @@ -1,42 +1,78 @@ package converter import ( "database/sql" "reflect" ) func SQLNullString(value string) reflect.Value { v := sql.NullString{} if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) } func SQLNullBool(value string) reflect.Value { v := sql.NullBool{} if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) } func SQLNullInt64(value string) reflect.Value { v := sql.NullInt64{} if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) } func SQLNullFloat64(value string) reflect.Value { v := sql.NullFloat64{} if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) } + +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) +}