https://github.com/gomoni/null
Go null and undefined aware type wrapper
https://github.com/gomoni/null
generics go golang json
Last synced: 6 months ago
JSON representation
Go null and undefined aware type wrapper
- Host: GitHub
- URL: https://github.com/gomoni/null
- Owner: gomoni
- License: bsd-3-clause
- Created: 2021-12-17T14:38:31.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-03T08:29:54.000Z (over 4 years ago)
- Last Synced: 2024-06-20T16:52:03.975Z (about 2 years ago)
- Topics: generics, go, golang, json
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://opensource.org/licenses/BSD-3-Clause)
# null
null is a Go library providing an easy to use interface to handle `null` value or missing key. `Type[T any]` implements `json.Marshaler` and `json.Unmarshaler` . Because of generics it works for any type which can be unmarshalled from/marshalled into JSON.
Marshalling of undefined is an error though.
# Usage
```go
// define struct with a wrapper type
var s struct {
Key Type[int] `json:"key"`
}
// unmarshal as usual
err := json.Unmarshal(data, &s)
// access value via Value() method, which returns error for null and undefined cases
value, err := s.Key.Value()
```
| JSON | Value | Error |
|----------------|--------|--------------|
|`{"key": 42}` | `42` | `nil` |
|`{"key": null}` | _zero_ | ErrNull |
|`{}` | _zero_ | ErrUndefined |
# Build and test
install gotip (until go 1.18 release)
```bash
go install golang.org/dl/gotip@latest
gotip download
gotip test -v
```