query

package
v3.30.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package query provides a parser for the right-hand side query part of the bloblang spec. This is useful as a separate package as it is used in isolation within interpolation functions.

Index

Constants

This section is empty.

Variables

View Source
var ErrDivideByZero = errors.New("attempted to divide by zero")

ErrDivideByZero occurs when an arithmetic operator is prevented from dividing a value by zero.

View Source
var (
	ErrNoContext = errors.New("context was undefined")
)

Common query errors.

Functions

func ExecToBytes

func ExecToBytes(fn Function, ctx FunctionContext) []byte

ExecToBytes returns a byte slice from a function exection.

func ExecToString

func ExecToString(fn Function, ctx FunctionContext) string

ExecToString returns a string from a function exection.

func IClone

func IClone(root interface{}) interface{}

IClone performs a deep copy of a generic value.

func IGetBool

func IGetBool(v interface{}) (bool, error)

IGetBool takes a boxed value and attempts to extract a boolean from it.

func IGetInt

func IGetInt(v interface{}) (int64, error)

IGetInt takes a boxed value and attempts to extract an integer (int64) from it.

func IGetNumber

func IGetNumber(v interface{}) (float64, error)

IGetNumber takes a boxed value and attempts to extract a number (float64) from it.

func IGetString

func IGetString(v interface{}) (string, error)

IGetString takes a boxed value and attempts to return a string value. Returns an error if the value is not a string or byte slice.

func IIsNull

func IIsNull(i interface{}) bool

IIsNull returns whether a bloblang type is null, this includes Delete and Nothing types.

func ISanitize

func ISanitize(i interface{}) interface{}

ISanitize takes a boxed value of any type and attempts to convert it into one of the following types: string, []byte, int64, uint64, float64, bool, []interface{}, map[string]interface{}, Delete, Nothing.

func IToBool

func IToBool(v interface{}) (bool, error)

IToBool takes a boxed value and attempts to extract a boolean from it or parse it into a bool.

func IToBytes

func IToBytes(i interface{}) []byte

IToBytes takes a boxed value of any type and attempts to convert it into a byte slice.

func IToInt added in v3.30.0

func IToInt(v interface{}) (int64, error)

IToInt takes a boxed value and attempts to extract a number (int64) from it or parse one.

func IToNumber

func IToNumber(v interface{}) (float64, error)

IToNumber takes a boxed value and attempts to extract a number (float64) from it or parse one.

func IToString

func IToString(i interface{}) string

IToString takes a boxed value of any type and attempts to convert it into a string.

func ListFunctions

func ListFunctions() []string

ListFunctions returns a slice of function names, sorted alphabetically.

func ListMethods

func ListMethods() []string

ListMethods returns a slice of method names, sorted alphabetically.

func NewArrayLiteral added in v3.25.0

func NewArrayLiteral(values ...interface{}) interface{}

NewArrayLiteral creates an array literal from a slice of values. If all values are static then a static []interface{} value is returned. However, if any values are dynamic a Function is returned.

func NewMapLiteral added in v3.25.0

func NewMapLiteral(values [][2]interface{}) (interface{}, error)

NewMapLiteral creates a map literal from a slice of key/value pairs. If all keys and values are static then a static map[string]interface{} value is returned. However, if any keys or values are dynamic a Function is returned.

func RegisterFunction

func RegisterFunction(spec FunctionSpec, allowDynamicArgs bool, ctor FunctionCtor, checks ...ArgCheckFn) struct{}

RegisterFunction to be accessible from Bloblang queries. Returns an empty struct in order to allow inline calls.

func RegisterMethod

func RegisterMethod(spec MethodSpec, allowDynamicArgs bool, ctor MethodCtor, checks ...ArgCheckFn) struct{}

RegisterMethod to be accessible from Bloblang queries. Returns an empty struct in order to allow inline calls.

Types

type ArgCheckFn

type ArgCheckFn func(args []interface{}) error

ArgCheckFn is an optional argument type checker for a function constructor.

func ExpectAllStringArgs

func ExpectAllStringArgs() ArgCheckFn

ExpectAllStringArgs returns an error if any argument is not a string type (or a byte slice that can be converted).

func ExpectAtLeastOneArg

func ExpectAtLeastOneArg() ArgCheckFn

ExpectAtLeastOneArg returns an error unless >0 arguments are specified.

func ExpectBetweenNAndMArgs added in v3.27.0

func ExpectBetweenNAndMArgs(n, m int) ArgCheckFn

ExpectBetweenNAndMArgs returns an error unless between N and M arguments are specified.

func ExpectBoolArg

func ExpectBoolArg(i int) ArgCheckFn

ExpectBoolArg returns an error if an argument i is not a boolean type.

func ExpectFloatArg

func ExpectFloatArg(i int) ArgCheckFn

ExpectFloatArg returns an error if an argument i is not a float type.

func ExpectIntArg

func ExpectIntArg(i int) ArgCheckFn

ExpectIntArg returns an error if an argument i is not an integer type.

func ExpectNArgs

func ExpectNArgs(i int) ArgCheckFn

ExpectNArgs returns an error unless exactly N arguments are specified.

func ExpectOneOrZeroArgs

func ExpectOneOrZeroArgs() ArgCheckFn

ExpectOneOrZeroArgs returns an error if more than one arg is specified.

func ExpectStringArg

func ExpectStringArg(i int) ArgCheckFn

ExpectStringArg returns an error if an argument i is not a string type (or a byte slice that can be converted).

type ArithmeticOperator added in v3.25.0

type ArithmeticOperator int

ArithmeticOperator represents an arithmetic operation that combines the results of two query functions.

const (
	ArithmeticAdd ArithmeticOperator = iota
	ArithmeticSub
	ArithmeticDiv
	ArithmeticMul
	ArithmeticMod
	ArithmeticEq
	ArithmeticNeq
	ArithmeticGt
	ArithmeticLt
	ArithmeticGte
	ArithmeticLte
	ArithmeticAnd
	ArithmeticOr
	ArithmeticPipe
)

All arithmetic operators.

type Delete

type Delete *struct{}

Delete is a special type that serializes to `null` when forced but indicates a target should be deleted.

type ErrRecoverable

type ErrRecoverable struct {
	Recovered interface{}
	Err       error
}

ErrRecoverable represents a function execution error that can optionally be recovered into a zero-value.

func (*ErrRecoverable) Error

func (e *ErrRecoverable) Error() string

Error implements the standard error interface.

func (*ErrRecoverable) Unwrap added in v3.25.0

func (e *ErrRecoverable) Unwrap() error

Unwrap the error.

type ExampleSpec added in v3.26.0

type ExampleSpec struct {
	Mapping string
	Summary string
	Results [][2]string
}

ExampleSpec provides a mapping example and some input/output results to display.

func NewExampleSpec added in v3.26.0

func NewExampleSpec(summary, mapping string, results ...string) ExampleSpec

NewExampleSpec creates a new example spec.

type Function

type Function interface {
	// Execute this function for a message of a batch.
	Exec(ctx FunctionContext) (interface{}, error)

	// Return a map of target types to path segments for any targets that this
	// query function references.
	QueryTargets(ctx TargetsContext) []TargetPath
}

Function takes a set of contextual arguments and returns the result of the query.

func ClosureFunction added in v3.25.0

func ClosureFunction(
	exec func(ctx FunctionContext) (interface{}, error),
	queryTargets func(ctx TargetsContext) []TargetPath,
) Function

ClosureFunction allows you to define a Function using closures, this is a convenient constructor for function implementations that don't manage complex state.

func DeprecatedFunction added in v3.25.0

func DeprecatedFunction(name, arg string) (Function, bool)

DeprecatedFunction attempts to initialize a (now deprecated) old-syntax function.

func InitFunction added in v3.25.0

func InitFunction(name string, args ...interface{}) (Function, error)

InitFunction attempts to initialise a function by its name and arguments.

func InitMethod added in v3.25.0

func InitMethod(name string, target Function, args ...interface{}) (Function, error)

InitMethod attempts to initialise a method by its name, target function and arguments.

func NewArithmeticExpression added in v3.25.0

func NewArithmeticExpression(fns []Function, ops []ArithmeticOperator) (Function, error)

NewArithmeticExpression creates a single query function from a list of child functions and the arithmetic operator types that chain them together. The length of functions must be exactly one fewer than the length of operators.

func NewFieldFunction added in v3.25.0

func NewFieldFunction(pathStr string) Function

NewFieldFunction creates a query function that returns a path from a JSON input document.

func NewGetMethod added in v3.25.0

func NewGetMethod(target Function, path string) (Function, error)

NewGetMethod creates a new get method.

func NewIfFunction added in v3.25.0

func NewIfFunction(queryFn Function, ifFn Function, elseFn Function) Function

NewIfFunction creates a logical if expression from a query which should return a boolean value. If the returned boolean is true then the ifFn is executed and returned, otherwise elseFn is executed and returned.

func NewMapMethod added in v3.25.0

func NewMapMethod(target, mapArg Function) (Function, error)

NewMapMethod attempts to create a map method.

func NewMatchFunction added in v3.25.0

func NewMatchFunction(contextFn Function, cases ...MatchCase) Function

NewMatchFunction takes a contextual mapping and a list of MatchCases, when the function is executed

func NewVarFunction added in v3.25.0

func NewVarFunction(path string) Function

NewVarFunction creates a new variable function.

func Not added in v3.25.0

func Not(fn Function) Function

Not returns a logical NOT of a child function.

type FunctionCategory added in v3.26.0

type FunctionCategory string

FunctionCategory is an abstract title for functions of a similar purpose.

var (
	FunctionCategoryGeneral     FunctionCategory = "General"
	FunctionCategoryMessage     FunctionCategory = "Message Info"
	FunctionCategoryEnvironment FunctionCategory = "Environment"
)

Function categories.

type FunctionContext

type FunctionContext struct {
	Value    func() *interface{}
	Maps     map[string]Function
	Vars     map[string]interface{}
	Index    int
	MsgBatch MessageBatch
	Legacy   bool
}

FunctionContext provides access to a range of query targets for functions to reference.

func (FunctionContext) WithValue added in v3.28.0

func (ctx FunctionContext) WithValue(v interface{}) FunctionContext

WithValue returns a function context with a new value.

type FunctionCtor

type FunctionCtor func(args ...interface{}) (Function, error)

FunctionCtor constructs a new function from input arguments.

type FunctionSpec added in v3.26.0

type FunctionSpec struct {
	// A category to place the function within.
	Category FunctionCategory

	// Name of the function (as it appears in config).
	Name string

	// Description of the functions purpose (in markdown).
	Description string

	// Examples shows general usage for the function.
	Examples []ExampleSpec

	// Beta describes whether this function is a beta component.
	Beta bool

	// Deprecated is true for functions that are deprecated and only exist for
	// backwards compatibility reasons.
	Deprecated bool
}

FunctionSpec describes a Bloblang function.

func FunctionDocs added in v3.26.0

func FunctionDocs() []FunctionSpec

FunctionDocs returns a slice of specs, one for each function.

func NewDeprecatedFunctionSpec added in v3.26.0

func NewDeprecatedFunctionSpec(name string) FunctionSpec

NewDeprecatedFunctionSpec creates a new function spec that is deprecated. The function will not appear in docs or searches but will still be usable in mappings.

func NewFunctionSpec added in v3.26.0

func NewFunctionSpec(category FunctionCategory, name, description string, examples ...ExampleSpec) FunctionSpec

NewFunctionSpec creates a new function spec.

func (FunctionSpec) IsBeta added in v3.28.0

func (s FunctionSpec) IsBeta(b bool) FunctionSpec

IsBeta sets whether the function is a beta component.

type Literal added in v3.25.0

type Literal struct {
	Value interface{}
}

Literal wraps a static value and returns it for each invocation of the function.

func NewLiteralFunction added in v3.25.0

func NewLiteralFunction(v interface{}) *Literal

NewLiteralFunction creates a query function that returns a static, literal value.

func (*Literal) Close added in v3.28.0

func (l *Literal) Close(ctx context.Context) error

Close does nothing.

func (*Literal) Exec added in v3.25.0

func (l *Literal) Exec(ctx FunctionContext) (interface{}, error)

Exec returns a literal value.

func (*Literal) QueryTargets added in v3.25.0

func (l *Literal) QueryTargets(ctx TargetsContext) []TargetPath

QueryTargets returns nothing.

type MatchCase added in v3.25.0

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

MatchCase represents a single match case of a match expression, where a case query is checked and, if true, the underlying query is executed and returned.

func NewMatchCase added in v3.25.0

func NewMatchCase(caseFn, queryFn Function) MatchCase

NewMatchCase creates a single match case of a match expression, where a case query is checked and, if true, the underlying query is executed and returned.

type MessageBatch

type MessageBatch interface {
	Get(p int) types.Part
	Len() int
}

MessageBatch is an interface type to be given to a query function, it allows the function to resolve fields and metadata from a Benthos message batch.

type MethodCatSpec added in v3.26.0

type MethodCatSpec struct {
	Category    MethodCategory
	Description string
	Examples    []ExampleSpec
}

MethodCatSpec describes how a method behaves in the context of a given category.

type MethodCategory added in v3.26.0

type MethodCategory string

MethodCategory is an abstract title for methods of a similar purpose.

var (
	MethodCategoryStrings        MethodCategory = "String Manipulation"
	MethodCategoryTime           MethodCategory = "Timestamp Manipulation"
	MethodCategoryRegexp         MethodCategory = "Regular Expressions"
	MethodCategoryEncoding       MethodCategory = "Encoding and Encryption"
	MethodCategoryCoercion       MethodCategory = "Type Coercion"
	MethodCategoryParsing        MethodCategory = "Parsing"
	MethodCategoryObjectAndArray MethodCategory = "Object & Array Manipulation"
)

Method categories.

type MethodCtor

type MethodCtor func(target Function, args ...interface{}) (Function, error)

MethodCtor constructs a new method from a target function and input args.

type MethodSpec added in v3.26.0

type MethodSpec struct {
	// Name of the method (as it appears in config).
	Name string

	// Description of the method purpose (in markdown).
	Description string

	// Examples shows general usage for the method.
	Examples []ExampleSpec

	// Categories that this method fits within.
	Categories []MethodCatSpec

	// Beta describes whether this function is a beta component.
	Beta bool

	// Deprecated is true for methods that are deprecated and only exist for
	// backwards compatibility reasons.
	Deprecated bool
}

MethodSpec describes a Bloblang method.

func MethodDocs added in v3.26.0

func MethodDocs() []MethodSpec

MethodDocs returns a slice of specs, one for each method.

func NewDeprecatedMethodSpec added in v3.26.0

func NewDeprecatedMethodSpec(name string) MethodSpec

NewDeprecatedMethodSpec creates a new method spec that is deprecated. The method will not appear in docs or searches but will still be usable in mappings.

func NewMethodSpec added in v3.26.0

func NewMethodSpec(name, description string, examples ...ExampleSpec) MethodSpec

NewMethodSpec creates a new method spec.

func (MethodSpec) InCategory added in v3.26.0

func (m MethodSpec) InCategory(category MethodCategory, description string, examples ...ExampleSpec) MethodSpec

InCategory describes the methods behaviour in the context of a given category, methods can belong to multiple categories. For example, the `contains` method behaves differently in the object and array category versus the strings one, but belongs in both.

func (MethodSpec) IsBeta added in v3.28.0

func (m MethodSpec) IsBeta(b bool) MethodSpec

IsBeta sets whether the function is a beta component.

type Nothing

type Nothing *struct{}

Nothing is a special type that serializes to `null` when forced but indicates a query should be disregarded (and not mapped).

type TargetPath added in v3.25.0

type TargetPath struct {
	Type TargetType
	Path []string
}

TargetPath represents a target type and segmented path that a query function references. An empty path indicates the root of the type is targetted.

func NewTargetPath added in v3.25.0

func NewTargetPath(t TargetType, Path ...string) TargetPath

NewTargetPath constructs a new target path from a type and zero or more path segments.

type TargetType added in v3.25.0

type TargetType int

TargetType represents a query target type, which is a source of information that a query might require, such as metadata, structured message contents, the context, etc.

const (
	TargetMetadata TargetType = iota
	TargetValue
	TargetVariable
)

TargetTypes

type TargetsContext added in v3.26.0

type TargetsContext struct {
	Maps map[string]Function
}

TargetsContext provides access to a range of query targets for functions to reference when determining their targets.

type TypeError

type TypeError struct {
	Expected []ValueType
	Actual   ValueType
	Value    interface{}
}

TypeError represents an error where a value of a type was required for a function, method or operator but instead a different type was found.

func NewTypeError

func NewTypeError(actual interface{}, exp ...ValueType) *TypeError

NewTypeError creates a new type error.

func (*TypeError) Error

func (t *TypeError) Error() string

Error implements the standard error interface for TypeError.

type TypeMismatch

type TypeMismatch struct {
	Left  ValueType
	Right ValueType
}

TypeMismatch represents an error where two values should be a comparable type but are not.

func NewTypeMismatch

func NewTypeMismatch(left, right interface{}) *TypeMismatch

NewTypeMismatch creates a new type mismatch error.

func (*TypeMismatch) Error

func (t *TypeMismatch) Error() string

Error implements the standard error interface.

type ValueType

type ValueType string

ValueType represents a discrete value type supported by Bloblang queries.

var (
	ValueString  ValueType = "string"
	ValueBytes   ValueType = "bytes"
	ValueNumber  ValueType = "number"
	ValueBool    ValueType = "bool"
	ValueArray   ValueType = "array"
	ValueObject  ValueType = "object"
	ValueNull    ValueType = "null"
	ValueDelete  ValueType = "delete"
	ValueNothing ValueType = "nothing"
	ValueUnknown ValueType = "unknown"
)

ValueType variants.

func ITypeOf

func ITypeOf(i interface{}) ValueType

ITypeOf returns the type of a boxed value as a discrete ValueType. If the type of the value is unknown then ValueUnknown is returned.

Jump to

Keyboard shortcuts

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