Categorygithub.com/stoewer/go-qparam
modulepackage
1.0.1
Repository: https://github.com/stoewer/go-qparam.git
Documentation: pkg.go.dev

# README

CircleCI codecov GoDoc

Go qparam

Package qparam provides convenient functions to read query parameters or form values into the fields of one (or more) target struct.

The following field types are supported: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool, string. In addition the package handles also all types implementing the TextUnmarshaler interface from the encoding package. Furthermore pointers and slices of all before mentioned types are supported.

To handle hierarchically structured data, the package can also be used to read values into fields of nested structs. In such a case the keys of the source must use dots (.) as 'path' delimiter. The reader can further be configured to use custom field tags and a custom name mapping, which keeps the necessity to add tags to struct fields at a minimum.

Versions and stability

This package can be considered stable and ready to use. All releases follow the rules of semantic versioning.

Although the master branch is supposed to remain stable, there is not guarantee that braking changes will not be merged into master when major versions are released. Therefore the repository contains version tags in order to support vendoring tools such as glide. The tag names follow common conventions and have the following format v1.0.0. This package also supports Go modules introduced with version 1.11.

Install and use

go get -u github.com/stoewer/go-qparam

The following example show how qparam can be used (there are more examples in the reference):

type Page struct {
    Limit  int
    Offset int
}

type Filters struct {
    Name string
    Age  int
}

values := map[string][]string{
    "limit":  {"25"},
    "offset": {"100"},
    "name":   {"Doe"},
    "age":    {"31"},
}

var page Page
var filters Filters
reader := qparam.NewReader()
reader.Read(values, &page, &filters)

# Functions

Mapper is a functional option which allows to specify a custom name mapper to the reader.
NewReader creates a new reader which can be configured with predefined functional options.
Strict is a functional option used to define whether the reader runs in struct mode or not.
Tag is a functional option which allows to specify a custom struct tag for the reader.

# Structs

Reader defines methods which can read query parameters and assign them to matching fields of target structs.

# Interfaces

MultiError is an error which also contains a map of additional (named) errors which altogether caused the actual failure.

# Type aliases

Option is a functional option which can be applied to a reader.