Skip to content
The package provides an easy way to "unmarshal" query string data into a struct. Without reflect.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
bench_test.go
example_test.go
httpreq.go
httpreq_test.go

README.md

httpreq

The package provides an easy way to "unmarshal" query string data into a struct. Without reflect.

GitHub release GoDoc Build Status Coverage Status

Example

Literal

package main

import (
	"encoding/json"
	"log"
	"net/http"
	"time"

	"github.com/creack/httpreq"
)

// Req is the request query struct.
type Req struct {
	Fields    []string
	Limit     int
	Page      int
	Timestamp time.Time
}

func handler(w http.ResponseWriter, req *http.Request) {
	if err := req.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	data := &Req{}
	if err := (httpreq.ParsingMap{
		{Field: "limit", Fct: httpreq.ToInt, Dest: &data.Limit},
		{Field: "page", Fct: httpreq.ToInt, Dest: &data.Page},
		{Field: "fields", Fct: httpreq.ToCommaList, Dest: &data.Fields},
		{Field: "timestamp", Fct: httpreq.ToTSTime, Dest: &data.Timestamp},
	}.Parse(req.Form)); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	_ = json.NewEncoder(w).Encode(data)
}

func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe(":8080", nil))
	// curl 'http://localhost:8080?timestamp=1437743020&limit=10&page=1&fields=a,b,c'
}

Chained

package main

import (
        "encoding/json"
        "log"
        "net/http"
        "time"

        "github.com/creack/httpreq"
)

// Req is the request query struct.
type Req struct {
        Fields    []string
        Limit     int
        Page      int
        Timestamp time.Time
}

func handler(w http.ResponseWriter, req *http.Request) {
        if err := req.ParseForm(); err != nil {
                http.Error(w, err.Error(), http.StatusBadRequest)
                return
        }

        data := &Req{}
        if err := httpreq.NewParsingMap().
                Add("limit", httpreq.ToInt, &data.Limit).
                Add("page", httpreq.ToInt, &data.Page).
                Add("fields", httpreq.ToCommaList, &data.Fields).
                Add("timestamp", httpreq.ToTSTime, &data.Timestamp).
                Parse(req.Form); err != nil {
                http.Error(w, err.Error(), http.StatusBadRequest)
                return
        }

        _ = json.NewEncoder(w).Encode(data)
}

func main() {
        http.HandleFunc("/", handler)
        log.Fatal(http.ListenAndServe(":8080", nil))
        // curl 'http://localhost:8080?timestamp=1437743020&limit=10&page=1&fields=a,b,c'
}

Benchmarks

Single CPU

BenchmarkRawLiteral           5000000        410 ns/op       96 B/op        2 allocs/op
BenchmarkRawAdd               1000000       1094 ns/op      384 B/op        5 allocs/op
BenchmarkRawJSONUnmarshal      500000       3038 ns/op      416 B/op       11 allocs/op

8 CPUs

BenchmarkRawPLiteral-8        5000000        299 ns/op       96 B/op        2 allocs/op
BenchmarkRawPAdd-8            2000000        766 ns/op      384 B/op        5 allocs/op
BenchmarkRawPJSONUnmarshal-8  1000000       1861 ns/op      416 B/op       11 allocs/op
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.