expr

package
v0.0.0-...-f9dd102 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(root []*yaml.Node) *ctx

NewContext creates a new context with the given root node.

Types

type ArithmeticExpr

type ArithmeticExpr struct {
	Left, Right Expr
	Operation   ArithmeticOp
}

ArithmeticExpr is a representation of the various arithmetic operations in the YAMLPath grammar, such as '-', '/', '*', and '%'. The only operator not included here is addition, since this is handled by the concat expression.

These operations are only able to operate on numeric values, and will error for all other types.

func (*ArithmeticExpr) Eval

func (e *ArithmeticExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the arithmetic expression against the given context.

type ArithmeticOp

type ArithmeticOp func(lhs, rhs decimal.Decimal) decimal.Decimal

ArithmeticOp is an arithmetic operation that takes two decimal.Decimal values.

var (

	// Subtraction is the arithmetic subtraction operation for decimal objects.
	Subtraction ArithmeticOp = func(lhs, rhs decimal.Decimal) decimal.Decimal {
		return lhs.Sub(rhs)
	}
	// Multiplication is the arithmetic multiplication operation for decimal objects.
	Multiplication ArithmeticOp = func(lhs, rhs decimal.Decimal) decimal.Decimal {
		return lhs.Mul(rhs)
	}
	// Division is the arithmetic division operation for decimal objects.
	Division ArithmeticOp = func(lhs, rhs decimal.Decimal) decimal.Decimal {
		return lhs.Div(rhs)
	}
	// Modulus is the arithmetic modulus operation for decimal objects.
	Modulus ArithmeticOp = func(lhs, rhs decimal.Decimal) decimal.Decimal {
		return lhs.Mod(rhs)
	}
)

type BooleanAndExpr

type BooleanAndExpr struct {
	Left, Right Expr
}

BooleanAndExpr represents a boolean AND expression.

func (*BooleanAndExpr) Eval

func (e *BooleanAndExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the boolean AND expression against the given context.

type BooleanOrExpr

type BooleanOrExpr struct {
	Left, Right Expr
}

BooleanOrExpr represents a boolean OR expression.

func (*BooleanOrExpr) Eval

func (e *BooleanOrExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the boolean OR expression against the given context.

type Comparator

type Comparator func(i int) bool

Comparator is a comparator function that represents a comparison operator.

var (
	// CompareLess represents the '<' operator
	CompareLess Comparator = func(i int) bool { return i < 0 }
	// CompareGreater represents the '>' operator
	CompareGreater Comparator = func(i int) bool { return i > 0 }
	// CompareLessEqual represents the '<=' operator
	CompareLessEqual Comparator = func(i int) bool { return i <= 0 }
	// CompareGreaterEqual represents the '>=' operator
	CompareGreaterEqual Comparator = func(i int) bool { return i >= 0 }
)

type CompareExpr

type CompareExpr struct {
	Left, Right Expr
	Compare     Comparator
}

CompareExpr is a representation of the various comparison operators for YAMLPath ('<=', '<', '>', '>='). Which operator is used here depends solely on the Compare field, which may be one of the Comparator definitions.

func (*CompareExpr) Eval

func (e *CompareExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the comparison expression against the given nodes.

type ConcatExpr

type ConcatExpr struct {
	Left, Right Expr
}

ConcatExpr represents an expression that concatenates two expressions together using the '+' operator.

For strings, this produces a true concatenation of the strings. For integers, this is the addition operator. If either side of the operator is not compatible, this will return an error.

func (*ConcatExpr) Eval

func (e *ConcatExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

type EqualityExpr

type EqualityExpr struct {
	Left, Right Expr
}

EqualityExpr is a representation of the '==' operator in the YAMLPath grammar. This enables comparison across both left and right sub-expressions.

func (*EqualityExpr) Eval

func (e *EqualityExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the equality expression against the given nodes.

type Expr

type Expr interface {
	// Eval evaluates the expression against the given context.
	Eval(ctx invocation.Context) ([]*yaml.Node, error)
}

Expr represents an expression that can be evaluated against a YAML node.

type FieldExpr

type FieldExpr struct {
	Fields []string
}

FieldExpr extracts the fields from the nodes that match the named fields.

func (*FieldExpr) Eval

func (e *FieldExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the field expression by extracting the fields from the nodes that match the named fields.

type FilterExpr

type FilterExpr struct {
	Expr Expr
}

FilterExpr is a representation of the filter operator `[?(...)]` in YAMLPath.

func (*FilterExpr) Eval

func (f *FilterExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the filter expression against the provided nodes.

type FuncExpr

type FuncExpr struct {
	Parameters []invocation.Parameter
	Func       invocation.Func
}

FuncExpr represents an expression that is implemented by a function that takes a context and returns a list of nodes.

func (*FuncExpr) Eval

func (e *FuncExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the expression by invoking the function with the given context and parameters.

type InExpr

type InExpr struct {
	Left, Right Expr
}

InExpr checks for the presence of a single value in a collection of values.

func (*InExpr) Eval

func (e *InExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the in expression by checking if the left-hand side is in the right-hand side.

type IndexExpr

type IndexExpr struct {
	Indices []int64
}

IndexExpr is a representation of the indexing operator `[...]` in YAMLPath for numeric indexing of sequences.

This implements both union-indexing, which allows multiple selection, as well as just individual indexing. If an index is out-of-bounds, it is not selected -- no error is returned.

Negative indices allow selecting from the reverse side.

func (*IndexExpr) Eval

func (i *IndexExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the index expression against the given nodes.

type MatchExpr

type MatchExpr struct {
	Regex *regexp.Regexp
	Expr  Expr
}

MatchExpr represents a regular expression match expression in YAMLPath.

func (*MatchExpr) Eval

func (e *MatchExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the match expression against the given nodes.

type NegationExpr

type NegationExpr struct {
	Expr Expr
}

NegationExpr is a representation of the '!' operator in YAMLPath expressions. It negates the result of the sub-expression.

func (*NegationExpr) Eval

func (e *NegationExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the '!' operator against the provided nodes.

type PrefixMinusExpr

type PrefixMinusExpr struct {
	Expr Expr
}

func (*PrefixMinusExpr) Eval

func (e *PrefixMinusExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

type PrefixPlusExpr

type PrefixPlusExpr struct {
	Expr Expr
}

PrefixPlusExpr is a representation of the unary '+' operator in YAMLPath.

This is effectively an identity expression that will only return the value back to the caller -- with the one exception that it will only operate on numeric values.

func (*PrefixPlusExpr) Eval

func (e *PrefixPlusExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the unary '+' operator against the provided nodes.

type RecursiveDescentExpr

type RecursiveDescentExpr struct{}

RecursiveDescentExpr is a representation of the recursive descent operator in YAMLPath expressions.

func (*RecursiveDescentExpr) Eval

func (r *RecursiveDescentExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the recursive descent operator against the provided nodes.

type RootExpr

type RootExpr struct {
	Root string
}

RootExpr represents a root expression, either '$' or '@' in the path.

func (*RootExpr) Eval

func (e *RootExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the root expression.

type ScriptExpr

type ScriptExpr struct {
	Expr Expr
}

ScriptExpr represents a script expression that can be used to create computed fields in a YAMLPath.

func (*ScriptExpr) Eval

func (e *ScriptExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the script expression by evaluating the expression and using the result as a key to extract a value from the input nodes.

type SequenceExpr

type SequenceExpr []Expr

SequenceExpr is a representation of a sequence of expressions in YAMLPath.

Almost every path is composed of sequence expressions that build up the path. For example, the path `$.foo.bar` is composed of three sequence expressions: `$`, `.foo`, and `.bar`.

func (*SequenceExpr) Append

func (s *SequenceExpr) Append(expr Expr)

Append appends an expression to the sequence.

func (SequenceExpr) Eval

func (s SequenceExpr) Eval(ctx invocation.Context) (nodes []*yaml.Node, err error)

Eval evaluates the sequence of expressions.

type Slice

type Slice struct {
	Start, End, Step int
}

Slice represents a slice expression in the form of `start[:end[:step]]` from the JSONPath specification.

func ParseSlice

func ParseSlice(s string) (*Slice, error)

ParseSlice parses a slice expression from a string in the form of `start[:end[:step]]`.

type SliceExpr

type SliceExpr struct {
	Slice *Slice
}

SliceExpr is a representation of the `[start:end:step]` slice expression in YAMLPath, which will select a range of elements from a sequence node. Any non-sequence node objects are filtered and ignored.

func (*SliceExpr) Eval

func (s *SliceExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the slice expression against the provided nodes and returns the resulting nodes that match the slice expression.

type SubsetOfExpr

type SubsetOfExpr struct {
	Left, Right Expr
}

SubsetOfExpr checks if the left-hand side is a subset of the right-hand side of the expression.

func (*SubsetOfExpr) Eval

func (e *SubsetOfExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the subset-of expression by checking if the left-hand side is a subset of the right-hand side.

type ValueExpr

type ValueExpr struct {
	Nodes []*yaml.Node
}

ValueExpr is an expression that always returns the specified value. This is primarily used to return literals that are specified in the path.

func (*ValueExpr) Eval

func (e *ValueExpr) Eval(invocation.Context) ([]*yaml.Node, error)

Eval evaluates the expression and returns the specified value.

type WildcardExpr

type WildcardExpr struct{}

WildcardExpr is a representation of the `.*` and `[*]` expressions in YAMLPath, which will select all fields or sequence elements from the current node.

func (*WildcardExpr) Eval

func (*WildcardExpr) Eval(ctx invocation.Context) ([]*yaml.Node, error)

Eval evaluates the wildcard expression against the provided nodes.

Directories

Path Synopsis
Package exprtest provides test-doubles for expr.Expr implementations.
Package exprtest provides test-doubles for expr.Expr implementations.

Jump to

Keyboard shortcuts

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