Query strings for Go (opinionated!) https://pkg.go.dev/github.com/annybs/go-qs

Aneurin Barker Snook 05ba1ccf56 ensure go.sum not committed 10 місяців тому
.gitignore 05ba1ccf56 ensure go.sum not committed 10 місяців тому
LICENSE.md 0d6091b30c duplicate license to each package 11 місяців тому
README.md 2fbbf5d70f fix link to license 10 місяців тому
filter.go 7b189b333e start adding more complex filter support 1 рік тому
filter_test.go 7f3dc0c79d add validation error type, fix tests, add number tests 1 рік тому
go.mod ec866af4ef migrate from recipeer org to annybs 1 рік тому
join.go d94f155714 add qs joins 1 рік тому
join_test.go 7f3dc0c79d add validation error type, fix tests, add number tests 1 рік тому
page.go d94f155714 add qs joins 1 рік тому
page_test.go 7f3dc0c79d add validation error type, fix tests, add number tests 1 рік тому
pagination.go 137dc89f64 rename query package to qs 1 рік тому
pagination_test.go 7f3dc0c79d add validation error type, fix tests, add number tests 1 рік тому
sort.go 137dc89f64 rename query package to qs 1 рік тому
sort_test.go 7f3dc0c79d add validation error type, fix tests, add number tests 1 рік тому

README.md

Go Query String

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!

Features

This package includes support for:

  • Filters filter=title eq Bolognese&filter=serves gte 4
  • Joins join=author&join=ingredient
  • Pagination limit=10&offset=5&page=3 (note: offset overrides page)
  • Sorting sort=title asc&sort=serves asc

You 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.

Example

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.

License

See LICENSE.md