Query strings for Go (opinionated!) https://pkg.go.dev/github.com/annybs/go-qs
|
|
1 year ago | |
|---|---|---|
| .github | 1 year ago | |
| .gitignore | 1 year ago | |
| LICENSE.md | 1 year ago | |
| README.md | 1 year ago | |
| filter.go | 2 years ago | |
| filter_test.go | 1 year ago | |
| go.mod | 1 year ago | |
| join.go | 2 years ago | |
| join_test.go | 1 year ago | |
| page.go | 2 years ago | |
| page_test.go | 1 year ago | |
| pagination.go | 2 years ago | |
| pagination_test.go | 1 year ago | |
| sort.go | 2 years ago | |
| sort_test.go | 1 year ago |
An opinionated suite of functions to help read common query strings into useful objects, mainly focused on data querying.
This particular package is primarily for the author's own use; if you want to pattern your query strings differently, this library is not for you!
This package includes support for:
filter=title eq Bolognese&filter=serves gte 4join=author&join=ingredientlimit=10&offset=5&page=3 (note: offset overrides page)sort=title asc&sort=serves ascYou can read these individually or use the ReadPage() function to retrieve a convenient Page object that's easy to pass along to your querying code.
package main
import (
"net/http"
"github.com/annybs/go/qs"
"github.com/annybs/go/rest"
)
type Handler struct{}
func (*Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
page, err := qs.ReadPage(req.URL.Query(), nil)
if err != nil {
rest.WriteErrorJSON(w, err)
} else {
rest.WriteResponseJSON(w, http.StatusOK, page)
}
}
func main() {
http.ListenAndServe("localhost:8000", &Handler{})
}
Open http://localhost:8000 in your browser and try different query strings (per Features, above) to see an example response object. You can also enter malformed query strings, which will cause a validation error.
See LICENSE.md