params

package module
v0.0.0-...-9742c45 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2016 License: MIT Imports: 2 Imported by: 1

README

params

A simple wrapper to parse map[string]string and map[string][]string values as int, float, bool.

It was written with Gorilla Mux (https://github.com/gorilla/mux) and url.Values in mind.

#API

There are two types Params and Param. The first one is a collection of all params. To create a Params one should call one of:

  • func NewParams(m map[string]string) Params
  • func NewParamsSlices(m map[string][]string) Params

One can check if a param is available and get it using: func (*Params) Has(name string) bool and func (*Params) Get(name string) *Param. Param keeps value of one param. Then one may check if parsing param as a given type is possible using one of many functions: func (*Param) CanInt() bool (similar for int32, int64, etc.). One may get value as a given type by: func (*Param) Float32() float32, it will return a value parsed as given type. It will return default value if cannot parse.

Beware, the code:

ps := params.NewParams(...).Get("var")
p := ps.Float32()

will panic if Params doesn't have a var parameter set. It's because Get will return nil and one calls a method on nil. It's by design. If one wants default's then should use .Float32Or(0.) which if parsing not possible or with error returns an argument.

Example

import (
  "github.com/orian/params"
  "github.com/gorilla/mux"
)

And

func ListH(w http.ResponseWriter, req *http.Request) {
	page := params.NewParams(mux.Vars(req)).Get("page").IntOr(0)
	fmt.Fprintf(w, "List view, page: %d", page)
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/new", FormH).Methods("GET")
	r.HandleFunc("/upload", UploadH).Methods("POST")
	r.HandleFunc("/", ListH)
	r.HandleFunc("/p/{page}", ListH)
	r.HandleFunc("/k/{id}", DetailH)

	log.Print(http.ListenAndServe(":8080", r))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpParams

type HttpParams []httprouter.Param

func (HttpParams) Get

func (p HttpParams) Get(name string) Param

func (HttpParams) Has

func (p HttpParams) Has(name string) bool

type Param

type Param interface {
	CanString() bool
	String() string
	StringOr(v string) string

	CanInt32() bool
	Int32() int32
	Int32Or(v int32) int32
	CanInt64() bool
	Int64() int64
	Int64Or(v int64) int64
	CanInt() bool
	Int() int
	IntOr(v int) int

	CanFloat32() bool
	Float32() float32
	Float32Or(v float32) float32
	CanFloat64() bool
	Float64() float64
	Float64Or(v float64) float64

	CanBool() bool
	Bool() bool
	BoolOr(v bool) bool
	// contains filtered or unexported methods
}

func NewParam

func NewParam(s ...string) Param

type Params

type Params interface {
	Has(name string) bool
	Get(name string) Param
}

func NewFromHttpRouter

func NewFromHttpRouter(p httprouter.Params) Params

func NewParams

func NewParams(m map[string]string) Params

func NewParamsSlices

func NewParamsSlices(m map[string][]string) Params

Jump to

Keyboard shortcuts

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