param

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 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 {
	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

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

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

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 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.

Jump to

Keyboard shortcuts

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