Skip to content

Commit

Permalink
Add json (un)marshalling methods on NullUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
blastrock committed Mar 31, 2017
1 parent b061729 commit b99e53e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"database/sql/driver"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"hash"
"net"
Expand Down Expand Up @@ -344,6 +345,29 @@ func (u *NullUUID) Scan(src interface{}) error {
return u.UUID.Scan(src)
}

// MarshalJSON marshalls 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 unmarshalls 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
}

// FromBytes returns UUID converted from raw byte slice input.
// It will return error if the slice isn't 16 bytes long.
func FromBytes(input []byte) (u UUID, err error) {
Expand Down
44 changes: 44 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package uuid

import (
"bytes"
"encoding/json"
"testing"
)

Expand Down Expand Up @@ -631,3 +632,46 @@ func TestNewV5(t *testing.T) {
t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
}
}

func TestMarshalNullUUID(t *testing.T) {
u := NullUUID{UUID: NewV4(), Valid: true}
j, err := json.Marshal(u)
if err != nil {
t.Error("Couldn't marshal a valid NullUUID: ", err)
}

if string(j) != "\""+u.UUID.String()+"\"" {
t.Error("Marshaled NullUUID is incorrect: ", string(j))
}

nu := NullUUID{Valid: false}
j, err = json.Marshal(nu)
if err != nil {
t.Error("Couldn't marshal an invalid NullUUID: ", err)
}

if string(j) != "null" {
t.Error("Marshaled NullUUID is incorrect: ", string(j))
}
}

func TestUnmarshalNullUUID(t *testing.T) {
var u NullUUID
err := json.Unmarshal([]byte("null"), &u)
if err != nil {
t.Error("Couldn't Unmarshal an invalid NullUUID: ", err)
}

if u.Valid != false {
t.Error("Unmarshaled NullUUID is valid but shouldn't")
}

err = json.Unmarshal([]byte("\"886313e1-3b8a-5372-9b90-0c9aee199e5d\""), &u)
if err != nil {
t.Error("Couldn't Unmarshal an invalid NullUUID: ", err)
}

if u.Valid != true || u.UUID.String() != "886313e1-3b8a-5372-9b90-0c9aee199e5d" {
t.Error("Unmarshaled NullUUID is incorrect: ", u.Valid, u.UUID)
}
}

0 comments on commit b99e53e

Please sign in to comment.