lbry.go/README.md

37 lines
2 KiB
Markdown
Raw Normal View History

2014-08-31 10:28:34 +02:00
## null [![GoDoc](https://godoc.org/github.com/guregu/null?status.svg)](https://godoc.org/github.com/guregu/null) [![Coverage](http://gocover.io/_badge/github.com/guregu/null)](http://gocover.io/github.com/guregu/null)
2014-08-28 17:11:18 +02:00
null is a library with opinions on how to deal with nullable SQL and JSON values
2014-08-31 10:28:34 +02:00
There are two packages, `null`, and `nuller`.
2014-08-31 10:30:01 +02:00
2014-08-31 10:28:34 +02:00
Types in `null` are treated like zero values in Go: blank string input will produce a null `null.String`, and null Strings will JSON encode to `""`. If you need zero and null treated the same, use these.
2014-08-31 10:30:01 +02:00
2014-08-31 10:28:34 +02:00
Types in `nuller` will only be considered null on null input, and will JSON encode to `null`. If you need zero and null be considered separate values, use these.
2014-08-31 10:30:01 +02:00
2014-08-31 10:36:30 +02:00
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`, and `json.Unmarshaler`.
2014-08-31 10:28:34 +02:00
2014-08-31 10:38:25 +02:00
#### null.String
2014-08-31 10:36:30 +02:00
A nullable string.
2014-08-29 03:29:17 +02:00
2014-08-31 10:28:34 +02:00
Will marshal to a blank string if null. Blank string input produces a null String. In other words, null values and empty values are considered equivalent. Can unmarshal from `sql.NullString` JSON input.
2014-08-31 10:38:25 +02:00
#### null.Int
2014-08-31 10:28:34 +02:00
A nullable int64.
Will marshal to 0 if null. Blank string or 0 input produces a null Int. In other words, null values and empty values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input.
2014-08-29 03:29:17 +02:00
2014-08-31 10:38:25 +02:00
#### nuller.String
2014-08-31 10:28:34 +02:00
An even nuller nullable string.
2014-08-29 03:29:17 +02:00
2014-08-31 10:28:34 +02:00
Unlike `null.String`, `nuller.String` will marshal to null if null. Zero (blank) input will not produce a null String. Can unmarshal from `sql.NullString` JSON input.
2014-08-30 17:03:04 +02:00
2014-08-31 10:38:25 +02:00
#### nuller.Int
2014-08-31 10:28:34 +02:00
An even nuller nullable int64.
2014-08-30 17:03:04 +02:00
2014-08-31 10:28:34 +02:00
Unlike `null.Int`, `nuller.Int` will marshal to null if null. Zero input will not produce a null Int. Can unmarshal from `sql.NullInt64` JSON input.
2014-08-30 17:03:04 +02:00
2014-08-29 03:29:17 +02:00
### Bugs
`json`'s `",omitempty"` struct tag does not work correctly right now. It will never omit a null or empty String. This should be [fixed in Go 1.4](https://code.google.com/p/go/issues/detail?id=4357).
### License
BSD