Instead of using pointers for optional or nullable fields, ogen generates generic wrapper types. This avoids pointer indirection and provides clearer semantics for the three states of a field:
- Optional: The field may be absent from the payload.
- Nullable: The field may be present but have a
null value. - Optional and Nullable: The field may be absent OR present as
null.
Commonly generated wrappers include Optional[T], Nullable[T], and OptionalNullable[T] (e.g., OptNilString).
Example: OptNilString
An OptNilString represents a string that is both optional and nullable.
type OptNilString struct {
Value string
Set bool
Null bool
}
Helper Methods
Generated wrappers include several convenience methods:
Get() (v T, ok bool): Returns the value and a boolean indicating if it was set.IsNull() bool: Returns true if the value is null.IsSet() bool: Returns true if the value was present in the payload.IsEmpty() bool: Returns true if the value is the zero value.New[Type](v T): A constructor function (e.g., NewOptNilString(v string)).
// OptNilString is optional nullable string.
type OptNilString struct {
Value string
Set bool
Null bool
}
func (OptNilString) Get() (v string, ok bool)
func (OptNilString) IsNull() bool
func (OptNilString) IsSet() bool
func (OptNilString) IsEmpty() bool
func NewOptNilString(v string) OptNilString