urlval

package module
v4.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 11 Imported by: 0

README

urlval

JSONAPI compliant query string decoder/encoder.

TL;DR

type ArticlesRequest struct {
    AuthorFilter    *string        `filter:"author"`
    CommentsInclude bool           `include:"comments"`
    PageNumber      uint64         `page:"number"`
    Search          string         `url:"search"`
    Sort            []urlval.Sort  `url:"sort"`
}

err := urlval.Decode(r.URL.Query(), &request)
query := urlval.Encode(request)

Compatibility with other libraries

urlval have a bit of compatibility with kit/pgdb. It exposes PagePagams and SortTypes that are a great fit to use with urlval:

type MyRequest struct {
    pgdb.PageParams
    Sort pgdb.Sort
}

// than it can be used direclty with your sql stmt:

stmt = request.Page.ApplyTo(...)
// or
stmt = request.Sort.ApplyTo(...)
Request struct annotations
  • filter and page accepts both implementors of encoding.UnmarshalText, but filter supports pointers only.
  • include only booleans are supported
  • url tags may tag arbitrary fields, but fields with struct types that do not implement TextMarshal/Unmarshal are ignored.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(values url.Values, dest interface{}) error

Decode is decodes provided url values to destination struct. Using Decode requires your request to follow JSON API spec - If it's values contains anything except from "include", "sort", "search", "filter", "page", or query has parameters that are not tagged in dest - Decode still populates dest but also returns an error. The only error type it returns is errBadRequest that is compatible with ape (https://gitlab.com/distributed_lab/ape), so can be rendered directly to client.

func DecodeSilently

func DecodeSilently(values url.Values, dest interface{}) error

DecodeSilently decodes provided url values to destination struct. Using DecodeSilently requires your request to follow JSON API spec - If it's values contains anything except from "include", "sort", "search", "filter", "page", or query has parameters that are not tagged in dest - DecodeSilently populates dest and does not return an error (where Decode returns). The only error type it returns is errBadRequest that is compatible with ape (https://gitlab.com/distributed_lab/ape), so can be rendered directly to client.

func Encode

func Encode(src interface{}) (string, error)

Encode encodes given struct with urlval tags into url query string.

func MustEncode

func MustEncode(src interface{}) string

MustEncode - encodes given struct with urlval tags into url query string. Panics on failure

Types

type Sort

type Sort string

Sort is a string that describes one sort parameter defined by client.

func (Sort) Desc

func (s Sort) Desc() bool

Desc returns whether sorting by specific column should be performed in descending order.

func (Sort) Key

func (s Sort) Key() string

Key of the sort parameter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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