Skip to content

Commit

Permalink
Migrate nulls to gobuffalo/nulls (#357)
Browse files Browse the repository at this point in the history
* Migrate nulls to gobuffalo/nulls

* Forward nulls to gobuffalo/nulls

* Update SHOULDERS.md and nulls README
  • Loading branch information
stanislas-m committed Mar 7, 2019
1 parent c276e05 commit ab105fe
Show file tree
Hide file tree
Showing 29 changed files with 59 additions and 851 deletions.
14 changes: 2 additions & 12 deletions SHOULDERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Thank you to the following **GIANTS**:

* [github.com/gobuffalo/makr](https://godoc.org/github.com/gobuffalo/makr)

* [github.com/gobuffalo/nulls](https://godoc.org/github.com/gobuffalo/nulls)

* [github.com/gobuffalo/packd](https://godoc.org/github.com/gobuffalo/packd)

* [github.com/gobuffalo/plush](https://godoc.org/github.com/gobuffalo/plush)
Expand All @@ -51,18 +53,6 @@ Thank you to the following **GIANTS**:

* [github.com/gobuffalo/plush/token](https://godoc.org/github.com/gobuffalo/plush/token)

* [github.com/gobuffalo/pop](https://godoc.org/github.com/gobuffalo/pop)

* [github.com/gobuffalo/pop/associations](https://godoc.org/github.com/gobuffalo/pop/associations)

* [github.com/gobuffalo/pop/columns](https://godoc.org/github.com/gobuffalo/pop/columns)

* [github.com/gobuffalo/pop/fix](https://godoc.org/github.com/gobuffalo/pop/fix)

* [github.com/gobuffalo/pop/logging](https://godoc.org/github.com/gobuffalo/pop/logging)

* [github.com/gobuffalo/pop/nulls](https://godoc.org/github.com/gobuffalo/pop/nulls)

* [github.com/gobuffalo/syncx](https://godoc.org/github.com/gobuffalo/syncx)

* [github.com/gobuffalo/tags](https://godoc.org/github.com/gobuffalo/tags)
Expand Down
2 changes: 1 addition & 1 deletion associations/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package associations
import (
"reflect"

"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/nulls"
)

// Association represents a definition of a model association
Expand Down
2 changes: 1 addition & 1 deletion associations/belongs_to_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"reflect"

"github.com/gobuffalo/flect"
"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/x/defaults"
)

Expand Down
2 changes: 1 addition & 1 deletion associations/has_many_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"

"github.com/gobuffalo/flect"
"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/nulls"
"github.com/jmoiron/sqlx"
)

Expand Down
2 changes: 1 addition & 1 deletion associations/has_many_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"reflect"
"testing"

"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/associations"
"github.com/gobuffalo/pop/nulls"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion associations/has_one_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"

"github.com/gobuffalo/flect"
"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/nulls"
"github.com/gobuffalo/x/defaults"
)

Expand Down
2 changes: 1 addition & 1 deletion associations/has_one_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"reflect"
"testing"

"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/associations"
"github.com/gobuffalo/pop/nulls"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"testing"

"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/nulls"
)

func Benchmark_Create_Pop(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion executors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pop
import (
"testing"

"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/nulls"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion finders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pop
import (
"testing"

"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/nulls"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 2 additions & 0 deletions nulls/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# github.com/gobuffalo/pop/nulls

DEPRECATED: use github.com/gobuffalo/nulls instead.

This package should be used in place of the built-in null types in the `sql` package.

The real benefit of this packages comes in its implementation of `MarshalJSON` and `UnmarshalJSON` to properly encode/decode `null` values.
Expand Down
77 changes: 4 additions & 73 deletions nulls/bool.go
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
72 changes: 3 additions & 69 deletions nulls/byte_slice.go
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
73 changes: 3 additions & 70 deletions nulls/float32.go
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
Loading

0 comments on commit ab105fe

Please sign in to comment.