You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
null is a library with reasonable options for dealing with nullable SQL and JSON values
5
5
@@ -9,20 +9,23 @@ Types in `null` will only be considered null on null input, and will JSON encode
9
9
10
10
Types in `zero` are treated like zero values in Go: blank string input will produce a null `zero.String`, and null Strings will JSON encode to `""`. Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these.
11
11
12
-
All types implement `sql.Scanner` and `driver.Valuer`, so you can use this library in place of `sql.NullXXX`.
13
-
All types also implement: `encoding.TextMarshaler`, `encoding.TextUnmarshaler`, `json.Marshaler`, and `json.Unmarshaler`. A null object's `MarshalText` will return a blank string.
12
+
#### Interfaces
14
13
15
-
### null package
14
+
- All types implement `sql.Scanner` and `driver.Valuer`, so you can use this library in place of `sql.NullXXX`.
15
+
- All types also implement `json.Marshaler` and `json.Unmarshaler`, so you can marshal them to their native JSON representation.
16
+
- All non-generic types implement `encoding.TextMarshaler`, `encoding.TextUnmarshaler`. A null object's `MarshalText` will return a blank string.
16
17
17
-
`import "gopkg.in/guregu/null.v4"`
18
+
## null package
19
+
20
+
`import "github.com/guregu/null/v5"`
18
21
19
22
#### null.String
20
23
Nullable string.
21
24
22
25
Marshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String.
23
26
24
-
#### null.Int
25
-
Nullable int64.
27
+
#### null.Int, null.Int32, null.Int16, null.Byte
28
+
Nullable int64/int32/int16/byte.
26
29
27
30
Marshals to JSON null if SQL source data is null. Zero input will not produce a null Int.
28
31
@@ -40,17 +43,22 @@ Marshals to JSON null if SQL source data is null. False input will not produce a
40
43
41
44
Marshals to JSON null if SQL source data is null. Zero input will not produce a null Time.
42
45
43
-
### zero package
46
+
#### null.Value
47
+
Generic nullable value.
48
+
49
+
Will marshal to JSON null if SQL source data is null. Does not implement `encoding.TextMarshaler`.
50
+
51
+
## zero package
44
52
45
-
`import "gopkg.in/guregu/null.v4/zero"`
53
+
`import "github.com/guregu/null/v5/zero"`
46
54
47
55
#### zero.String
48
56
Nullable string.
49
57
50
58
Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent.
51
59
52
-
#### zero.Int
53
-
Nullable int64.
60
+
#### zero.Int, zero.Int32, zero.Int16, zero.Byte
61
+
Nullable int64/int32/int16/byte.
54
62
55
63
Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent.
56
64
@@ -65,17 +73,35 @@ Nullable bool.
65
73
Will marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent.
66
74
67
75
#### zero.Time
76
+
Nullable time.
68
77
69
78
Will marshal to the zero time if null. Uses `time.Time`'s marshaler.
70
79
71
-
### Can you add support for other types?
80
+
#### zero.Value[`T`]
81
+
Generic nullable value.
82
+
83
+
Will marshal to zero value if null. `T` is required to be a comparable type. Does not implement `encoding.TextMarshaler`.
84
+
85
+
## About
86
+
87
+
### Q&A
88
+
89
+
#### Can you add support for other types?
72
90
This package is intentionally limited in scope. It will only support the types that [`driver.Value`](https://godoc.org/database/sql/driver#Value) supports. Feel free to fork this and add more types if you want.
73
91
74
-
### Can you add a feature that ____?
92
+
####Can you add a feature that ____?
75
93
This package isn't intended to be a catch-all data-wrangling package. It is essentially finished. If you have an idea for a new feature, feel free to open an issue to talk about it or fork this package, but don't expect this to do everything.
76
94
77
95
### Package history
78
-
*As of v4*, unmarshaling from JSON `sql.NullXXX` JSON objects (ex. `{"Int64": 123, "Valid": true}`) is no longer supported. It's unlikely many people used this, but if you need it, use v3.
96
+
97
+
#### v5
98
+
- Now a Go module under the path `github.com/guregu/null/v5`
99
+
- Added missing types from `database/sql`: `Int32, Int16, Byte`
- Unmarshaling from JSON `sql.NullXXX` JSON objects (e.g. `{"Int64": 123, "Valid": true}`) is no longer supported. It's unlikely many people used this, but if you need it, use v3.
79
105
80
106
### Bugs
81
107
`json`'s `",omitempty"` struct tag does not work correctly right now. It will never omit a null or empty String. This might be [fixed eventually](https://github.com/golang/go/issues/11939).
0 commit comments