param

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderRealIp is the standard header for real client IP when behind proxies.
	HeaderRealIp = "X-REAL-IP"

	// HeaderForwardedFor is the standard header for forwarded client IPs.
	HeaderForwardedFor = "X-FORWARDED-FOR"
)

Variables

This section is empty.

Functions

func IpCaster

func IpCaster(value any) (any, error)

IpCaster converts a string to net.IP. Handles comma-separated lists (X-Forwarded-For) and returns the first valid IP.

Types

type Caster

type Caster func(any) (any, error)

Caster converts a raw string value to the desired type. Returns the converted value or an error.

type DefaultValueFunc

type DefaultValueFunc func() any

DefaultValueFunc provides a default value when none is found in the request.

type Getter

type Getter func(request *http.Request) string

Getter extracts a string value from an HTTP request.

type Option

type Option func(param *Param) *Param

Option configures a Param during creation.

func AddGetter

func AddGetter(getter Getter) Option

AddGetter adds a custom getter function to the parameter.

func AddValidator

func AddValidator(validator Validator) Option

AddValidator adds a custom validator function to the parameter.

func WithCookieGetter

func WithCookieGetter() Option

WithCookieGetter adds a getter that extracts the parameter from cookies. Uses the parameter's name as the cookie name.

func WithCookieGetterByName

func WithCookieGetterByName(name string) Option

WithCookieGetterByName adds a getter that extracts from cookies with a custom key.

func WithDefault

func WithDefault(value any) Option

WithDefault sets a static default value for the parameter.

func WithDefaultFunc

func WithDefaultFunc(defaultFunc DefaultValueFunc) Option

WithDefaultFunc sets a dynamic default value provider for the parameter.

func WithHeaderGetter

func WithHeaderGetter() Option

WithHeaderGetter adds a getter that extracts the parameter from HTTP headers. Uses the parameter's name as the header key.

func WithHeaderGetterByName

func WithHeaderGetterByName(name string) Option

WithHeaderGetterByName adds a getter that extracts from headers with a custom key.

func WithUriGetter

func WithUriGetter() Option

WithUriGetter adds a getter that extracts the parameter from URL query string. Uses the parameter's name as the query key.

func WithUriGetterByName

func WithUriGetterByName(name string) Option

WithUriGetterByName adds a getter that extracts from URL query with a custom key.

type Param

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

Param defines a request parameter with extraction, casting, and validation rules. It implements http.Middleware, injecting the parsed value into the request context.

func NewIp

func NewIp(name string, logger logger.Logger) *Param

NewIp creates a parameter for extracting client IP addresses. It checks X-REAL-IP, X-FORWARDED-FOR, and finally RemoteAddr. Returns a net.IP value.

func NewParam

func NewParam(name string, logger logger.Logger, caster Caster, options ...Option) *Param

NewParam creates a new Param with the given name, logger, caster, and options. The parameter value will be stored in the request context under the parameter name.

func NewParamBool

func NewParamBool(name string, logger logger.Logger, options ...Option) *Param

NewParamBool creates a new boolean parameter.

func NewParamDuration

func NewParamDuration(name string, logger logger.Logger, options ...Option) *Param

NewParamDuration creates a new time.Duration parameter.

func NewParamFloat32

func NewParamFloat32(name string, logger logger.Logger, options ...Option) *Param

NewParamFloat32 creates a new float32 parameter.

func NewParamFloat64

func NewParamFloat64(name string, logger logger.Logger, options ...Option) *Param

NewParamFloat64 creates a new float64 parameter.

func NewParamInt

func NewParamInt(name string, logger logger.Logger, options ...Option) *Param

NewParamInt creates a new int parameter.

func NewParamInt8

func NewParamInt8(name string, logger logger.Logger, options ...Option) *Param

NewParamInt8 creates a new int8 parameter.

func NewParamInt16

func NewParamInt16(name string, logger logger.Logger, options ...Option) *Param

NewParamInt16 creates a new int16 parameter.

func NewParamInt32

func NewParamInt32(name string, logger logger.Logger, options ...Option) *Param

NewParamInt32 creates a new int32 parameter.

func NewParamInt64

func NewParamInt64(name string, logger logger.Logger, options ...Option) *Param

NewParamInt64 creates a new int64 parameter.

func NewParamString

func NewParamString(name string, logger logger.Logger, options ...Option) *Param

NewParamString creates a new string parameter.

func NewParamTime

func NewParamTime(name string, logger logger.Logger, options ...Option) *Param

NewParamTime creates a new time.Time parameter. Supports flexible date parsing via github.com/araddon/dateparse.

func NewParamUint

func NewParamUint(name string, logger logger.Logger, options ...Option) *Param

NewParamUint creates a new uint parameter.

func NewParamUint8

func NewParamUint8(name string, logger logger.Logger, options ...Option) *Param

NewParamUint8 creates a new uint8 parameter.

func NewParamUint16

func NewParamUint16(name string, logger logger.Logger, options ...Option) *Param

NewParamUint16 creates a new uint16 parameter.

func NewParamUint32

func NewParamUint32(name string, logger logger.Logger, options ...Option) *Param

NewParamUint32 creates a new uint32 parameter.

func NewParamUint64

func NewParamUint64(name string, logger logger.Logger, options ...Option) *Param

NewParamUint64 creates a new uint64 parameter.

func (*Param) Middleware

func (param *Param) Middleware(next http.Handler) http.Handler

Middleware implements http.Middleware. It extracts, casts, validates the parameter and injects it into the request context. If any step fails, it returns an appropriate HTTP error response.

func (*Param) Name

func (param *Param) Name() string

type Validator

type Validator func(any) error

Validator validates a typed value. Returns an error if validation fails.

func GteValidator

func GteValidator(val float64) Validator

GteValidator returns a validator that checks if a numeric value is >= the given threshold.

Jump to

Keyboard shortcuts

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