Skip to content

qparams is a golang package that provides url query parameters to be decoded to user defined golang structs with custom fields.

Notifications You must be signed in to change notification settings

aneshas/qparams

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qparams

import “github.com/tonto/qparams”

Build Status Coverage Status

qparams is a golang package that provides url query parameters to be decoded to user defined golang structs with custom fields.

Example usage

import qpgithub.com/tonto/qparams// User defined struct
type MyParams struct {
	// qparams Map (map[string]string)
	// with defined operations (required)
	Filter qp.Map `qparams:”ops:>=,==,!=“`

	// qparams Slice ([]string)
	Embed qp.Slice 

	// qparams Slice ([]string)
	// with custom separator
	Flags qp.Slice `qparams:”sep:|”`

    // field with custom query param name (lowercase is the default)
    // tags can be combined eg. `qparams:"name:foo sep:| ops:==,<>"`
    FooBar string `qparams:"name:foo-bar"`

	// Regular primitive values
	Limit int
	Page int
}

…

// Your http handler
func handler(w http.ResponseWriter, r *http.Request) {
	params := MyParams{}

	err := qp.Parse(&params, r)
	if err != nil {
		// Handle error
	}

	// at this point params is filled with 
	// parsed query parameters
	//

	// Assuming that request URL was:
	// http://foo.bar.baz?limit=100&page=2&embed=order,invoice,discount&filter=amount>=1000,currency==EUR,order_number!=753&flags=a|b|c
	//
	// params would contain following values:
	//
	// params.Filter 
	// qp.Map{
	//   “amount >=“: “1000”,
	//   “currency ==“: “EUR”,
	//   “order_number !=“: “753”,
	// }
	// params.Filter.Map() will convert it to map[string]string
	//
	// params.Embed
	// qp.Slice{“order”, “invoice”, “discount”}
	// params.Embed.Slice() will convert it to []string
	//
	// params.Flags
	// qp.Slice{“a”, “b”, “c”}
	// params.Embed.Slice() will convert it to []string
	//
	// params.Limit
	// int 100
	//
	// params.Page
	// int 2
}

Features

Docs

godoc.org/github.com/tonto/qparams

About

qparams is a golang package that provides url query parameters to be decoded to user defined golang structs with custom fields.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages