Skip to content

Commit 043f53c

Browse files
committed
initial commit
0 parents  commit 043f53c

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage.out

LICENSE

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Copyright (c) 2014, Greg Roseberry
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
8+
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.
9+
10+
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.

README.md

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

string.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package null
2+
3+
import (
4+
"database/sql"
5+
"encoding/json"
6+
)
7+
8+
type String struct {
9+
sql.NullString
10+
}
11+
12+
func StringFrom(s string) String {
13+
return String{
14+
NullString: sql.NullString{
15+
String: s,
16+
Valid: s != "",
17+
},
18+
}
19+
}
20+
21+
func (s *String) UnmarshalJSON(data []byte) error {
22+
var err error
23+
var v interface{}
24+
json.Unmarshal(data, &v)
25+
switch v.(type) {
26+
case string:
27+
err = json.Unmarshal(data, &s.String)
28+
case map[string]interface{}:
29+
err = json.Unmarshal(data, &s.NullString)
30+
case nil:
31+
s.Valid = false
32+
return nil
33+
}
34+
s.Valid = err == nil
35+
return err
36+
}
37+
38+
func (s String) MarshalJSON() ([]byte, error) {
39+
return json.Marshal(s.String)
40+
}
41+
42+
func (s String) Pointer() *string {
43+
if s.String == "" {
44+
return nil
45+
}
46+
return &s.String
47+
}

string_test.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package null
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
var (
9+
stringJSON = []byte(`"test"`)
10+
nullStringJSON = []byte(`{"String":"test","Valid":true}`)
11+
nullJSON = []byte(`null`)
12+
)
13+
14+
func TestStringFrom(t *testing.T) {
15+
str := From("test")
16+
assert(t, str, "From() string")
17+
18+
null := From("")
19+
assertNull(t, null, "From() empty string")
20+
}
21+
22+
func TestUnmarshalString(t *testing.T) {
23+
var str String
24+
err := json.Unmarshal(stringJSON, &str)
25+
maybePanic(err)
26+
assert(t, str, "string json")
27+
28+
var ns String
29+
err = json.Unmarshal(nullStringJSON, &ns)
30+
maybePanic(err)
31+
assert(t, ns, "null string object json")
32+
33+
var null String
34+
err = json.Unmarshal(nullJSON, &null)
35+
maybePanic(err)
36+
assertNull(t, null, "null json")
37+
}
38+
39+
func TestMarshalString(t *testing.T) {
40+
str := From("test")
41+
data, err := json.Marshal(str)
42+
maybePanic(err)
43+
assertJSONEquals(t, data, `"test"`, "non-empty json marshal")
44+
45+
// invalid values should be encoded as an empty string
46+
null := From("")
47+
data, err = json.Marshal(null)
48+
maybePanic(err)
49+
assertJSONEquals(t, data, `""`, "non-empty json marshal")
50+
}
51+
52+
func TestPointer(t *testing.T) {
53+
str := From("test")
54+
ptr := str.Pointer()
55+
if *ptr != "test" {
56+
t.Errorf("bad %s string: %#v ≠ %s\n", "pointer", ptr, "test")
57+
}
58+
59+
null := From("")
60+
ptr = null.Pointer()
61+
if ptr != nil {
62+
t.Errorf("bad %s: %#v ≠ %s\n", "nil pointer", ptr, "nil")
63+
}
64+
}
65+
66+
func TestScan(t *testing.T) {
67+
var str String
68+
err := str.Scan("test")
69+
maybePanic(err)
70+
assert(t, str, "scanned string")
71+
72+
var null String
73+
err = null.Scan(nil)
74+
maybePanic(err)
75+
assertNull(t, null, "scanned null")
76+
}
77+
78+
func maybePanic(err error) {
79+
if err != nil {
80+
panic(err)
81+
}
82+
}
83+
84+
func assert(t *testing.T, s String, from string) {
85+
if s.String != "test" {
86+
t.Errorf("bad %s string: %s ≠ %s\n", from, s.String, "test")
87+
}
88+
if !s.Valid {
89+
t.Error(from, "is invalid, but should be valid")
90+
}
91+
}
92+
93+
func assertNull(t *testing.T, s String, from string) {
94+
if s.Valid {
95+
t.Error(from, "is valid, but should be invalid")
96+
}
97+
}
98+
99+
func assertJSONEquals(t *testing.T, data []byte, cmp string, from string) {
100+
if string(data) != cmp {
101+
t.Errorf("bad %s data: %s ≠ %s\n", from, data, cmp)
102+
}
103+
}

0 commit comments

Comments
 (0)