Skip to content

Commit 4f91fe4

Browse files
committed
Add all additional types to zero package
* Add all additional float, int and uint types to zero package * Moved convert to its own package * Updated README to reflect changes
1 parent bbb7b84 commit 4f91fe4

38 files changed

+4175
-277
lines changed

README.md

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,74 @@ Nullable string.
9898

9999
Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullString` JSON input.
100100

101+
#### zero.Bool
102+
Nullable bool.
103+
104+
Will marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullBool` JSON input.
105+
106+
#### zero.Time
107+
108+
Will marshal to the zero time if null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input.
109+
110+
#### zero.Float32
111+
Nullable float32.
112+
113+
Will marshal to 0 if null. 0.0 produces a null Float32. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullFloat32` JSON input.
114+
115+
#### zero.Float64
116+
Nullable float64.
117+
118+
Will marshal to 0 if null. 0.0 produces a null Float64. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input.
119+
101120
#### zero.Int
121+
Nullable int.
122+
123+
Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt` JSON input.
124+
125+
#### zero.Int8
126+
Nullable int8.
127+
128+
Will marshal to 0 if null. 0 produces a null Int8. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt8` JSON input.
129+
130+
#### zero.Int16
131+
Nullable int16.
132+
133+
Will marshal to 0 if null. 0 produces a null Int16. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt16` JSON input.
134+
135+
#### zero.Int32
136+
Nullable int32.
137+
138+
Will marshal to 0 if null. 0 produces a null Int32. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullInt32` JSON input.
139+
140+
#### zero.Int64
102141
Nullable int64.
103142

104-
Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input.
143+
Will marshal to 0 if null. 0 produces a null Int64. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input.
105144

106-
#### zero.Float
107-
Nullable float64.
145+
#### zero.Uint
146+
Nullable uint.
108147

109-
Will marshal to 0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input.
148+
Will marshal to 0 if null. 0 produces a null Uint. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint` JSON input.
110149

111-
#### zero.Bool
112-
Nullable bool.
150+
#### zero.Uint8
151+
Nullable uint8.
113152

114-
Will marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullBool` JSON input.
153+
Will marshal to 0 if null. 0 produces a null Uint8. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint8` JSON input.
115154

116-
#### zero.Time
155+
#### zero.Uint16
156+
Nullable uint16.
117157

118-
Will marshal to the zero time if null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input.
158+
Will marshal to 0 if null. 0 produces a null Uint16. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint16` JSON input.
159+
160+
#### zero.Uint32
161+
Nullable uint32.
162+
163+
Will marshal to 0 if null. 0 produces a null Uint32. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint32` JSON input.
164+
165+
#### zero.Uint64
166+
Nullable uint64.
119167

168+
Will marshal to 0 if null. 0 produces a null Uint64. Null values and zero values are considered equivalent. Can unmarshal from `zero.NullUint64` JSON input.
120169

121170
### Bugs
122171
`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).

convert.go renamed to convert/convert.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package null
1+
package convert
22

33
// Copyright 2011 The Go Authors. All rights reserved.
44
// Use of this source code is governed by a BSD-style
@@ -19,10 +19,10 @@ import (
1919

2020
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
2121

22-
// convertAssign copies to dest the value in src, converting it if possible.
22+
// ConvertAssign copies to dest the value in src, converting it if possible.
2323
// An error is returned if the copy would result in loss of information.
2424
// dest should be a pointer type.
25-
func convertAssign(dest, src interface{}) error {
25+
func ConvertAssign(dest, src interface{}) error {
2626
// Common cases, without reflect.
2727
switch s := src.(type) {
2828
case string:
@@ -172,7 +172,7 @@ func convertAssign(dest, src interface{}) error {
172172
return nil
173173
} else {
174174
dv.Set(reflect.New(dv.Type().Elem()))
175-
return convertAssign(dv.Interface(), src)
175+
return ConvertAssign(dv.Interface(), src)
176176
}
177177
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
178178
s := asString(src)

0 commit comments

Comments
 (0)