queryparser

package module
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2022 License: MIT Imports: 5 Imported by: 0

README

go-queryparser -- "q" http GET variable parser that supports filters/tags for advanced searches :thumbsup:

Code Coverage Go Documentation Go Report Card Bug reports Feature requests Open Pull Requests Latest Semver Tag Last commit Ask a Question Discord Chat

🔗 Table of Contents

What?

go-queryparser parses a common "q" http GET variable to strip out filters, which can be used for advanced searching, like:

Hello World tags:example,world foo:"something quoted" author:lrstanley

Use:

This will pull from master (currently v2):

$ go get -u github.com/lrstanley/go-queryparser@latest

Example:

package main

import (
	"fmt"
	"net/http"

	"github.com/lrstanley/go-queryparser/v2"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		q := queryparser.Parse(r.FormValue("q"))
		if q.Has("author") {
			fmt.Fprintf(w, "filtering by author %q!", q.GetOne("author"))
			return
		}

		fmt.Fprint(w, "no filtering requested!")
	})

	http.ListenAndServe(":8080", nil)
}
$ curl -s localhost:8080
no filtering requested!
$ curl -s 'localhost:8080?q=author:"liam"'
filtering by author "liam"!

The main benefit is for user input boxes where you want additional filtering, like the Github issues search box, or similar.

🙋♂ Support & Assistance

  • ❤ Please review the Code of Conduct for guidelines on ensuring everyone has the best experience interacting with the community.
  • 🙋♂ Take a look at the support document on guidelines for tips on how to ask the right questions.
  • 🐞 For all features/bugs/issues/questions/etc, head over here.

🤝 Contributing

  • ❤ Please review the Code of Conduct for guidelines on ensuring everyone has the best experience interacting with the community.
  • 📋 Please review the contributing doc for submitting issues/a guide on submitting pull requests and helping out.
  • 🗝 For anything security related, please review this repositories security policy.

⚖ License

MIT License

Copyright (c) 2017 Liam Stanley <me@liamstanley.io>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Also located here

Documentation

Overview

Package queryparser parses a common "q" http GET variable to look for text filters, which can be used for advanced searching. For example:

Hello World tags:example,world foo:"something quoted" author:lrstanley

go-queryparser also strips out not very safe/useful characters by default. See the DefaultCut function for details (and New() for how you can do this yourself.)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultCut

func DefaultCut(r rune) (strip bool)

DefaultCut is the default cut function, which allowed stripping out potentially unwanted characters from filter fields and raw text. Only allows " _,-.:A-Za-z0-9" (or unicode equivalents).

Types

type Options

type Options struct {
	// CutFn allows excluding specific characters from being allowed within
	// the filter fields. When the function returns true on a rune, it will
	// be excluded from the filter field.
	CutFn func(rune) bool
	// Allowed is a slice of allowed filter names. If no allowed filter names
	// are provided, all are considered allowed.
	Allowed []string
}

Options allow the adjustment of allowed filters and characters.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser represents a parser.

func New

func New(query string, opt Options) *Parser

New returns a new instance of Parser. Make sure Parser.Parser() is called or this will leak goroutines.

func (*Parser) Parse

func (p *Parser) Parse() *Query

Parse parses the input query and returns a new instance of Query if there were no errors.

type Query

type Query struct {
	// Raw is the raw (trailing) text of items that weren't filters.
	Raw string
	// contains filtered or unexported fields
}

Query represents filtered input.

func Parse

func Parse(query string) *Query

Parse is a higher level helper method to return a query from a query string.

func (*Query) Get

func (q *Query) Get(key string) (results []string, ok bool)

Get returns the results of the filter if it exists, and if it successfully found a result.

func (*Query) GetOne

func (q *Query) GetOne(key string) string

GetOne returns the last known result for the filter, if it exists. Useful if you only want a user to define a filter once. The resulting string is empty if no filter of that key was found.

func (*Query) Has

func (q *Query) Has(key string) (exists bool)

Has returns true if there is a filter matching the given name.

func (*Query) String

func (q *Query) String() (out string)

String will return a string representation of the query, however all filters will be sorted alphabetically, and the raw text will be placed at the end of the string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL