param

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Package for parsing path and query parameters from http request into struct, similar to parsing body as json to struct.

type MyInputStruct struct {
	UserID   int   `param:"path=id"`
	SomeFlag *bool `param:"query=flag"`
}

Then a request like http://somewhere.com/users/9?flag=true can be parsed as follows. In this example, using chi to access path parameters that has a {id} wildcard in configured chi router

	parsedInput := MyInputStruct{}
	param.DefaultParser().PathParamFunc(chi.URLParam).Parse(request, &parsedInput)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	QueryParamTagResolver TagResolver
	PathParamTagResolver  TagResolver
	PathParamFunc         PathParamFunc
}

Parser can Parse query and path parameters from http.Request into a struct. Fields struct have to be tagged such that either QueryParamTagResolver or PathParamTagResolver returns valid parameter name from the provided tag.

PathParamFunc is for getting path parameter from http.Request, as each http router handles it in different way (if at all). For example for chi, use WithPathParamFunc(chi.URLParam) to be able to use tags for path parameters.

func DefaultParser

func DefaultParser() Parser

DefaultParser returns query and path parameter Parser with intended struct tags `param:"query=param_name"` for query parameters and `param:"path=param_name"` for path parameters

func (Parser) Parse

func (p Parser) Parse(r *http.Request, dest any) error

Parse accepts the request and a pointer to struct that is tagged with appropriate tags set in Parser. All such tagged fields are assigned the respective parameter from the actual request.

Fields are assigned their zero value if the field was tagged but request did not contain such parameter.

Supported tagged field types are: - primitive types - bool, all ints, all uints, both floats, and string - pointer to any supported type - slice of non-slice supported type (only for query parameters) - any type that implements encoding.TextUnmarshaler

For query parameters, the tagged type can be a slice. This means that a query like /endpoint?key=val1&key=val2 is allowed, and in such case the slice field will be assigned []T{"val1", "val2"} . Otherwise, only single query parameter is allowed in request.

func (Parser) WithPathParamFunc

func (p Parser) WithPathParamFunc(f PathParamFunc) Parser

WithPathParamFunc returns a copy of Parser with set function for getting path parameters from http.Request. For more see Parser description.

type PathParamFunc

type PathParamFunc func(r *http.Request, key string) string

PathParamFunc is a function that returns value of specified http path parameter

type TagResolver

type TagResolver func(fieldTag reflect.StructTag) (string, bool)

TagResolver is a function that decides from a field type what key of http parameter should be searched. Second return value should return whether the key should be searched in http parameter at all.

func FixedTagNameParamTagResolver

func FixedTagNameParamTagResolver(tagName string) TagResolver

FixedTagNameParamTagResolver returns a TagResolver, that matches struct params by specific tag. Example: FixedTagNameParamTagResolver("mytag") matches a field tagged with `mytag:"param_name"`

func TagWithModifierTagResolver

func TagWithModifierTagResolver(tagName string, tagModifier string) TagResolver

TagWithModifierTagResolver returns a TagResolver, that matches struct params by specific tag and by a value before a '=' separator. Example: FixedTagNameParamTagResolver("mytag", "mymodifier") matches a field tagged with `mytag:"mymodifier=param_name"`

Jump to

Keyboard shortcuts

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