Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ritwickdey/querydecoder
Go Package to populate struct from optional query parameters using 'query' tag
https://github.com/ritwickdey/querydecoder
decoder golang query
Last synced: 2 months ago
JSON representation
Go Package to populate struct from optional query parameters using 'query' tag
- Host: GitHub
- URL: https://github.com/ritwickdey/querydecoder
- Owner: ritwickdey
- License: mit
- Created: 2021-12-31T04:40:23.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-01T11:28:12.000Z (about 3 years ago)
- Last Synced: 2024-06-20T16:35:18.853Z (7 months ago)
- Topics: decoder, golang, query
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# querydecoder
`querydecoder` is a Go Package for populating struct from optional query parameters using 'query' tag. This package can be used to populate value into primitive variable.
#### Example
```go
import (
"github.com/ritwickdey/querydecoder"
)type User struct {
IsSuperUser bool `query:"is_super_user"`
UserName string `query:"user_name"`
UserID int64 `query:"user_id"`
}// Parse by struct tags
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
u1 := User{}
query := r.URL.Query()
// Decode into struct
err := querydecoder.New(query).Decode(&u1)
if err != nil {
panic(err)
}
log.Println(u1)}
// OR,
// Parse by key
func ServeHTTP2(w http.ResponseWriter, r *http.Request) {
var isDog bool
err := querydecoder.New(query).DecodeField("is_dog", &isDog)
// if `is_dog` query param is not there, it'll not modity variable.if err != nil {
panic(err)
}}
```
#### Benchmark
```
goos: darwin
goarch: arm64 (Mac M1 Chip)
pkg: github.com/ritwickdey/querydecoderBenchmarkDecode-8 2019590 603.1 ns/op //Parse by struct tags
BenchmarkDecodeField-8 10521158 113.1 ns/op //Parse by key
BenchmarkManualDecode-8 25947067 46.74 ns/op //Manual parsingBenchmarkJsonUnmarshal-8 883681 1413 ns/op //json.Unmarshal - Unrelated, but added to compare.
```