Skip to content

Commit

Permalink
Adapt satori/go.uuid#44 for our fork
Browse files Browse the repository at this point in the history
This commit adapts satori/go.uuid#44 for our fork of the
original project. This brings JSON Marshaling and Unmarshaling to the
`uuid.NullUUID` type used for use with SQL databases.

This is needed because `uuid.NullUUID` is a shim around `uuid.UUID`, providing
the information needed by the `database/sql` package to support storing the
value in a nullable column. Without these methods, the type gets converted
unnecessarily to a different format than the standard `uuid.UUID` type.

Signed-off-by: Tim Heckman <t@heckman.io>
  • Loading branch information
theckman committed Jul 23, 2018
2 parents 3a54a64 + b99e53e commit d036565
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 53 deletions.
26 changes: 26 additions & 0 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
package uuid

import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -76,3 +78,27 @@ func (u *NullUUID) Scan(src interface{}) error {
u.Valid = true
return u.UUID.Scan(src)
}

// MarshalJSON marshals the NullUUID as nil or the nested UUID
func (u NullUUID) MarshalJSON() ([]byte, error) {
if u.Valid == false {
return json.Marshal(nil)
}
return json.Marshal(u.UUID)
}

// UnmarshalJSON unmarshals a NullUUID
func (u *NullUUID) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
u.UUID, u.Valid = Nil, false
return nil
}

if err := json.Unmarshal(b, &u.UUID); err != nil {
return err
}

u.Valid = true

return nil
}
176 changes: 123 additions & 53 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

package uuid

import "testing"
import (
"fmt"
"testing"
)

func TestSQL(t *testing.T) {
t.Run("Value", testSQLValue)
Expand Down Expand Up @@ -106,59 +109,126 @@ func testSQLScanNil(t *testing.T) {
}

func TestNullUUID(t *testing.T) {
t.Run("NilValue", func(t *testing.T) {
nu := NullUUID{}
got, err := nu.Value()
if got != nil {
t.Errorf("null NullUUID.Value returned non-nil driver.Value")
}
if err != nil {
t.Errorf("null NullUUID.Value returned non-nil error")
}
t.Run("Value", func(t *testing.T) {
t.Run("Nil", testNullUUIDValueNil)
t.Run("Valid", testNullUUIDValueValid)
})
t.Run("ValidValue", func(t *testing.T) {
nu := NullUUID{
Valid: true,
UUID: codecTestUUID,
}
got, err := nu.Value()
if err != nil {
t.Fatal(err)
}
s, ok := got.(string)
if !ok {
t.Errorf("Value() returned %T, want string", got)
}
want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
if s != want {
t.Errorf("%v.Value() == %s, want %s", nu, s, want)
}
})
t.Run("ScanValid", func(t *testing.T) {
s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u := NullUUID{}
err := u.Scan(s)
if err != nil {
t.Fatal(err)
}
if !u.Valid {
t.Errorf("Valid == false after Scan(%q)", s)
}
if u.UUID != codecTestUUID {
t.Errorf("UUID == %v after Scan(%q), want %v", u.UUID, s, codecTestUUID)
}

t.Run("Scan", func(t *testing.T) {
t.Run("Nil", testNullUUIDScanNil)
t.Run("Valid", testNullUUIDScanValid)
})
t.Run("ScanNil", func(t *testing.T) {
u := NullUUID{}
err := u.Scan(nil)
if err != nil {
t.Fatal(err)
}
if u.Valid {
t.Error("NullUUID is valid after Scan(nil)")
}
if u.UUID != Nil {
t.Errorf("NullUUID.UUID is %v after Scan(nil) want Nil", u.UUID)
}

t.Run("MarshalJSON", func(t *testing.T) {
t.Run("Nil", testNullUUIDMarshalJSONNil)
t.Run("Valid", testNullUUIDMarshalJSONValid)
t.Run("Invalid", testNullUUIDMarshalJSONInvalid)
})
}

func testNullUUIDValueNil(t *testing.T) {
nu := NullUUID{}
got, err := nu.Value()
if got != nil {
t.Errorf("null NullUUID.Value returned non-nil driver.Value")
}
if err != nil {
t.Errorf("null NullUUID.Value returned non-nil error")
}
}

func testNullUUIDValueValid(t *testing.T) {
nu := NullUUID{
Valid: true,
UUID: codecTestUUID,
}
got, err := nu.Value()
if err != nil {
t.Fatal(err)
}
s, ok := got.(string)
if !ok {
t.Errorf("Value() returned %T, want string", got)
}
want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
if s != want {
t.Errorf("%v.Value() == %s, want %s", nu, s, want)
}
}

func testNullUUIDScanNil(t *testing.T) {
u := NullUUID{}
err := u.Scan(nil)
if err != nil {
t.Fatal(err)
}
if u.Valid {
t.Error("NullUUID is valid after Scan(nil)")
}
if u.UUID != Nil {
t.Errorf("NullUUID.UUID is %v after Scan(nil) want Nil", u.UUID)
}
}

func testNullUUIDScanValid(t *testing.T) {
s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u := NullUUID{}
err := u.Scan(s)
if err != nil {
t.Fatal(err)
}
if !u.Valid {
t.Errorf("Valid == false after Scan(%q)", s)
}
if u.UUID != codecTestUUID {
t.Errorf("UUID == %v after Scan(%q), want %v", u.UUID, s, codecTestUUID)
}
}

func testNullUUIDMarshalJSONNil(t *testing.T) {
u := NullUUID{Valid: true}

data, err := u.MarshalJSON()
if err != nil {
t.Fatalf("(%#v).MarshalJSON err want: <nil>, got: %v", u, err)
}

dataStr := string(data)

if dataStr != fmt.Sprintf("%q", Nil) {
t.Fatalf("(%#v).MarshalJSON value want: %s, got: %s", u, Nil, dataStr)
}
}

func testNullUUIDMarshalJSONValid(t *testing.T) {
u := NullUUID{
Valid: true,
UUID: codecTestUUID,
}

data, err := u.MarshalJSON()
if err != nil {
t.Fatalf("(%#v).MarshalJSON err want: <nil>, got: %v", u, err)
}

dataStr := string(data)

if dataStr != fmt.Sprintf("%q", codecTestUUID) {
t.Fatalf("(%#v).MarshalJSON value want: %s, got: %s", u, codecTestUUID, dataStr)
}
}

func testNullUUIDMarshalJSONInvalid(t *testing.T) {
u := NullUUID{}

data, err := u.MarshalJSON()
if err != nil {
t.Fatalf("(%#v).MarshalJSON err want: <nil>, got: %v", u, err)
}

dataStr := string(data)

if dataStr != "null" {
t.Fatalf("(%#v).MarshalJSON value want: %s, got: %s", u, "null", dataStr)
}
}

0 comments on commit d036565

Please sign in to comment.