|
| 1 | +# oapi-codegen/nullable |
| 2 | + |
| 3 | +> An implementation of a `Nullable` type for JSON bodies, indicating whether the field is absent, set to null, or set to a value |
| 4 | +
|
| 5 | +Unlike other known implementations, this makes it possible to both marshal and unmarshal the value, as well as represent all three states: |
| 6 | + |
| 7 | +- the field is _not set_ |
| 8 | +- the field is _explicitly set to null_ |
| 9 | +- the field is _explicitly set to a given value_ |
| 10 | + |
| 11 | +And can be embedded in structs, for instance with the following definition: |
| 12 | + |
| 13 | +```go |
| 14 | +obj := struct { |
| 15 | + // RequiredID is a required, nullable field |
| 16 | + RequiredID nullable.Nullable[int] `json:"id"` |
| 17 | + // RequiredID is an optional, nullable field |
| 18 | + OptionalString *nullable.Nullable[string] `json:"optionalString,omitempty"` |
| 19 | +}{} |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +> [!IMPORTANT] |
| 25 | +> 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. |
| 26 | +
|
| 27 | +First, add to your project with: |
| 28 | + |
| 29 | +```sh |
| 30 | +go get github.com/oapi-codegen/nullable |
| 31 | +``` |
| 32 | + |
| 33 | +Check out the examples in [the package documentation on pkg.go.dev](https://pkg.go.dev/github.com/oapi-codegen/nullable) for more details. |
| 34 | + |
| 35 | +## Credits |
| 36 | + |
| 37 | +- [KumanekoSakura](https://github.com/KumanekoSakura), [via](https://github.com/golang/go/issues/64515#issuecomment-1841057182) |
| 38 | +- [Sebastien Guilloux], [via](https://github.com/sebgl/nullable/) |
| 39 | + |
| 40 | +As well as contributions from: |
| 41 | + |
| 42 | +- [Jamie Tanna](https://www.jvt.me) |
| 43 | +- [Ashutosh Kumar](https://github.com/sonasingh46) |
| 44 | + |
| 45 | +## License |
| 46 | + |
| 47 | +Licensed under the Apache-2.0 license. |
0 commit comments