query package - github.com/jawher/bateau/query - Go Packages

query

package
v0.0.0-...-cfb2c22 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2015 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package query provides the means the filter a collection of arbitrary data structures according to user provided query.

Here's an example for a query over docker containers:

matcher, err := query.Parse("!running & (exit!=0 | name~junk", ...)
if matcher.Match(container) {
  :
  :
}

The queries supported by this library are parsed from a string represetnation according to the following rules:

Boolean fields

The simplest form a query is a simple boolean field:

running

String fields

Fields of type string are supported and can be used with the following operators:

  • "=" : Exact equality name=library/container
  • "!=" : Not equal to: name!=value
  • "~" : Like, i.e. the field value must contain the provided value: name~value
  • "!~" : Not like: name!~value

String slice fields

bateau/query also supports multi-valued fields. The same operators as for string fields (=, !=, ~, !~) are available for multi-valued fields.

  • "=" : Matches if any value of the slice is exactly equal to the provided value cmd=/bin/sh
  • "!=" : Fails if any value of the slice is exactly equal to the provided value cmd!=/bin/sh
  • "~" : Matches if any value of the slice contains the provided value cmd~sh
  • "!~" : Fails if any value of the slice contains the provided value cmd!~sh

Negation

A condition can be negated using the "!" operator, e.g.:

!running

or

!name=server

And

Two or more conditions can be combined using the boolean and operator "&"

running & name=server

or

running & image=mongo & cmd~sh

Or

Two or more conditions can be combined using the boolean or operator "|"

running | name=server

or

running | image=mongo | cmd~sh

Parenthesis

To control the evaluation precedence, conditions can be wrapped between "(" and ")":

running & !(name=server | image~mongo)

Grammar

The query langauge is described below using the EBNF notation:

expr     -> or
or       -> and ('|' and)*
and      -> atom ('&' atom)*
atom     -> cond | '(' expr ')' | '!' atom
cond     -> LITERAL (OPERATOR LITERAL)?
LITERAL  -> "[^|&!=~]+"
OPERATOR -> '=' | '!=' | '~' | '!~' | '<' | '<=' | '>' | '>='

Index

Constants

View Source
const (
	IS   Operator = ""
	EQ            = "="
	LIKE          = "~"
	GT            = ">"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Expression

type Expression interface {
	// Match accepts or rejects a queryable depending on the expression implementation
	Match(queryable Queryable) bool
}

Expression is a predicate which can be applied to a queryable

func Parse

func Parse(input string, fields map[string][]Operator) (Expression, error)

Parse accepts an input string and the list and types of valid fields and returns either a matcher expression if the query is valid, or else an error

type Operator

type Operator string

type ParseError

type ParseError struct {
	// The original query
	Input string
	// The position where the parsing fails
	Pos int
	// The error message
	Message string
}

ParseError is returned if a query cannot be successfuly parsed

func (ParseError) Error

func (e ParseError) Error() string

type Queryable

type Queryable interface {
	// Returns true if the provided field is set
	Is(field string, operator Operator, value string) bool
}

Queryable is the abstraction used by this library to work on arbitrary data structures. Implementing this interface's methods for a user type makes it compatible with the Expression predicates

Jump to

Keyboard shortcuts

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