Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oapi-codegen/nullable
An implementation of a `Nullable` type for JSON bodies, indicating whether the field is absent, set to null, or set to a value
https://github.com/oapi-codegen/nullable
Last synced: 6 days ago
JSON representation
An implementation of a `Nullable` type for JSON bodies, indicating whether the field is absent, set to null, or set to a value
- Host: GitHub
- URL: https://github.com/oapi-codegen/nullable
- Owner: oapi-codegen
- License: other
- Created: 2024-01-09T09:33:36.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-09-10T02:44:28.000Z (3 months ago)
- Last Synced: 2024-10-19T10:59:50.938Z (2 months ago)
- Language: Go
- Size: 14.6 KB
- Stars: 99
- Watchers: 3
- Forks: 6
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# oapi-codegen/nullable
> An implementation of a `Nullable` type for JSON bodies, indicating whether the field is absent, set to null, or set to a value
Unlike other known implementations, this makes it possible to both marshal and unmarshal the value, as well as represent all three states:
- the field is _not set_
- the field is _explicitly set to null_
- the field is _explicitly set to a given value_And can be embedded in structs, for instance with the following definition:
```go
obj := struct {
// RequiredID is a required, nullable field
RequiredID nullable.Nullable[int] `json:"id"`
// OptionalString is an optional, nullable field
// NOTE that no pointer is required, only `omitempty`
OptionalString nullable.Nullable[string] `json:"optionalString,omitempty"`
}{}
```## Usage
> [!IMPORTANT]
> Although this project is under the [oapi-codegen org](https://github.com/oapi-codegen) for the `oapi-codegen` OpenAPI-to-Go code generator, this is intentionally released as a separate, standalone library which can be used by other projects.First, add to your project with:
```sh
go get github.com/oapi-codegen/nullable
```Check out the examples in [the package documentation on pkg.go.dev](https://pkg.go.dev/github.com/oapi-codegen/nullable) for more details.
## Credits
- [KumanekoSakura](https://github.com/KumanekoSakura), [via](https://github.com/golang/go/issues/64515#issuecomment-1842973794)
- [Sebastien Guilloux](https://github.com/sebgl), [via](https://github.com/sebgl/nullable/)As well as contributions from:
- [Jamie Tanna](https://www.jvt.me)
- [Ashutosh Kumar](https://github.com/sonasingh46)## License
Licensed under the Apache-2.0 license.