Skip to content

Commit

Permalink
Merge pull request #8 from twharmon/nulls
Browse files Browse the repository at this point in the history
add null string, int64, and time.Time
  • Loading branch information
twharmon authored Jan 28, 2020
2 parents 383d014 + 0a71b2b commit 3e23ba7
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
63 changes: 63 additions & 0 deletions null_int64.go
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
}
57 changes: 57 additions & 0 deletions null_string.go
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
}
54 changes: 54 additions & 0 deletions null_time.go
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
}

0 comments on commit 3e23ba7

Please sign in to comment.