-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from twharmon/nulls
add null string, int64, and time.Time
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package gosql | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
"strconv" | ||
) | ||
|
||
// NullInt64 . | ||
type NullInt64 struct { | ||
Int64 int64 | ||
Valid bool | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (n *NullInt64) Scan(value interface{}) error { | ||
if value == nil { | ||
n.Valid = false | ||
return nil | ||
} | ||
n.Valid = true | ||
switch value.(type) { | ||
case int64: | ||
n.Int64 = value.(int64) | ||
case []byte: | ||
i, err := strconv.ParseInt(string(value.([]byte)), 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
n.Int64 = i | ||
} | ||
return nil | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (n NullInt64) Value() (driver.Value, error) { | ||
if !n.Valid { | ||
return nil, nil | ||
} | ||
return n.Int64, nil | ||
} | ||
|
||
// MarshalJSON . | ||
func (n NullInt64) MarshalJSON() ([]byte, error) { | ||
if n.Valid { | ||
return json.Marshal(n.Int64) | ||
} | ||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON . | ||
func (n *NullInt64) UnmarshalJSON(data []byte) error { | ||
var i64 *int64 | ||
if err := json.Unmarshal(data, &i64); err != nil { | ||
return err | ||
} | ||
if i64 != nil { | ||
n.Int64, n.Valid = *i64, true | ||
} else { | ||
n.Valid = false | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package gosql | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
) | ||
|
||
// NullString . | ||
type NullString struct { | ||
String string | ||
Valid bool | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (n *NullString) Scan(value interface{}) error { | ||
if value == nil { | ||
n.Valid = false | ||
return nil | ||
} | ||
switch value.(type) { | ||
case string: | ||
n.String, n.Valid = value.(string), true | ||
case []byte: | ||
n.String, n.Valid = string(value.([]byte)), true | ||
} | ||
return nil | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (n NullString) Value() (driver.Value, error) { | ||
if !n.Valid { | ||
return nil, nil | ||
} | ||
return n.String, nil | ||
} | ||
|
||
// MarshalJSON . | ||
func (n NullString) MarshalJSON() ([]byte, error) { | ||
if n.Valid { | ||
return json.Marshal(n.String) | ||
} | ||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON . | ||
func (n *NullString) UnmarshalJSON(data []byte) error { | ||
var s *string | ||
if err := json.Unmarshal(data, &s); err != nil { | ||
return err | ||
} | ||
if s != nil { | ||
n.String, n.Valid = *s, true | ||
} else { | ||
n.Valid = false | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package gosql | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
// NullTime . | ||
type NullTime struct { | ||
Time time.Time | ||
Valid bool | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (n *NullTime) Scan(value interface{}) error { | ||
if value == nil { | ||
n.Valid = false | ||
return nil | ||
} | ||
n.Valid = true | ||
n.Time = value.(time.Time) | ||
return nil | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (n NullTime) Value() (driver.Value, error) { | ||
if !n.Valid { | ||
return nil, nil | ||
} | ||
return n.Time, nil | ||
} | ||
|
||
// MarshalJSON . | ||
func (n NullTime) MarshalJSON() ([]byte, error) { | ||
if n.Valid { | ||
return json.Marshal(n.Time) | ||
} | ||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON . | ||
func (n *NullTime) UnmarshalJSON(data []byte) error { | ||
var t *time.Time | ||
if err := json.Unmarshal(data, &t); err != nil { | ||
return err | ||
} | ||
if t != nil { | ||
n.Time, n.Valid = *t, true | ||
} else { | ||
n.Valid = false | ||
} | ||
return nil | ||
} |