query

package
v0.0.0-...-2935fa7 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package query provides a parser for a custom query format:

abci.invoice.number=22 AND abci.invoice.owner=Ivan

See query.peg for the grammar, which is a https://en.wikipedia.org/wiki/Parsing_expression_grammar. More: https://github.com/PhilippeSigaud/Pegged/wiki/PEG-Basics

It has a support for numbers (integer and floating point), dates and times.

Index

Constants

View Source
const (
	// DateLayout defines a layout for all dates (`DATE date`)
	DateLayout = "2006-01-02"
	// TimeLayout defines a layout for all times (`TIME time`)
	TimeLayout = time.RFC3339
)
View Source
const (
	MultipleValueTagSeparator = ";"
)

Variables

This section is empty.

Functions

func AsQueryable

func AsQueryable(query Query) parsedQuery

func GetReflect

func GetReflect(rv reflect.Value, key string) (interface{}, bool)

func GetReflectDepth

func GetReflectDepth(rv reflect.Value, key string, maxDepth int) (interface{}, bool)

Pull out values in a nested struct by following path

func IsEmpty

func IsEmpty(query Query) bool

func StringFromValue

func StringFromValue(value interface{}) string

func TaggedPrefix

func TaggedPrefix(prefix string, tagged Tagged) *taggedPrefix

Types

type Builder

type Builder struct {

	// reusable buffer for building queryString
	bytes.Buffer
	// contains filtered or unexported fields
}

A fluent query builder

func NewBuilder

func NewBuilder(queries ...string) *Builder

Creates a new query builder with a base query that is the conjunction of all queries passed

func (*Builder) And

func (qb *Builder) And(queryBuilders ...*Builder) *Builder

Creates the conjunction of Builder and rightQuery

func (*Builder) AndContains

func (qb *Builder) AndContains(tag string, operand interface{}) *Builder

func (*Builder) AndEquals

func (qb *Builder) AndEquals(tag string, operand interface{}) *Builder

Creates the conjunction of Builder and tag = operand

func (*Builder) AndGreaterThanOrEqual

func (qb *Builder) AndGreaterThanOrEqual(tag string, operand interface{}) *Builder

func (*Builder) AndLessThanOrEqual

func (qb *Builder) AndLessThanOrEqual(tag string, operand interface{}) *Builder

func (*Builder) AndNotEquals

func (qb *Builder) AndNotEquals(tag string, operand interface{}) *Builder

func (*Builder) AndStrictlyGreaterThan

func (qb *Builder) AndStrictlyGreaterThan(tag string, operand interface{}) *Builder

func (*Builder) AndStrictlyLessThan

func (qb *Builder) AndStrictlyLessThan(tag string, operand interface{}) *Builder

func (*Builder) Not

func (qb *Builder) Not() *Builder

func (*Builder) Or

func (qb *Builder) Or(queryBuilders ...*Builder) *Builder

func (*Builder) Query

func (qb *Builder) Query() (Query, error)

func (*Builder) String

func (qb *Builder) String() string

type CombinedTags

type CombinedTags []interface{}

func TagsFor

func TagsFor(vs ...interface{}) CombinedTags

func (CombinedTags) Get

func (ct CombinedTags) Get(key string) (interface{}, bool)

type Condition

type Condition struct {
	Tag     string
	Op      Operator
	Operand interface{}
}

Condition represents a single condition within a query and consists of tag (e.g. "tx.gas"), operator (e.g. "=") and operand (e.g. "7").

type Empty

type Empty struct {
}

Empty query matches any set of tags.

func (Empty) MatchError

func (empty Empty) MatchError() error

func (Empty) Matches

func (Empty) Matches(tags Tagged) bool

Matches always returns true.

func (Empty) Query

func (Empty) Query() (Query, error)

func (Empty) String

func (Empty) String() string

type Expression

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

A Boolean expression for the query grammar

func (*Expression) Date

func (e *Expression) Date(value string)

func (*Expression) Evaluate

func (e *Expression) Evaluate(getTagValue func(tag string) (interface{}, bool)) (bool, error)

Evaluate expects an Execute() to have filled the code of the Expression so it can be run in the little stack machine below

func (*Expression) Number

func (e *Expression) Number(value string)

func (*Expression) Operator

func (e *Expression) Operator(operator Operator)

func (*Expression) String

func (e *Expression) String() string

func (*Expression) Tag

func (e *Expression) Tag(value string)

func (*Expression) Time

func (e *Expression) Time(value string)

func (*Expression) Value

func (e *Expression) Value(value string)

type MatchError

type MatchError struct {
	Tagged Tagged
	Cause  error
}

func (*MatchError) Error

func (m *MatchError) Error() string

type Operator

type Operator uint8

Operator is an operator that defines some kind of relation between tag and operand (equality, etc.).

const (
	OpTerminal Operator = iota
	OpAnd
	OpOr
	OpLessEqual
	OpGreaterEqual
	OpLess
	OpGreater
	OpEqual
	OpContains
	OpNotEqual
	OpNot
)

func (Operator) Arity

func (op Operator) Arity() int

func (Operator) String

func (op Operator) String() string

type PegQuery

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

Query holds the query string and the query parser.

func MustParse

func MustParse(s string) *PegQuery

MustParse turns the given string into a query or panics; for tests or others cases where you know the string is valid.

func New

func New(s string) (*PegQuery, error)

New parses the given string and returns a query or error if the string is invalid.

func (*PegQuery) ExplainTo

func (q *PegQuery) ExplainTo(explainer func(fmt string, args ...interface{}))

func (*PegQuery) MatchError

func (q *PegQuery) MatchError() error

Returns whether a matching error occurred (which would result in a false from Matches)

func (*PegQuery) Matches

func (q *PegQuery) Matches(tags Tagged) bool

Matches returns true if the query matches the given set of tags, false otherwise.

For example, query "name=John" matches tags = {"name": "John"}. More examples could be found in parser_test.go and query_test.go.

func (*PegQuery) Query

func (q *PegQuery) Query() (Query, error)

func (*PegQuery) String

func (q *PegQuery) String() string

String returns the original string.

type Query

type Query interface {
	Matches(tags Tagged) bool
	String() string
	MatchError() error
}

func Must

func Must(qry Query, err error) Query

func NewOrEmpty

func NewOrEmpty(queryString string) (Query, error)

type QueryParser

type QueryParser struct {
	Expression

	Buffer string

	Parse  func(rule ...int) error
	Reset  func()
	Pretty bool
	// contains filtered or unexported fields
}

func (*QueryParser) AST

func (t *QueryParser) AST() *node32

func (*QueryParser) Add

func (t *QueryParser) Add(rule pegRule, begin, end, depth uint32, index int)

func (*QueryParser) Error

func (t *QueryParser) Error() []token32

func (*QueryParser) Execute

func (p *QueryParser) Execute()

func (*QueryParser) Expand

func (t *QueryParser) Expand(index int)

func (*QueryParser) Highlighter

func (p *QueryParser) Highlighter()

func (*QueryParser) Init

func (p *QueryParser) Init()

func (*QueryParser) Order

func (t *QueryParser) Order() [][]token32

func (*QueryParser) PreOrder

func (t *QueryParser) PreOrder() (<-chan state32, [][]token32)

func (*QueryParser) Print

func (t *QueryParser) Print()

func (*QueryParser) PrintSyntax

func (t *QueryParser) PrintSyntax()

func (*QueryParser) PrintSyntaxTree

func (p *QueryParser) PrintSyntaxTree()

func (*QueryParser) Tokens

func (t *QueryParser) Tokens() <-chan token32

type Queryable

type Queryable interface {
	Query() (Query, error)
}

func MatchAllQueryable

func MatchAllQueryable() Queryable

type String

type String string

A yet-to-be-parsed query

func (String) Query

func (qs String) Query() (Query, error)

type TagMap

type TagMap map[string]interface{}

func (TagMap) Get

func (ts TagMap) Get(key string) (value interface{}, ok bool)

type Tagged

type Tagged interface {
	Get(key string) (value interface{}, ok bool)
}

Jump to

Keyboard shortcuts

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