Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/creack/httpreq
The package provides an easy way to "unmarshal" query string data into a struct. Without reflect.
https://github.com/creack/httpreq
Last synced: about 1 month ago
JSON representation
The package provides an easy way to "unmarshal" query string data into a struct. Without reflect.
- Host: GitHub
- URL: https://github.com/creack/httpreq
- Owner: creack
- License: mit
- Created: 2015-07-24T12:54:54.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-08-01T14:56:21.000Z (over 8 years ago)
- Last Synced: 2024-06-19T00:36:33.521Z (6 months ago)
- Language: Go
- Size: 16.6 KB
- Stars: 35
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httpreq
The package provides an easy way to "unmarshal" query string data into a struct. Without reflect.
[![GitHub release](https://img.shields.io/github/release/creack/httpreq.svg?maxAge=2592000)]() [![GoDoc](https://godoc.org/github.com/creack/httpreq?status.svg)](https://godoc.org/github.com/creack/httpreq) [![Build Status](https://travis-ci.org/creack/httpreq.svg)](https://travis-ci.org/creack/httpreq) [![Coverage Status](https://coveralls.io/repos/github/creack/httpreq/badge.svg?branch=master)](https://coveralls.io/github/creack/httpreq?branch=master)
# Example
## Literal
```go
package mainimport (
"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
```go
package mainimport (
"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
```