parameter

package
v0.0.0-...-8c81104 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package parameter defines serialization functions for HTTP parameters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeCookieParameters

func DecodeCookieParameters(
	definition []*oaschema.Parameter,
	cookies []*http.Cookie,
) (map[string]any, []httperror.ValidationError)

DecodeCookieParameters decodes header parameters from the header map value. Each header value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

func DecodeHeaderParameters

func DecodeHeaderParameters(
	definition []*oaschema.Parameter,
	headers http.Header,
) (map[string]any, []httperror.ValidationError)

DecodeHeaderParameters decodes header parameters from the header map value. Each header value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

func DecodePathValue

func DecodePathValue(
	definition *oaschema.Parameter,
	value string,
) (any, []httperror.ValidationError)

DecodePathValue decodes the path parameter from a string value. The value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

func DecodeQueryFromParameters

func DecodeQueryFromParameters(
	definitions []*oaschema.Parameter,
	values map[string][]string,
) (map[string]any, []httperror.ValidationError)

DecodeQueryFromParameters decodes the query parameters from string values. The value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

func EncodeHeader

func EncodeHeader(definition *oaschema.Parameter, value any) string

EncodeHeader encodes the header from an arbitrary value. The value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

func EncodeParamDelimitedStyleNonExplode

func EncodeParamDelimitedStyleNonExplode(
	params ParameterItems,
	separator string,
	assignSymbol string,
	formatFunc func(string) string,
) string

EncodeParamDelimitedStyleNonExplode encodes ampersand-separated values with explode=false.

func EncodePathValue

func EncodePathValue(definition *oaschema.Parameter, value any) string

EncodePathValue encodes the path parameter from an arbitrary value. The value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

func EncodeQueryEscape

func EncodeQueryEscape(value string, allowReserved bool) string

EncodeQueryEscape encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key with escape.

func EncodeQueryValuesUnescape

func EncodeQueryValuesUnescape(values url.Values) string

EncodeQueryValuesUnescape encode query values into “URL encoded” form ("bar=baz&foo=quux") sorted by key without escape.

func GetQueryEscapeFunc

func GetQueryEscapeFunc(allowReserved bool) func(string) string

GetQueryEscapeFunc returns either query escape or allow-reserved encoder function.

func IsParamIndex

func IsParamIndex(selector ParamSelector) bool

IsParamIndex reports whether selector is a numeric index.

func IsParamKey

func IsParamKey(selector ParamSelector) bool

IsParamKey reports whether selector is a string key.

func IsReservedCharacter

func IsReservedCharacter[C byte | rune](c C) bool

IsReservedCharacter checks if the character is allowed in a URI and has a reserved purpose.

reserved    = gen-delims / sub-delims
gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

func IsUnreservedCharacter

func IsUnreservedCharacter[C byte | rune](c C) bool

IsUnreservedCharacter checks if the character is allowed in a URI but do not has a reserved purpose are called unreserved.

unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"

func NormalizeHeaders

func NormalizeHeaders(headers http.Header) map[string]any

NormalizeHeaders normalize HTTP headers to a string value map. Header keys are transformed to lowercase. Header values are transformed to either string or array string if they have many items.

func QueryEscapeAllowReserved

func QueryEscapeAllowReserved(query string) string

QueryEscapeAllowReserved escapes the string so it can be safely placed inside a URL query. Allow reserved character.

func SetQueryParam

func SetQueryParam(params url.Values, definition *oaschema.Parameter, value any)

SetQueryParam encodes and set the query param into URL values. The value is encoded differently on each style, according to the [OpenAPI specification].

[OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/3.2.0/versions/3.2.0.md#style-examples)

Types

type ParamIndex

type ParamIndex int

ParamIndex represents a parameter index. The sentinel value -1 is used when a bare "[]" suffix is parsed from a deep-object key (e.g. "ids[]"), meaning the position within the array is not known until Normalize resolves it.

func (ParamIndex) Equal

func (k ParamIndex) Equal(target ParamSelector) bool

Equal checks if the target value is equal.

func (ParamIndex) IsZero

func (k ParamIndex) IsZero() bool

IsZero checks if the key is empty.

func (ParamIndex) String

func (k ParamIndex) String() string

String implements fmt.Stringer interface.

type ParamKey

type ParamKey string

ParamKey represents a parameter key string.

func (ParamKey) Equal

func (k ParamKey) Equal(target ParamSelector) bool

Equal checks if the target value is equal.

func (ParamKey) IsZero

func (k ParamKey) IsZero() bool

IsZero checks if the key is empty.

func (ParamKey) String

func (k ParamKey) String() string

String implements fmt.Stringer interface.

type ParamKeys

type ParamKeys []ParamSelector

ParamKeys is an ordered path of selectors that locates a value within a nested parameter tree, e.g. [ParamKey("address"), ParamKey("city")] or [ParamKey("ids"), ParamIndex(0)].

func (ParamKeys) Equal

func (ks ParamKeys) Equal(target ParamKeys) bool

Equal checks if the target value is equal.

func (ParamKeys) Format

func (ks ParamKeys) Format(root string, isDeepObject bool) string

Format prints parameter keys with format.

func (ParamKeys) String

func (ks ParamKeys) String() string

String implements fmt.Stringer interface.

type ParamSelector

type ParamSelector interface {
	goutils.Equaler[ParamSelector]
	goutils.IsZeroer
	fmt.Stringer
}

ParamSelector is the discriminated union for a single path segment: either a string key (ParamKey) for object properties or a numeric index (ParamIndex) for array elements.

type ParameterItem

type ParameterItem struct {
	// contains filtered or unexported fields
}

ParameterItem represents the key-value pair.

func NewParameterItem

func NewParameterItem(keys ParamKeys, value string) ParameterItem

NewParameterItem creates a parameter value pair.

func (ParameterItem) IsNested

func (pi ParameterItem) IsNested() bool

IsNested checks if the parameter is a nested object field.

func (ParameterItem) Keys

func (pi ParameterItem) Keys() ParamKeys

Keys return key fragments of the parameter item.

func (ParameterItem) Value

func (pi ParameterItem) Value() string

Value return the value of the item.

type ParameterItems

type ParameterItems []ParameterItem

ParameterItems is the flattened representation of a (potentially nested) parameter value produced by EvaluateParameterValue. Each item carries the full key path and the serialized leaf value so encoders can reconstruct any OpenAPI serialization style.

func EvaluateParameterValue

func EvaluateParameterValue(value any, parentKeys ParamKeys) ParameterItems

EvaluateParameterValue evaluates the type of the value and encode it into a string map.

func (ParameterItems) Build

func (ssp ParameterItems) Build(
	prefix string,
	isDeepObject bool,
	formatFunc func(string) string,
) (map[string][]string, int)

Build build parameter items to a key-values map and estimate the length of the string will be built.

type ParameterNode

type ParameterNode struct {
	// contains filtered or unexported fields
}

ParameterNode is a single node in the decoded parameter tree. Leaf nodes (no items) carry raw string values; branch nodes carry child nodes keyed by ParamKey (object property) or ParamIndex (array element).

func (*ParameterNode) Decode

func (pn *ParameterNode) Decode(schema *base.Schema) (any, []httperror.ValidationError)

Decode resolves the node's raw values into a typed Go value guided by schema. Object and array schema types are tried first; remaining scalar types are tried in declaration order.

func (*ParameterNode) FindNode

func (pn *ParameterNode) FindNode(key ParamSelector) *ParameterNode

FindNode returns the direct child node matching key, or nil if absent.

func (*ParameterNode) InsertNode

func (pn *ParameterNode) InsertNode(keys ParamKeys, values []string) *httperror.ValidationError

InsertNode walks (or creates) the path described by keys and stores values at the leaf. When mixing ParamKey and ParamIndex siblings it tries to coerce the odd type so that the whole level is consistently typed; it returns an error if coercion is impossible.

func (*ParameterNode) Normalize

func (pn *ParameterNode) Normalize()

Normalize resolves ambiguities that arise from deep-object query parsing:

  • A single numeric ParamKey child ("0") is promoted to ParamIndex so the node is treated as an array element rather than an object property.
  • A single ParamIndex child with no further children is collapsed: its values are hoisted into the parent, flattening a one-element array.
  • Multi-element arrays are sorted by index so later schema validation sees items in order.

func (ParameterNode) String

func (pn ParameterNode) String() string

type ParameterNodes

type ParameterNodes []*ParameterNode

ParameterNodes is a flat list of root-level ParameterNode entries, one per distinct top-level key. It is used as the working set when inserting and decoding deep-object query parameters.

func (ParameterNodes) Find

Find returns the first node whose key equals key, or nil if not found.

func (*ParameterNodes) Insert

func (pn *ParameterNodes) Insert(keys ParamKeys, values []string) *httperror.ValidationError

Insert inserts a leaf at the path described by keys, creating intermediate nodes as needed.

func (ParameterNodes) String

func (pn ParameterNodes) String() string

String implements fmt.Stringer for debugging.

Jump to

Keyboard shortcuts

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