-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate nulls to gobuffalo/nulls (#357)
* Migrate nulls to gobuffalo/nulls * Forward nulls to gobuffalo/nulls * Update SHOULDERS.md and nulls README
- Loading branch information
1 parent
c276e05
commit ab105fe
Showing
29 changed files
with
59 additions
and
851 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
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
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,82 +1,13 @@ | ||
package nulls | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"github.com/gobuffalo/nulls" | ||
) | ||
|
||
// Bool replaces sql.NullBool with an implementation | ||
// that supports proper JSON encoding/decoding. | ||
type Bool struct { | ||
Bool bool | ||
Valid bool | ||
} | ||
|
||
// Interface implements the nullable interface. It returns nil if | ||
// the bool is not valid, otherwise it returns the bool value. | ||
func (ns Bool) Interface() interface{} { | ||
if !ns.Valid { | ||
return nil | ||
} | ||
return ns.Bool | ||
} | ||
type Bool nulls.Bool | ||
|
||
// NewBool returns a new, properly instantiated | ||
// Boll object. | ||
func NewBool(b bool) Bool { | ||
return Bool{Bool: b, Valid: true} | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (ns *Bool) Scan(value interface{}) error { | ||
n := sql.NullBool{Bool: ns.Bool} | ||
err := n.Scan(value) | ||
ns.Bool, ns.Valid = n.Bool, n.Valid | ||
return err | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (ns Bool) Value() (driver.Value, error) { | ||
if !ns.Valid { | ||
return nil, nil | ||
} | ||
return ns.Bool, nil | ||
} | ||
|
||
// MarshalJSON marshals the underlying value to a | ||
// proper JSON representation. | ||
func (ns Bool) MarshalJSON() ([]byte, error) { | ||
if ns.Valid { | ||
return json.Marshal(ns.Bool) | ||
} | ||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON will unmarshal a JSON value into | ||
// the proper representation of that value. The strings | ||
// "true" and "t" will be considered "true", "false" and "f" will | ||
// be treated as "false". All other values will | ||
//be set to null by Valid = false | ||
func (ns *Bool) UnmarshalJSON(text []byte) error { | ||
t := string(text) | ||
if t == "true" || t == "t" { | ||
ns.Valid = true | ||
ns.Bool = true | ||
return nil | ||
} | ||
if t == "false" || t == "f" { | ||
ns.Valid = true | ||
ns.Bool = false | ||
return nil | ||
} | ||
ns.Bool = false | ||
ns.Valid = false | ||
return nil | ||
} | ||
|
||
// UnmarshalText will unmarshal text value into | ||
// the propert representation of that value. | ||
func (ns *Bool) UnmarshalText(text []byte) error { | ||
return ns.UnmarshalJSON(text) | ||
} | ||
// Bool object. | ||
var NewBool = nulls.NewBool |
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 |
---|---|---|
@@ -1,79 +1,13 @@ | ||
package nulls | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"encoding/base64" | ||
"encoding/json" | ||
"github.com/gobuffalo/nulls" | ||
) | ||
|
||
// ByteSlice adds an implementation for []byte | ||
// that supports proper JSON encoding/decoding. | ||
type ByteSlice struct { | ||
ByteSlice []byte | ||
Valid bool // Valid is true if ByteSlice is not NULL | ||
} | ||
|
||
// Interface implements the nullable interface. It returns nil if | ||
// the byte slice is not valid, otherwise it returns the byte slice value. | ||
func (ns ByteSlice) Interface() interface{} { | ||
if !ns.Valid { | ||
return nil | ||
} | ||
return ns.ByteSlice | ||
} | ||
type ByteSlice nulls.ByteSlice | ||
|
||
// NewByteSlice returns a new, properly instantiated | ||
// ByteSlice object. | ||
func NewByteSlice(b []byte) ByteSlice { | ||
return ByteSlice{ByteSlice: b, Valid: true} | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (ns *ByteSlice) Scan(value interface{}) error { | ||
n := sql.NullString{String: base64.StdEncoding.EncodeToString(ns.ByteSlice)} | ||
err := n.Scan(value) | ||
if err != nil { | ||
return err | ||
} | ||
//ns.Float32, ns.Valid = float32(n.Float64), n.Valid | ||
ns.ByteSlice, err = base64.StdEncoding.DecodeString(n.String) | ||
ns.Valid = n.Valid | ||
return err | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (ns ByteSlice) Value() (driver.Value, error) { | ||
if !ns.Valid { | ||
return nil, nil | ||
} | ||
return base64.StdEncoding.EncodeToString(ns.ByteSlice), nil | ||
} | ||
|
||
// MarshalJSON marshals the underlying value to a | ||
// proper JSON representation. | ||
func (ns ByteSlice) MarshalJSON() ([]byte, error) { | ||
if ns.Valid { | ||
return json.Marshal(ns.ByteSlice) | ||
} | ||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON will unmarshal a JSON value into | ||
// the propert representation of that value. | ||
func (ns *ByteSlice) UnmarshalJSON(text []byte) error { | ||
ns.Valid = false | ||
if string(text) == "null" { | ||
return nil | ||
} | ||
|
||
ns.ByteSlice = text | ||
ns.Valid = true | ||
return nil | ||
} | ||
|
||
// UnmarshalText will unmarshal text value into | ||
// the propert representation of that value. | ||
func (ns *ByteSlice) UnmarshalText(text []byte) error { | ||
return ns.UnmarshalJSON(text) | ||
} | ||
var NewByteSlice = nulls.NewByteSlice |
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 |
---|---|---|
@@ -1,80 +1,13 @@ | ||
package nulls | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"strconv" | ||
"github.com/gobuffalo/nulls" | ||
) | ||
|
||
// Float32 adds an implementation for float32 | ||
// that supports proper JSON encoding/decoding. | ||
type Float32 struct { | ||
Float32 float32 | ||
Valid bool // Valid is true if Float32 is not NULL | ||
} | ||
|
||
// Interface implements the nullable interface. It returns nil if | ||
// the float32 is not valid, otherwise it returns the float32 value. | ||
func (ns Float32) Interface() interface{} { | ||
if !ns.Valid { | ||
return nil | ||
} | ||
return ns.Float32 | ||
} | ||
type Float32 nulls.Float32 | ||
|
||
// NewFloat32 returns a new, properly instantiated | ||
// Float32 object. | ||
func NewFloat32(i float32) Float32 { | ||
return Float32{Float32: i, Valid: true} | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (ns *Float32) Scan(value interface{}) error { | ||
n := sql.NullFloat64{Float64: float64(ns.Float32)} | ||
err := n.Scan(value) | ||
ns.Float32, ns.Valid = float32(n.Float64), n.Valid | ||
return err | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (ns Float32) Value() (driver.Value, error) { | ||
if !ns.Valid { | ||
return nil, nil | ||
} | ||
return float64(ns.Float32), nil | ||
} | ||
|
||
// MarshalJSON marshals the underlying value to a | ||
// proper JSON representation. | ||
func (ns Float32) MarshalJSON() ([]byte, error) { | ||
if ns.Valid { | ||
return json.Marshal(ns.Float32) | ||
} | ||
return json.Marshal(nil) | ||
} | ||
|
||
// UnmarshalJSON will unmarshal a JSON value into | ||
// the propert representation of that value. | ||
func (ns *Float32) UnmarshalJSON(text []byte) error { | ||
txt := string(text) | ||
ns.Valid = true | ||
if txt == "null" { | ||
ns.Valid = false | ||
return nil | ||
} | ||
i, err := strconv.ParseFloat(txt, 32) | ||
if err != nil { | ||
ns.Valid = false | ||
return err | ||
} | ||
j := float32(i) | ||
ns.Float32 = j | ||
return nil | ||
} | ||
|
||
// UnmarshalText will unmarshal text value into | ||
// the propert representation of that value. | ||
func (ns *Float32) UnmarshalText(text []byte) error { | ||
return ns.UnmarshalJSON(text) | ||
} | ||
var NewFloat32 = nulls.NewFloat32 |
Oops, something went wrong.