Documentation
¶
Index ¶
- Constants
- func IsNullPtr[T any](ptr *T) bool
- func IsNullSlice[T any](slice []T) bool
- func IsOmitted(v any) bool
- func MarshalObject[T OverridableObject](f T, underlying any) ([]byte, error)
- func MarshalUnion[T any](variants ...any) ([]byte, error)
- func NullObj[T NullableObject, PT Settable[T]]() T
- func NullPtr[T any]() *T
- func NullSlice[T any]() []T
- func OverrideObj[T OverridableObject, PT Settable[T]](v any) T
- func VariantFromUnion(u reflect.Value) (any, error)
- type APIObject
- type APIUnion
- type EncodedAsDate
- type NullableObject
- type Opt
- func (o Opt[T]) IsNull() bool
- func (o Opt[T]) IsOmitted() bool
- func (o Opt[T]) IsPresent() bool
- func (o Opt[T]) MarshalJSON() ([]byte, error)
- func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte
- func (o Opt[T]) Or(v T) T
- func (o Opt[T]) String() string
- func (o *Opt[T]) UnmarshalJSON(data []byte) error
- type Optional
- type OverridableObject
- type Settable
- type Status
Constants ¶
const Omit forceOmit = -1
Omit can be used with [metadata.WithExtraFields] to ensure that a required field is omitted. This is useful as an escape hatch for when a required is unwanted for some unexpected reason.
Variables ¶
This section is empty.
Functions ¶
func IsNullSlice ¶
IsNullSlice returns true if the slice was created by NullSlice.
func IsOmitted ¶
IsOmitted returns true if v is the zero value of its type.
It indicates if a field with the `json:"...,omitzero"` tag will be omitted from serialization.
If v is set explicitly to the JSON value "null", this function will return false. Therefore, when available, prefer using the [IsPresent] method to check whether a field is present.
Generally, this function should only be used on structs, arrays, maps.
func MarshalObject ¶
func MarshalObject[T OverridableObject](f T, underlying any) ([]byte, error)
This uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag
func MarshalUnion ¶
This uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag
func NullObj ¶
func NullObj[T NullableObject, PT Settable[T]]() T
NullObj is used to mark a struct as null. To send null to an Opt field use NullOpt.
func NullPtr ¶
func NullPtr[T any]() *T
NullPtr returns a pointer to the zero value of the type T. When used with MarshalObject or MarshalUnion, it will be marshaled as null.
It is unspecified behavior to mutate the value pointed to by the returned pointer.
func NullSlice ¶
func NullSlice[T any]() []T
NullSlice returns a non-nil slice with a length of 0. When used with MarshalObject or MarshalUnion, it will be marshaled as null.
It is undefined behavior to mutate the slice returned by NullSlice.
func OverrideObj ¶
func OverrideObj[T OverridableObject, PT Settable[T]](v any) T
To override a specific field in a struct, use its [WithExtraFields] method.
Types ¶
type APIObject ¶
type APIObject struct {
// contains filtered or unexported fields
}
APIObject should be embedded in api object fields, preferably using an alias to make private
func (APIObject) GetExtraFields ¶
func (APIObject) IsNull ¶
func (m APIObject) IsNull() bool
IsNull returns true if the field is the explicit value `null`, prefer using [IsPresent] to check for presence, since it checks against null and omitted.
func (APIObject) IsOverridden ¶
func (*APIObject) WithExtraFields ¶
WithExtraFields adds extra fields to the JSON object.
WithExtraFields will override any existing fields with the same key. For security reasons, ensure this is only used with trusted input data.
To intentionally omit a required field, use Omit.
foo.WithExtraFields(map[string]any{"bar": Omit})
type APIUnion ¶
type APIUnion struct {
// contains filtered or unexported fields
}
APIUnion should be embedded in all api unions fields, preferably using an alias to make private
func (APIUnion) GetExtraFields ¶
func (APIUnion) IsNull ¶
func (m APIUnion) IsNull() bool
IsNull returns true if the field is the explicit value `null`, prefer using [IsPresent] to check for presence, since it checks against null and omitted.
func (APIUnion) IsOverridden ¶
func (*APIUnion) WithExtraFields ¶
WithExtraFields adds extra fields to the JSON object.
WithExtraFields will override any existing fields with the same key. For security reasons, ensure this is only used with trusted input data.
To intentionally omit a required field, use Omit.
foo.WithExtraFields(map[string]any{"bar": Omit})
type EncodedAsDate ¶
This type will not be stable and shouldn't be relied upon
func (EncodedAsDate) MarshalJSON ¶
func (m EncodedAsDate) MarshalJSON() ([]byte, error)
type NullableObject ¶
type NullableObject = overridableStruct
type Opt ¶
type Opt[T comparable] struct { Value T // indicates whether the field should be omitted, null, or valid Status Status // contains filtered or unexported fields }
func NewOpt ¶
func NewOpt[T comparable](v T) Opt[T]
func NullOpt ¶
func NullOpt[T comparable]() Opt[T]
Sets an optional field to null, to set an object to null use NullObj.
func (Opt[T]) IsNull ¶
IsNull returns true if the value is specifically the JSON value "null". It returns false if the value is omitted.
Prefer to use [IsPresent] to check the presence of a value.
func (Opt[T]) IsOmitted ¶
IsOmitted returns true if the value is omitted. It returns false if the value is the JSON value "null".
Prefer to use [IsPresent] to check the presence of a value.
func (Opt[T]) MarshalJSON ¶
func (Opt[T]) MarshalJSONWithTimeLayout ¶
Don't worry about this function, returns nil to fallback towards [MarshalJSON]
func (*Opt[T]) UnmarshalJSON ¶
type Optional ¶
type Optional interface { // IsPresent returns true if the value is not "null" or omitted IsPresent() bool // IsOmitted returns true if the value is omitted, it returns false if the value is "null". IsOmitted() bool // IsNull returns true if the value is "null", it returns false if the value is omitted. IsNull() bool // contains filtered or unexported methods }
type OverridableObject ¶
type OverridableObject = overridableStruct