param

package
v0.1.0-beta.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2025 License: Apache-2.0 Imports: 7 Imported by: 20

Documentation

Index

Constants

View Source
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 IsNullPtr

func IsNullPtr[T any](ptr *T) bool

IsNullPtr returns true if the pointer was created by NullPtr.

func IsNullSlice

func IsNullSlice[T any](slice []T) bool

IsNullSlice returns true if the slice was created by NullSlice.

func IsOmitted

func IsOmitted(v any) bool

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

func MarshalUnion[T any](variants ...any) ([]byte, error)

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.

func VariantFromUnion

func VariantFromUnion(u reflect.Value) (any, error)

VariantFromUnion can be used to extract the present variant from a param union type. A param union type is a struct with an embedded field of APIUnion.

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 (m APIObject) GetExtraFields() map[string]any

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 (m APIObject) IsOverridden() (any, bool)

func (*APIObject) WithExtraFields

func (m *APIObject) WithExtraFields(extraFields map[string]any)

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 (m APIUnion) GetExtraFields() map[string]any

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 (m APIUnion) IsOverridden() (any, bool)

func (*APIUnion) WithExtraFields

func (m *APIUnion) WithExtraFields(extraFields map[string]any)

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

type EncodedAsDate Opt[time.Time]

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

func (o Opt[T]) IsNull() bool

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

func (o Opt[T]) IsOmitted() bool

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]) IsPresent

func (o Opt[T]) IsPresent() bool

IsPresent returns true if the value is not "null" and not omitted

func (Opt[T]) MarshalJSON

func (o Opt[T]) MarshalJSON() ([]byte, error)

func (Opt[T]) MarshalJSONWithTimeLayout

func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte

Don't worry about this function, returns nil to fallback towards [MarshalJSON]

func (Opt[T]) Or

func (o Opt[T]) Or(v T) T

func (Opt[T]) String

func (o Opt[T]) String() string

func (*Opt[T]) UnmarshalJSON

func (o *Opt[T]) UnmarshalJSON(data []byte) error

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

type Settable

type Settable[T overridableStruct] interface {
	*T
	// contains filtered or unexported methods
}

type Status

type Status int8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL