Skip to content

Commit 3d3e4d9

Browse files
committed
Merge branch 'origin/master'
2 parents 59e018b + 3050386 commit 3d3e4d9

File tree

3 files changed

+64
-77
lines changed

3 files changed

+64
-77
lines changed

README.md

+47-77
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,61 @@
1-
## null-extended [![GoDoc](https://godoc.org/github.com/volatiletech/null?status.svg)](https://godoc.org/github.com/volatiletech/null) [![Coverage](http://gocover.io/_badge/github.com/volatiletech/null)](http://gocover.io/github.com/volatiletech/null)
1+
## null [![GoDoc](https://godoc.org/github.com/volatiletech/null?status.svg)](https://godoc.org/github.com/volatiletech/null) [![Coverage](http://gocover.io/_badge/github.com/volatiletech/null)](http://gocover.io/github.com/volatiletech/null)
22

3-
null-extended is a library with reasonable options for dealing with nullable SQL and JSON values
3+
`null` is a library with reasonable options for dealing with nullable SQL and
4+
JSON values.
45

5-
Types in `null` will only be considered null on null input, and will JSON encode to `null`.
6+
Types in `null` will only be considered null on null input, and will JSON
7+
encode to `null`.
68

7-
All types implement `sql.Scanner` and `driver.Valuer`, so you can use this library in place of `sql.NullXXX`. All types also implement: `encoding.TextMarshaler`, `encoding.TextUnmarshaler`, `json.Marshaler`, `json.Unmarshaler` and `sql.Scanner`.
9+
All types implement `sql.Scanner` and `driver.Valuer`, so you can use this
10+
library in place of `sql.NullXXX`. All types also implement:
11+
`encoding.TextMarshaler`, `encoding.TextUnmarshaler`, `json.Marshaler`,
12+
`json.Unmarshaler` and `sql.Scanner`.
813

914
---
1015

11-
Install:
16+
### Installation
1217

1318
Null used to be versioned with gopkg.in, so once you upgrade to v8 and beyond
14-
please stop using gopkg.in and ensure you're using `vgo`, `dep` or vendoring
15-
to version null.
19+
please stop using gopkg.in and ensure you're using `vgo`, `dep` or vendoring to
20+
version null.
21+
22+
```
23+
go get -u "github.com/volatiletech/null"
24+
```
25+
26+
### Usage
27+
28+
The following are all types supported in this package. All types will marshal
29+
to JSON null if Invalid or SQL source data is null.
30+
31+
| Type | Description | Notes |
32+
|------|-------------|-------|
33+
| `null.JSON` | Nullable `[]byte` | Will marshal to JSON null if Invalid. `[]byte{}` input will not produce an Invalid JSON, but `[]byte(nil)` will. This should be used for storing raw JSON in the database. Also has `null.JSON.Marshal` and `null.JSON.Unmarshal` helpers to marshal and unmarshal foreign objects. |
34+
| `null.Bytes` | Nullable `[]byte` | `[]byte{}` input will not produce an Invalid Bytes, but `[]byte(nil)` will. This should be used for storing binary data (bytes in PSQL for example) in the database. |
35+
| `null.String` | Nullable `string` | |
36+
| `null.Byte` | Nullable `byte` | |
37+
| `null.Bool` | Nullable `bool` | |
38+
| `null.Time` | Nullable `time.Time | Marshals to JSON null if SQL source data is null. Uses `time.Time`'s marshaler. |
39+
| `null.Float32` | Nullable `float32` | |
40+
| `null.Float64` | Nullable `float64` | |
41+
| `null.Int` | Nullable `int` | |
42+
| `null.Int8` | Nullable `int8` | |
43+
| `null.Int16` | Nullable `int16` | |
44+
| `null.Int32` | Nullable `int32` | |
45+
| `null.Int64` | Nullable `int64` | |
46+
| `null.Uint` | Nullable `uint` | |
47+
| `null.Uint8` | Nullable `uint8` | |
48+
| `null.Uint16` | Nullable `uint16` | |
49+
| `null.Uint32` | Nullable `int32` | |
50+
| `null.Int64` | Nullable `uint64` | | |
1651

17-
`go get -u "github.com/volatiletech/null"`
18-
19-
### null package
20-
21-
`import "github.com/volatiletech/null"`
22-
23-
The following are all types supported in this package. All types will marshal to JSON null if Invalid or SQL source data is null.
24-
25-
#### null.JSON
26-
Nullable []byte.
27-
28-
Will marshal to JSON null if Invalid. []byte{} input will not produce an Invalid JSON, but []byte(nil) will. This should be used for storing raw JSON in the database.
29-
30-
Also has `null.JSON.Marshal` and `null.JSON.Unmarshal` helpers to marshal and unmarshal foreign objects.
31-
32-
#### null.Bytes
33-
Nullable []byte.
34-
35-
[]byte{} input will not produce an Invalid Bytes, but []byte(nil) will. This should be used for storing binary data (bytea in PSQL for example) in the database.
36-
37-
#### null.String
38-
Nullable string.
39-
40-
#### null.Byte
41-
Nullable byte.
42-
43-
#### null.Bool
44-
Nullable bool.
45-
46-
#### null.Time
47-
Nullable time.Time
48-
49-
Marshals to JSON null if SQL source data is null. Uses `time.Time`'s marshaler.
50-
51-
#### null.Float32
52-
Nullable float32.
53-
54-
#### null.Float64
55-
Nullable float64.
56-
57-
#### null.Int
58-
Nullable int.
59-
60-
#### null.Int8
61-
Nullable int8.
62-
63-
#### null.Int16
64-
Nullable int16.
65-
66-
#### null.Int32
67-
Nullable int32.
68-
69-
#### null.Int64
70-
Nullable int64.
71-
72-
#### null.Uint
73-
Nullable uint.
74-
75-
#### null.Uint8
76-
Nullable uint8.
77-
78-
#### null.Uint16
79-
Nullable uint16.
80-
81-
#### null.Uint32
82-
Nullable int32.
52+
### Bugs
8353

84-
#### null.Int64
85-
Nullable uint64.
54+
`json`'s `",omitempty"` struct tag does not work correctly right now. It will
55+
never omit a null or empty String. This might be [fixed
56+
eventually](https://github.com/golang/go/issues/4357).
8657

87-
### Bugs
88-
`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/4357).
8958

9059
### License
60+
9161
BSD

time.go

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (t Time) Ptr() *time.Time {
9595
return &t.Time
9696
}
9797

98+
// IsZero returns true for an invalid Time's value, for potential future omitempty support.
99+
func (t Time) IsZero() bool {
100+
return !t.Valid
101+
}
102+
98103
// Scan implements the Scanner interface.
99104
func (t *Time) Scan(value interface{}) error {
100105
var err error

time_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ func TestTimePointer(t *testing.T) {
114114
}
115115
}
116116

117+
func TestTimeIsZero(t *testing.T) {
118+
ti := TimeFrom(time.Now())
119+
if ti.IsZero() {
120+
t.Errorf("IsZero() should be false")
121+
}
122+
123+
null := TimeFromPtr(nil)
124+
if !null.IsZero() {
125+
t.Errorf("IsZero() should be true")
126+
}
127+
}
128+
117129
func TestTimeScanValue(t *testing.T) {
118130
var ti Time
119131
err := ti.Scan(timeValue)

0 commit comments

Comments
 (0)