expression

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package expression provides a declarative expression language for data transformation and querying within Δ-controller pipelines.

The expression system supports JSONPath-based field access, boolean logic, arithmetic operations, string manipulation, and Kubernetes-specific operations like label selectors. Expressions are serialized as JSON and can be embedded in pipeline specifications.

Expression operations include:

  • Field access: "$.metadata.name", "$.spec.containers[0].image".
  • Boolean logic: @and, @or, @not, @eq, @ne, @lt, @gt.
  • String operations: @concat, @split, @replace, @regex.
  • Arithmetic: @add, @sub, @mul, @div, @mod.
  • Collections: @len, @contains, @in, @keys, @values.
  • Kubernetes: @selector for label matching.

Example usage:

expr := Expression{
    Op: "@eq",
    Arg: []Expression{
        {Op: "@string", Literal: "$.metadata.labels.app"},
        {Op: "@string", Literal: "nginx"},
    },
}
result, err := expr.Evaluate(EvalCtx{Object: obj})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsBinaryBoolList

func AsBinaryBoolList(d any) ([]bool, error)

func AsBinaryFloatList

func AsBinaryFloatList(d any) ([]float64, error)

func AsBinaryIntList

func AsBinaryIntList(d any) ([]int64, error)

func AsBinaryIntOrFloatList

func AsBinaryIntOrFloatList(d any) ([]int64, []float64, reflect.Kind, error)

func AsBinaryStringList

func AsBinaryStringList(d any) ([]string, error)

func AsBool

func AsBool(d any) (bool, error)

func AsBoolList

func AsBoolList(d any) ([]bool, error)

func AsFloat

func AsFloat(d any) (float64, error)

func AsFloatList

func AsFloatList(d any) ([]float64, error)

func AsInt

func AsInt(d any) (int64, error)

func AsIntList

func AsIntList(d any) ([]int64, error)

func AsIntOrFloat

func AsIntOrFloat(d any) (int64, float64, reflect.Kind, error)

func AsIntOrFloatList

func AsIntOrFloatList(d any) ([]int64, []float64, reflect.Kind, error)

func AsList

func AsList(d any) ([]any, error)

func AsMap

func AsMap(d any) (map[string]any, error)

func AsObject

func AsObject(d any) (map[string]any, error)

func AsObjectList

func AsObjectList(d any) ([]map[string]any, error)

func AsObjectOrObjectList

func AsObjectOrObjectList(d any) ([]map[string]any, error)

AsObjectOrObjectList returns an object or an expression list.

func AsString

func AsString(d any) (string, error)

func AsStringList

func AsStringList(d any) ([]string, error)

func GetJSONPath

func GetJSONPath(ctx EvalCtx, key string) (any, error)

GetJSONPath evaluates a string op that may or may not be a JSONPath expession.

func GetJSONPathRaw

func GetJSONPathRaw(query string, object any) (any, error)

GetJSONPathRaw evaluates a JSONPath expression on the specified object and returns the result or an error.

func IsList

func IsList(d any) bool

func MatchLabels

func MatchLabels(labelMap map[string]any, selectorMap map[string]any) (bool, error)

MatchLabels implements a Kubernetes style label matcher.

func SetJSONPath

func SetJSONPath(ctx EvalCtx, key string, value, data any) error

SetJSONPath overwrites the data in-place with the given value at the given key. Leaves the rest of the data unchanged.

func SetJSONPathRaw

func SetJSONPathRaw(key string, value, target any) error

SetJSONPathRaw sets a key (possibly represented with a JSONPath expression) to a value (can also be a JSONPath expression, which will be evaluated using the object argument) in the given data structure.

func SetJSONPathRawExp

func SetJSONPathRawExp(keyExp *Expression, value, data any) error

SetJSONPathRawExp is the same as SetJSONPathRaw but takes the key as a string expression.

Types

type ErrExpression

type ErrExpression = error

ErrExpression is a custom error.

func NewExpressionError

func NewExpressionError(e *Expression, err error) ErrExpression

NewExpressionError creates a new custom error.

type ErrInvalidArguments

type ErrInvalidArguments = error

ErrInvalidArguments is a custom error.

func NewInvalidArgumentsError

func NewInvalidArgumentsError(content string) ErrInvalidArguments

NewInvalidArgumentsError creates a new custom error.

type ErrUnmarshal

type ErrUnmarshal = error

ErrUnmarshal is a custom error.

func NewUnmarshalError

func NewUnmarshalError(kind, content string) ErrUnmarshal

NewUnmarshalError creates a new custom error.

type EvalCtx

type EvalCtx struct {
	Object, Subject any
	Log             logr.Logger
}

EvalCtx defines the context for a running evaluation.

type Expression

type Expression struct {
	Op      string
	Arg     *Expression
	Literal any
}

Expression defines a single expression op.

func AsExpOrExpList

func AsExpOrExpList(d any) ([]Expression, error)

AsExpOrExpList returns an expression or an expression list.

func NewJSONPathGetExpression

func NewJSONPathGetExpression(key string) Expression

NewJSONPathGetExpression creates an expression that, when evaluated on an object, will return the value at the given key.

func NewJSONPathSetExpression

func NewJSONPathSetExpression(key string, value any) (Expression, error)

NewJSONPathSetExpression creates an expression that, when evaluated on an object, will set the value at the given key to the value.

func NewLiteralExpression

func NewLiteralExpression(value any) (Expression, error)

NewLiteralExpression creates a new literal expression with the given argument.

func (*Expression) DeepCopyInto

func (e *Expression) DeepCopyInto(out *Expression)

DeepCopyInto deep-copies an expression.

func (*Expression) Evaluate

func (e *Expression) Evaluate(ctx EvalCtx) (any, error)

Evaluate processes an expression.

func (*Expression) GetLiteralBool

func (e *Expression) GetLiteralBool() (bool, error)

GetLiteralBool returns a liteal bool from an expression.

func (*Expression) GetLiteralFloat

func (e *Expression) GetLiteralFloat() (float64, error)

GetLiteralFloat returns a liteal floating point number from an expression.

func (*Expression) GetLiteralInt

func (e *Expression) GetLiteralInt() (int64, error)

GetLiteralInt returns a liteal integer from an expression.

func (*Expression) GetLiteralList

func (e *Expression) GetLiteralList() ([]any, error)

GetLiteralList returns a liteal list from an expression.

func (*Expression) GetLiteralMap

func (e *Expression) GetLiteralMap() (map[string]any, error)

GetLiteralMap returns a liteal set of key-value pairs from an expression.

func (*Expression) GetLiteralString

func (e *Expression) GetLiteralString() (string, error)

GetLiteralString returns a liteal string from an expression.

func (*Expression) MarshalJSON

func (e *Expression) MarshalJSON() ([]byte, error)

UnmarshalJSON serializes an expression into JSON.

func (*Expression) String

func (e *Expression) String() string

String stringifies an expression.

func (*Expression) UnmarshalJSON

func (e *Expression) UnmarshalJSON(b []byte) error

UnmarshalJSON de-serializes an expression from JSON.

Jump to

Keyboard shortcuts

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