https://github.com/toru/nullable
Productive syntax for the nullable data types in Go's standard database/sql package. ActiveSupport inspired.
https://github.com/toru/nullable
activesupport database go null
Last synced: 26 days ago
JSON representation
Productive syntax for the nullable data types in Go's standard database/sql package. ActiveSupport inspired.
- Host: GitHub
- URL: https://github.com/toru/nullable
- Owner: toru
- License: mit
- Created: 2023-03-28T02:40:32.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-21T03:38:38.000Z (about 2 years ago)
- Last Synced: 2024-03-21T04:35:02.697Z (about 2 years ago)
- Topics: activesupport, database, go, null
- Language: Go
- Homepage: https://pkg.go.dev/github.com/toru/nullable
- Size: 50.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nullable
[](https://github.com/toru/nullable/actions/workflows/go.yml)
[](https://pkg.go.dev/github.com/toru/nullable)
[](/LICENSE)
The nullable package provides an unobtrusive, [Active Support](https://guides.rubyonrails.org/active_support_core_extensions.html)
inspired syntax on top of Go's standard [database/sql](https://github.com/golang/go/tree/master/src/database/sql) types.
Because nullable wraps the standard package, it is a simple drop-in replacement.
## Quick Examples
Usage within struct definition.
```go
type IceCream struct {
// Using the nullable package
Flavor nullable.String `db:"flavor"`
// Using the database/sql package
Topping sql.NullString `db:"topping"`
}
```
### String
#### Initialization
```go
// Using the nullable package
nullable.NewString("hello")
// Equivalent using the standard type
sql.NullString{String: "hello", Valid: true}
```
#### Mutation
```go
// Overwrite the existing value
value.Set("hello there")
```
#### Checking State
```go
// Null() is also available
if value.Nil() {
log.Println("stored as NULL in the database")
}
if value.Present() {
log.Println("non-empty string")
}
if value.Empty() {
log.Println("either NULL or empty string")
}
```
#### Utilities
```go
// Hexadecimal string representation of the underlying value
value.HexString()
```
### Binary
#### Initialization
```go
nullable.NewBinary([]byte("hello"))
```
#### Checking State
```go
if value.Nil() {
log.Println("stored as NULL in the database")
}
if value.Present() {
log.Println("non-empty byte slice")
}
```
#### Utilities
```go
// Hexadecimal string representation of the underlying bytes
value.HexString()
```
For all available types, see the [package documentation](https://pkg.go.dev/github.com/toru/nullable).
## Motivation
Go's standard library provides precisely what it should: well-thought-out
primitive building blocks. This package scratches the itch of those who avoid
ORMs but still miss the ergonomics of Rails when handling nullable values.
## Contributing
Contributions of any kind are welcome! If you are going to submit a PR, please
follow [Go's commit message structure](https://go.dev/wiki/CommitMessage).