param

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: Apache-2.0 Imports: 7 Imported by: 62

Documentation

Index

Constants

View Source
const Omit forceOmit = -1

Omit can be used with [metadata.SetExtraFields] 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 IsNull

func IsNull[T any](v T) bool

IsNull returns true if v was set to the JSON value null.

To set a param to null use NullStruct, Null, NullMap, or NullSlice depending on the type of v.

IsNull returns false if the value is omitted.

func IsOmitted

func IsOmitted(v any) bool

IsOmitted returns true if v is the zero value of its type.

If IsOmitted is true, and the field uses a `json:"...,omitzero"` tag, the field will be omitted from the request.

If v is set explicitly to the JSON value "null", IsOmitted returns false.

func MarshalObject

func MarshalObject[T ParamStruct](f T, underlying any) ([]byte, error)

MarshalObject uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag

Stability for the API of MarshalObject is not guaranteed.

func MarshalUnion

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

MarshalUnion uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag

Stability for the API of MarshalUnion is not guaranteed.

func MarshalWithExtras

func MarshalWithExtras[T ParamStruct, R any](f T, underlying any, extras map[string]R) ([]byte, error)

MarshalWithExtras is used to marshal a struct with additional properties.

Stability for the API of MarshalWithExtras is not guaranteed.

func NullMap added in v1.7.0

func NullMap[MapT ~map[string]T, T any]() MapT

NullMap returns a non-nil map with a length of 0. When used with MarshalObject or MarshalUnion, it will be marshaled as null.

It is unspecified behavior to mutate the map returned by NullMap.

func NullSlice added in v1.7.0

func NullSlice[SliceT ~[]T, T any]() SliceT

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 unspecified behavior to mutate the slice returned by NullSlice.

func NullStruct

func NullStruct[T ParamStruct, PtrT InferPtr[T]]() T

NullStruct is used to set a struct to the JSON value null. Check for null structs with IsNull.

Only the first type parameter should be provided, the type PtrT will be inferred.

json.Marshal(param.NullStruct[MyStruct]()) -> 'null'

To send null to an Opt field use Null.

func Override

func Override[T ParamStruct, PtrT InferPtr[T]](v any) T

Override replaces the value of a struct with any type.

Only the first type parameter should be provided, the type PtrT will be inferred.

It's often useful for providing raw JSON

param.Override[MyStruct](json.RawMessage(`{"foo": "bar"}`))

The public fields of the returned struct T will be unset.

To override a specific field in a struct, use its [SetExtraFields] 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) ExtraFields

func (m APIObject) ExtraFields() map[string]any

ExtraFields returns the extra fields added to the JSON object.

func (APIObject) Overrides

func (m APIObject) Overrides() (any, bool)

Overrides returns the value of the struct when it is created with Override, the second argument helps differentiate an explicit null.

func (*APIObject) SetExtraFields

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

SetExtraFields adds extra fields to the JSON object.

SetExtraFields 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.SetExtraFields(map[string]any{"bar": Omit})

If the struct already contains the field ExtraFields, then this method will have no effect.

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) ExtraFields

func (m APIUnion) ExtraFields() map[string]any

ExtraFields returns the extra fields added to the JSON object.

func (APIUnion) Overrides

func (m APIUnion) Overrides() (any, bool)

Overrides returns the value of the struct when it is created with Override, the second argument helps differentiate an explicit null.

func (*APIUnion) SetExtraFields

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

SetExtraFields adds extra fields to the JSON object.

SetExtraFields 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.SetExtraFields(map[string]any{"bar": Omit})

If the struct already contains the field ExtraFields, then this method will have no effect.

type EncodedAsDate

type EncodedAsDate Opt[time.Time]

EncodedAsDate is not be stable and shouldn't be relied upon

func (EncodedAsDate) MarshalJSON

func (m EncodedAsDate) MarshalJSON() ([]byte, error)

type InferPtr

type InferPtr[T ParamStruct] interface {
	*T
	// contains filtered or unexported methods
}

This is an implementation detail and should never be explicitly set.

type Opt

type Opt[T comparable] struct {
	Value T
	// contains filtered or unexported fields
}

Opt represents an optional parameter of type T. Use the Opt.Valid method to confirm.

func NewOpt

func NewOpt[T comparable](v T) Opt[T]

func Null

func Null[T comparable]() Opt[T]

Null creates optional field with the JSON value "null".

To set a struct to null, use NullStruct.

func (Opt[T]) MarshalJSON

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

func (Opt[T]) MarshalJSONWithTimeLayout

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

MarshalJSONWithTimeLayout is necessary to bypass the internal caching performed by json.Marshal. Prefer to use Opt.MarshalJSON instead.

This function requires that the generic type parameter of Opt is not time.Time.

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

func (Opt[T]) Valid

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

Valid returns true if the value is not "null" or omitted.

To check if explicitly null, use [Opt.Null].

type Optional

type Optional interface {
	Valid() bool
	// contains filtered or unexported methods
}

This interface is useful for internal purposes.

type ParamNullable

type ParamNullable interface {
	// contains filtered or unexported methods
}

ParamNullable encapsulates all structs in parameters, and all Opt types in parameters.

type ParamStruct

type ParamStruct interface {
	Overrides() (any, bool)
	// contains filtered or unexported methods
}

ParamStruct represents the set of all structs that are used in API parameters, by convention these usually end in "Params" or "Param".

Jump to

Keyboard shortcuts

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