Add NullJSON

This commit is contained in:
Patrick O'brien 2016-09-07 03:31:45 +10:00
parent 877f2a1492
commit f3d5b4769e
2 changed files with 289 additions and 0 deletions

130
json.go Normal file
View file

@ -0,0 +1,130 @@
package null
import (
"database/sql/driver"
"gopkg.in/nullbio/null.v4/convert"
)
// NullJSON is a nullable byte slice.
type NullJSON struct {
JSON []byte
Valid bool
}
// JSON is a nullable []byte.
// JSON marshals to zero if null.
// Considered null to SQL if zero.
type JSON struct {
NullJSON
}
// NewJSON creates a new JSON
func NewJSON(b []byte, valid bool) JSON {
return JSON{
NullJSON: NullJSON{
JSON: b,
Valid: valid,
},
}
}
// JSONFrom creates a new JSON that will be null if len zero.
func JSONFrom(b []byte) JSON {
return NewJSON(b, len(b) != 0)
}
// JSONFromPtr creates a new JSON that be null if len zero.
func JSONFromPtr(b *[]byte) JSON {
if b == nil || len(*b) == 0 {
return NewJSON(nil, false)
}
n := NewJSON(*b, true)
return n
}
// UnmarshalJSON implements json.Unmarshaler.
// JSON UnmarshalJSON is different in that it only
// unmarshals sql.NullJSON defined as JSON objects,
// It supports all JSON types.
// It also supports unmarshalling a sql.NullJSON.
func (j *JSON) UnmarshalJSON(data []byte) error {
if data == nil || len(data) == 0 {
j.JSON = nil
j.Valid = false
} else {
j.JSON = append(j.JSON[0:0], data...)
j.Valid = true
}
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null JSON if the input is blank.
// It will return an error if the input is not an integer, blank, or "null".
func (j *JSON) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
j.Valid = false
} else {
j.JSON = append(j.JSON[0:0], text...)
j.Valid = true
}
return nil
}
// MarshalJSON implements json.Marshaler.
// It will encode null if the JSON is invalid.
func (j JSON) MarshalJSON() ([]byte, error) {
if !j.Valid {
return []byte("null"), nil
}
return j.JSON, nil
}
// MarshalText implements encoding.TextMarshaler.
// It will encode nil if the JSON is invalid.
func (j JSON) MarshalText() ([]byte, error) {
if !j.Valid {
return nil, nil
}
return j.JSON, nil
}
// SetValid changes this JSON's value and also sets it to be non-null.
func (j *JSON) SetValid(n []byte) {
j.JSON = n
j.Valid = true
}
// Ptr returns a pointer to this JSON's value, or a nil pointer if this JSON is null.
func (j JSON) Ptr() *[]byte {
if !j.Valid {
return nil
}
return &j.JSON
}
// IsZero returns true for null or zero JSON's, for future omitempty support (Go 1.4?)
func (j JSON) IsZero() bool {
return !j.Valid
}
// Scan implements the Scanner interface.
func (n *NullJSON) Scan(value interface{}) error {
if value == nil {
n.JSON, n.Valid = []byte{}, false
return nil
}
n.Valid = true
return convert.ConvertAssign(&n.JSON, value)
}
// Value implements the driver Valuer interface.
func (n NullJSON) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.JSON, nil
}

159
json_test.go Normal file
View file

@ -0,0 +1,159 @@
package null
import (
"bytes"
"encoding/json"
"testing"
)
var (
jsonJSON = []byte(`"hello"`)
)
func TestJSONFrom(t *testing.T) {
i := JSONFrom([]byte(`"hello"`))
assertJSON(t, i, "JSONFrom()")
zero := JSONFrom(nil)
if zero.Valid {
t.Error("JSONFrom(nil)", "is valid, but should be invalid")
}
}
func TestJSONFromPtr(t *testing.T) {
n := []byte(`"hello"`)
iptr := &n
i := JSONFromPtr(iptr)
assertJSON(t, i, "JSONFromPtr()")
null := JSONFromPtr(nil)
assertNullJSON(t, null, "JSONFromPtr(nil)")
}
func TestUnmarshalJSON(t *testing.T) {
var i JSON
err := json.Unmarshal(jsonJSON, &i)
maybePanic(err)
assertJSON(t, i, "[]byte json")
var ni JSON
err = ni.UnmarshalJSON([]byte{})
if ni.Valid == true {
t.Errorf("expected Valid to be false, got true")
}
if !bytes.Equal(ni.JSON, []byte(nil)) {
t.Errorf("Expected JSON to be nil slice, but was not: %#v %#v", ni.JSON, []byte(nil))
}
var null JSON
err = ni.UnmarshalJSON(nil)
if ni.Valid == true {
t.Errorf("expected Valid to be false, got true")
}
if !bytes.Equal(null.JSON, []byte(nil)) {
t.Errorf("Expected JSON to be []byte nil, but was not: %#v %#v", null.JSON, []byte(nil))
}
}
func TestTextUnmarshalJSON(t *testing.T) {
var i JSON
err := i.UnmarshalText([]byte(`"hello"`))
maybePanic(err)
assertJSON(t, i, "UnmarshalText() []byte")
var blank JSON
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullJSON(t, blank, "UnmarshalText() empty []byte")
}
func TestMarshalJSON(t *testing.T) {
i := JSONFrom([]byte(`"hello"`))
data, err := json.Marshal(i)
maybePanic(err)
assertJSONEquals(t, data, `"hello"`, "non-empty json marshal")
// invalid values should be encoded as null
null := NewJSON(nil, false)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, "null", "null json marshal")
}
func TestMarshalJSONText(t *testing.T) {
i := JSONFrom([]byte(`"hello"`))
data, err := i.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, `"hello"`, "non-empty text marshal")
// invalid values should be encoded as null
null := NewJSON(nil, false)
data, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "", "null text marshal")
}
func TestJSONPointer(t *testing.T) {
i := JSONFrom([]byte(`"hello"`))
ptr := i.Ptr()
if !bytes.Equal(*ptr, []byte(`"hello"`)) {
t.Errorf("bad %s []byte: %#v ≠ %s\n", "pointer", ptr, `"hello"`)
}
null := NewJSON(nil, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s []byte: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestJSONIsZero(t *testing.T) {
i := JSONFrom([]byte(`"hello"`))
if i.IsZero() {
t.Errorf("IsZero() should be false")
}
null := NewJSON(nil, false)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
zero := NewJSON(nil, true)
if zero.IsZero() {
t.Errorf("IsZero() should be false")
}
}
func TestJSONSetValid(t *testing.T) {
change := NewJSON(nil, false)
assertNullJSON(t, change, "SetValid()")
change.SetValid([]byte(`"hello"`))
assertJSON(t, change, "SetValid()")
}
func TestJSONScan(t *testing.T) {
var i JSON
err := i.Scan(`"hello"`)
maybePanic(err)
assertJSON(t, i, "scanned []byte")
var null JSON
err = null.Scan(nil)
maybePanic(err)
assertNullJSON(t, null, "scanned null")
}
func assertJSON(t *testing.T, i JSON, from string) {
if !bytes.Equal(i.JSON, []byte(`"hello"`)) {
t.Errorf("bad %s []byte: %#v ≠ %#v\n", from, string(i.JSON), string([]byte(`"hello"`)))
}
if !i.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullJSON(t *testing.T, i JSON, from string) {
if i.Valid {
t.Error(from, "is valid, but should be invalid")
}
}