initial commit

This commit is contained in:
Greg 2014-08-29 00:11:18 +09:00
commit 043f53c9b4
5 changed files with 164 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
coverage.out

10
LICENSE Normal file
View file

@ -0,0 +1,10 @@
Copyright (c) 2014, Greg Roseberry
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

3
README.md Normal file
View file

@ -0,0 +1,3 @@
null is a library with opinions on how to deal with nullable SQL and JSON values
BSD licensed.

47
string.go Normal file
View file

@ -0,0 +1,47 @@
package null
import (
"database/sql"
"encoding/json"
)
type String struct {
sql.NullString
}
func StringFrom(s string) String {
return String{
NullString: sql.NullString{
String: s,
Valid: s != "",
},
}
}
func (s *String) UnmarshalJSON(data []byte) error {
var err error
var v interface{}
json.Unmarshal(data, &v)
switch v.(type) {
case string:
err = json.Unmarshal(data, &s.String)
case map[string]interface{}:
err = json.Unmarshal(data, &s.NullString)
case nil:
s.Valid = false
return nil
}
s.Valid = err == nil
return err
}
func (s String) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String)
}
func (s String) Pointer() *string {
if s.String == "" {
return nil
}
return &s.String
}

103
string_test.go Normal file
View file

@ -0,0 +1,103 @@
package null
import (
"encoding/json"
"testing"
)
var (
stringJSON = []byte(`"test"`)
nullStringJSON = []byte(`{"String":"test","Valid":true}`)
nullJSON = []byte(`null`)
)
func TestStringFrom(t *testing.T) {
str := From("test")
assert(t, str, "From() string")
null := From("")
assertNull(t, null, "From() empty string")
}
func TestUnmarshalString(t *testing.T) {
var str String
err := json.Unmarshal(stringJSON, &str)
maybePanic(err)
assert(t, str, "string json")
var ns String
err = json.Unmarshal(nullStringJSON, &ns)
maybePanic(err)
assert(t, ns, "null string object json")
var null String
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNull(t, null, "null json")
}
func TestMarshalString(t *testing.T) {
str := From("test")
data, err := json.Marshal(str)
maybePanic(err)
assertJSONEquals(t, data, `"test"`, "non-empty json marshal")
// invalid values should be encoded as an empty string
null := From("")
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, `""`, "non-empty json marshal")
}
func TestPointer(t *testing.T) {
str := From("test")
ptr := str.Pointer()
if *ptr != "test" {
t.Errorf("bad %s string: %#v ≠ %s\n", "pointer", ptr, "test")
}
null := From("")
ptr = null.Pointer()
if ptr != nil {
t.Errorf("bad %s: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestScan(t *testing.T) {
var str String
err := str.Scan("test")
maybePanic(err)
assert(t, str, "scanned string")
var null String
err = null.Scan(nil)
maybePanic(err)
assertNull(t, null, "scanned null")
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
func assert(t *testing.T, s String, from string) {
if s.String != "test" {
t.Errorf("bad %s string: %s ≠ %s\n", from, s.String, "test")
}
if !s.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNull(t *testing.T, s String, from string) {
if s.Valid {
t.Error(from, "is valid, but should be invalid")
}
}
func assertJSONEquals(t *testing.T, data []byte, cmp string, from string) {
if string(data) != cmp {
t.Errorf("bad %s data: %s ≠ %s\n", from, data, cmp)
}
}