queryparam

package module
v0.0.0-...-1416dbb Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2018 License: MIT Imports: 5 Imported by: 0

README

Query Param

Build Status codecov Go Report Card Documentation

Query param makes it easy to access the query parameters stored in a URL.

Installation

go get -u github.com/tomwright/queryparam

Examples

Parsing a simple URL
type GetUsersRequest struct {
	Name   string `queryparam:"name"`
	Age    string `queryparam:"age"`
	Gender string `queryparam:"gender"`
}

u, err := url.Parse("https://example.com/users?name=Tom&age=23")
if err != nil {
    panic(err)
}

req := &GetUsersRequest{}

err = queryparam.Unmarshal(u, req)
if err != nil {
    panic(err)
}

fmt.Println(req.Name) // "Tom"
fmt.Println(req.Age) // "23"
fmt.Println(req.Gender) // ""
Parsing a URL with delimited parameters
type GetUsersRequest struct {
	Name   []string `queryparam:"name"`
}

u, err := url.Parse("https://example.com/users?name=Tom,Jim")
if err != nil {
    panic(err)
}

req := &GetUsersRequest{}

err = queryparam.Unmarshal(u, req)
if err != nil {
    panic(err)
}

fmt.Println(req.Name) // { "Tom", "Jim" }

If you want to change the delimiter you can do so by changing the value of queryparam.Delimiter.

The default value is ,.

Parsing a URL from a HTTP Request
var r *http.Request
// Receive r in a http handler...

type GetUsersRequest struct {
	Name   string `queryparam:"name"`
	Age    string `queryparam:"age"`
	Gender string `queryparam:"gender"`
}

req := &GetUsersRequest{}
err = queryparam.Unmarshal(r.URL, req)
if err != nil {
    panic(err)
}

// Do something with req...

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNonPointerTarget is returned when the given interface does not represent a pointer
	ErrNonPointerTarget = errors.New("invalid Unmarshal target. must be a pointer")
	// ErrInvalidURL is returned when the given *url.URL is nil
	ErrInvalidURL = errors.New("invalid url provided")
)

Functions

func Unmarshal

func Unmarshal(u *url.URL, i interface{}) error

Unmarshal attempts to parse query parameters from the specified URL and store any found values into the given interface

Example

ExampleUnmarshal creates a dummy http request and unmarshals the data into a struct

package main

import (
	"fmt"
	"net/http"
	"net/url"

	"github.com/tomwright/queryparam"
)

func main() {
	var err error
	var request = &http.Request{}
	request.URL, err = url.Parse("https://example.com/some/path?name=Tom&age=23")
	if err != nil {
		panic(err)
	}

	requestData := struct {
		Name string `queryparam:"name"`
		Age  string `queryparam:"age"`
	}{}

	err = queryparam.Unmarshal(request.URL, &requestData)
	if err != nil {
		panic(err)
	}

	fmt.Println(requestData.Name + " is " + requestData.Age)

}
Output:

Tom is 23

Types

This section is empty.

Jump to

Keyboard shortcuts

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