Documentation
¶
Index ¶
- func DefaultSchemaMetadata(field reflect.StructField, index int) any
- func GetTagMetadata[T any](f *FieldMetadata, tagName string) (T, bool)
- func ParseBodyTag(field reflect.StructField, index int, tagValue string) (any, error)
- func ParseSchemaTag(field reflect.StructField, index int, tagValue string) (any, error)
- type BodyMetadata
- type BodyType
- type Codec
- type Decoder
- type DefaultMetadataFunc
- type FieldMetadata
- type Metadata
- type ParameterLocation
- type SchemaMetadata
- type StructMetadata
- type Style
- type TagParserFunc
- type TagParserRegistry
- type TagParserRegistryOption
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultSchemaMetadata ¶
func DefaultSchemaMetadata(field reflect.StructField, index int) any
NewDefaultSchemaMetadata creates default SchemaMetadata for untagged fields. This function is used by the metadata builder when no explicit schema tag is found.
func GetTagMetadata ¶
func GetTagMetadata[T any](f *FieldMetadata, tagName string) (T, bool)
GetTagMetadata is a package-level generic function for type-safe access to tag metadata. Usage: GetTagMetadata[*SchemaMetadata](field, "schema").
func ParseBodyTag ¶
ParseBodyTag parses a body tag and returns BodyMetadata.
func ParseSchemaTag ¶
ParseSchemaTag parses a schema tag and returns SchemaMetadata.
Types ¶
type BodyMetadata ¶
BodyMetadata represents metadata for body tag fields.
type Codec ¶
type Codec struct {
// contains filtered or unexported fields
}
Codec handles encoding and decoding between structs and parameter strings. It uses injectable decoder and unmarshaler for request handling.
func NewCodec ¶
func NewCodec(metadata *Metadata, unmarshaler Unmarshaler, decoder Decoder) *Codec
NewCodec creates a new Codec with custom dependencies for advanced use cases.
func NewDefaultCodec ¶
func NewDefaultCodec() *Codec
NewDefaultCodec creates a new Codec with default configuration. This is the recommended constructor for most use cases.
type Decoder ¶
type Decoder interface {
// Decode decodes HTTP parameters (query, header, cookie, path, body) to map.
Decode(request *http.Request, routerParams map[string]string, metadata *StructMetadata) (map[string]any, error)
}
Decoder interface for decoding HTTP request data to maps.
func NewDecoder ¶
newDefaultDecoder creates a new decoder.
func NewDefaultDecoder ¶
func NewDefaultDecoder() Decoder
type DefaultMetadataFunc ¶
type DefaultMetadataFunc func(field reflect.StructField, index int) any
DefaultMetadataFunc creates default metadata for untagged fields.
type FieldMetadata ¶
type FieldMetadata struct {
// StructFieldName is the name of the struct field in Go source code.
StructFieldName string
// Index is the field index in the struct (used for reflection-based field access).
Index int
// Embedded indicates whether this field is an embedded/anonymous struct field.
Embedded bool
// Type is the reflect.Type of the field.
Type reflect.Type
// Tag-specific metadata: tag name -> metadata object
// A field can have multiple tags (e.g., schema + validate)
TagMetadata map[string]any // "schema" -> *SchemaMetadata, "body" -> *BodyMetadata, etc.
}
FieldMetadata represents a cached struct field metadata. It can represent both parameter fields (schema tag) and body fields (body tag).
func (*FieldMetadata) HasTag ¶
func (f *FieldMetadata) HasTag(tagName string) bool
HasTag checks if field has a specific tag.
type Metadata ¶
type Metadata struct {
// contains filtered or unexported fields
}
Metadata is a separate component for metadata operations (tag parsing, metadata building, caching). It serves use cases beyond Codec: - Validation metadata (for validators) - OpenAPI schema generation - Introspection and tooling.
func NewDefaultMetadata ¶
func NewDefaultMetadata() *Metadata
NewDefaultMetadata creates a new Metadata with default parsers (schema and body).
func NewMetadata ¶
func NewMetadata(registry *TagParserRegistry) *Metadata
NewMetadata creates a new Metadata with the given registry.
func (*Metadata) GetStructMetadata ¶
func (m *Metadata) GetStructMetadata(typ reflect.Type) (*StructMetadata, error)
GetStructMetadata retrieves or builds struct metadata for the given type.
type ParameterLocation ¶
type ParameterLocation string
ParameterLocation represents the location of a parameter in an OpenAPI spec.
const ( // LocationQuery represents query parameters. LocationQuery ParameterLocation = "query" // LocationPath represents path parameters. LocationPath ParameterLocation = "path" // LocationHeader represents header parameters. LocationHeader ParameterLocation = "header" // LocationCookie represents cookie parameters. LocationCookie ParameterLocation = "cookie" )
type SchemaMetadata ¶
type SchemaMetadata struct {
ParamName string
MapKey string
Location ParameterLocation
Style Style
Explode bool
}
SchemaMetadata represents metadata for schema tag fields.
type StructMetadata ¶
type StructMetadata struct {
Type reflect.Type
Fields []FieldMetadata
// contains filtered or unexported fields
}
func NewStructMetadata ¶
func NewStructMetadata(typ reflect.Type, fields []FieldMetadata) (*StructMetadata, error)
NewStructMetadata creates a new struct metadata from a type and fields. This is useful for tests and when you already have FieldMetadata built.
func (*StructMetadata) Field ¶
func (m *StructMetadata) Field(fieldName string) (*FieldMetadata, bool)
Field returns FieldMetadata by field name.
type Style ¶
type Style string
Style represents the serialization style for a parameter.
const ( // StyleMatrix is used for path parameters. // Values are prefixed with a semicolon (;) and key-value pairs are separated by an equals sign (=). StyleMatrix Style = "matrix" // StyleLabel is used for path parameters. // Values are prefixed with a period (.). StyleLabel Style = "label" // StyleForm is commonly used for query and cookie parameters. // Values are serialized as form data. StyleForm Style = "form" // StyleSimple is applicable to path and header parameters. // Values are serialized without any additional formatting. StyleSimple Style = "simple" // StyleSpaceDelimited is used for query parameters. // Array values are separated by spaces. StyleSpaceDelimited Style = "spaceDelimited" // StylePipeDelimited is used for query parameters. // Array values are separated by pipes (|). StylePipeDelimited Style = "pipeDelimited" // StyleDeepObject is used for query parameters. // Allows for complex objects to be represented in a deep object style. StyleDeepObject Style = "deepObject" )
type TagParserFunc ¶
TagParserFunc is a function type for parsing struct tags into metadata.
type TagParserRegistry ¶
type TagParserRegistry struct {
// contains filtered or unexported fields
}
TagParserRegistry manages registered tag parsers with explicit tag name mapping. It is immutable after construction.
func NewDefaultTagParserRegistry ¶
func NewDefaultTagParserRegistry() *TagParserRegistry
NewDefaultTagParserRegistry creates a new tag parser registry with default parsers (schema and body).
func NewTagParserRegistry ¶
func NewTagParserRegistry(opts ...TagParserRegistryOption) *TagParserRegistry
NewTagParserRegistry creates a new immutable tag parser registry with the given options.
func (*TagParserRegistry) All ¶
func (r *TagParserRegistry) All() map[string]TagParserFunc
All returns a copy of all registered parsers.
func (*TagParserRegistry) Get ¶
func (r *TagParserRegistry) Get(tagName string) TagParserFunc
Get returns the parser for the given tag name, or nil if not found.
func (*TagParserRegistry) GetDefault ¶
func (r *TagParserRegistry) GetDefault(tagName string) DefaultMetadataFunc
GetDefault returns the default metadata factory for the given tag name, or nil if not found.
type TagParserRegistryOption ¶
type TagParserRegistryOption func(parsers map[string]TagParserFunc, defaults map[string]DefaultMetadataFunc)
TagParserRegistryOption configures a TagParserRegistry during construction.
func WithTagParser ¶
func WithTagParser(tagName string, parser TagParserFunc, defaultFunc ...DefaultMetadataFunc) TagParserRegistryOption
WithTagParser registers a parser with an explicit tag name. If parser is nil, it is skipped. If tag already exists, it is overridden. An optional default metadata function can be provided as a third parameter.