Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Parser ¶
type Parser struct { ParamTagResolver 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 ¶
Parse accepts the request and a pointer to struct with its fields tagged with appropriate tags set in Parser. Such tagged fields must be in top level struct, or in exported struct embedded in top-level struct. 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 ¶
PathParamFunc is a function that returns value of specified http path parameter.
type TagResolver ¶
TagResolver is a function that decides from a field tag what parameter should be searched. Second return value should return whether the parameter should be searched at all.
func TagNameResolver ¶ added in v0.8.0
func TagNameResolver(tagName string) TagResolver
TagNameResolver returns a TagResolver that returns the value of tag with tagName, and whether the tag exists at all. It can be used to replace Parser.ParamTagResolver to change what tag name the Parser reacts to.