expr

package
v0.0.0-...-5b88717 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package expr contains all the expression types and related logic for FHIRPath expressions.

Index

Constants

View Source
const (
	Equals        = "="
	NotEquals     = "!="
	Equivalence   = "~"
	Inequivalence = "!~"
	Is            = "is"
	As            = "as"
	And           = "and"
	Or            = "or"
	Xor           = "xor"
	Implies       = "implies"
	Lt            = "<"
	Gt            = ">"
	Lte           = "<="
	Gte           = ">="
	Add           = "+"
	Sub           = "-"
	Concat        = "&"
	Mul           = "*"
	Div           = "/"
	FloorDiv      = "div"
	Mod           = "mod"
)

Operator constants.

Variables

View Source
var (
	ErrNotSingleton     = errors.New("collection is not a singleton")
	ErrInvalidType      = errors.New("collection evaluates to incorrect type")
	ErrInvalidOperator  = errors.New("received invalid operator")
	ErrToBeImplemented  = errors.New("expression not yet implemented")
	ErrInvalidField     = errors.New("invalid field")
	ErrConstantNotFound = errors.New("external constant not found")
)

Functions

func EvaluateAdd

func EvaluateAdd(lhs, rhs system.Any) (system.Any, error)

EvaluateAdd takes in two system types, and calls the appropriate Add method.

func EvaluateDiv

func EvaluateDiv(lhs, rhs system.Any) (system.Any, error)

EvaluateDiv takes in two system types, and calls the appropriate Div method.

func EvaluateFloorDiv

func EvaluateFloorDiv(lhs, rhs system.Any) (system.Any, error)

EvaluateFloorDiv takes in two system types, and calls the appropriate FloorDiv method.

func EvaluateMod

func EvaluateMod(lhs, rhs system.Any) (system.Any, error)

EvaluateMod takes in two system types, and calls the appropriate Mod method.

func EvaluateMul

func EvaluateMul(lhs, rhs system.Any) (system.Any, error)

EvaluateMul takes in two system types, and calls the appropriate Mul method.

func EvaluateSub

func EvaluateSub(lhs, rhs system.Any) (system.Any, error)

EvaluateSub takes in two system types, and calls the appropriate Sub method.

Types

type ArithmeticExpression

type ArithmeticExpression struct {
	Left  Expression
	Right Expression
	Op    func(system.Any, system.Any) (system.Any, error)
}

ArithmeticExpression enables mathematical arithmetic operations. Includes '+', '-', '*', "/", "div", and 'mod'.

func (*ArithmeticExpression) Evaluate

func (e *ArithmeticExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the two subexpressions, with respect to singleton evaluation of collections, and performs the respective additive operation.

type AsExpression

type AsExpression struct {
	Expr Expression
	Type reflection.TypeSpecifier
}

AsExpression enables evaluation of an "as" type expression.

func (*AsExpression) Evaluate

func (e *AsExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the contained expression with respect to singleton evaluation of collections, returns the singleton if it is of the given type. Returns empty otherwise.

type BooleanExpression

type BooleanExpression struct {
	Left  Expression
	Right Expression
	Op    Operator
}

BooleanExpression enables evaluation of boolean expressions, including "and", "or", "xor", and "implies".

func (*BooleanExpression) Evaluate

func (e *BooleanExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the subexpressions with respect to singleton evaluation of collections, and performs the respective Boolean operation.

type ComparisonExpression

type ComparisonExpression struct {
	Left  Expression
	Right Expression
	Op    Operator
}

func (*ComparisonExpression) Evaluate

func (e *ComparisonExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the subexpressions with respect to singleton evaluation of collections, and performs the respective comparison operation.

type ConcatExpression

type ConcatExpression struct {
	Left  Expression
	Right Expression
}

ConcatExpression enables the evaluation of a string concatenation expression.

func (*ConcatExpression) Evaluate

func (e *ConcatExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the two subexpressions with respect to singleton evaluation of collections, and attempts to concatenate the two strings. Returns an error if the expressions don't resolve to strings. This differs from string addition when either collection is empty. Rather than returning empty, it will treat the empty collection as an empty string.

type Context

type Context struct {
	Now               time.Time
	ExternalConstants map[string]any

	// LastResult is required for implementing most FHIRPatch operations, since
	// a reference to the node before the one being (inserted, replaced, moved) is
	// necessary in order to alter the containing object.
	LastResult system.Collection

	// BeforeLastResult is necessary for implementing FHIRPatch delete due to an
	// edge-case, where deleting a specific element from a list requires a pointer
	// to the container that holds the list. In a path like `Patient.name.given[0]`,
	// the 'LastResult' will be the unwrapped list from 'given', but we need the
	// 'name' element that contains the 'given' list in order to alter the list.
	BeforeLastResult system.Collection
}

Context holds the global time and external constant variable map, to enable deterministic evaluation.

func InitializeContext

func InitializeContext(input system.Collection) *Context

InitializeContext returns a base context, initialized with current time and initial constant variables set.

func (*Context) Clone

func (c *Context) Clone() *Context

Clone copies this Context object to produce a new instance.

type EqualityExpression

type EqualityExpression struct {
	Left  Expression
	Right Expression
	Not   bool
}

EqualityExpression allows checking equality of the two contained subexpressions. The two expressions should return comparable values when evaluated.

func (*EqualityExpression) Evaluate

func (e *EqualityExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the two subexpressions, and returns true if their contents are equal, using the functionality of system.Collection.Equal. If either collection is empty, returns an empty collection.

type Expression

type Expression interface {
	Evaluate(*Context, system.Collection) (system.Collection, error)
}

Expression is the abstraction for all FHIRPath expressions, ie. taking in some input collection and outputting some collection.

type ExpressionSequence

type ExpressionSequence struct {
	Expressions []Expression
}

ExpressionSequence abstracts the flow of evaluation for compound expressions. It consists of a sequence of expressions whose outputs flow into the inputs of the next expression.

func (*ExpressionSequence) Evaluate

func (s *ExpressionSequence) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate iterates through the ExpressionSequence, feeding the output of an evaluation to the next Expression.

type ExternalConstantExpression

type ExternalConstantExpression struct {
	Identifier string
}

ExternalConstantExpression enables evaluation of external constants.

func (*ExternalConstantExpression) Evaluate

Evaluate retrieves the constant from the map located in the Context. Returns an error if the constant is not present.

type FieldExpression

type FieldExpression struct {
	FieldName  string
	Permissive bool
}

FieldExpression is the expression that accesses the specified FieldName in the input collection.

func (*FieldExpression) Evaluate

func (e *FieldExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate filters the input collections by those that contain the FieldName string, and returns the result.

type FunctionExpression

type FunctionExpression struct {
	Fn   func(*Context, system.Collection, ...Expression) (system.Collection, error)
	Args []Expression
}

FunctionExpression enables evaluation of Function Invocation expressions. It holds the function and function arguments.

func (*FunctionExpression) Evaluate

func (e *FunctionExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the function with respect to its arguments. Returns the result of the function, or an error if raised.

type IdentityExpression

type IdentityExpression struct{}

IdentityExpression encapsulates the top-level expression, ie. the first step in the chain of evaluation. A no-op expression that returns itself.

func (*IdentityExpression) Evaluate

Evaluate returns the input collection, without raising an error.

type IndexExpression

type IndexExpression struct {
	Index Expression
}

IndexExpression allows accessing of an input system.Collection's index. Contains an expression, that when evaluated, should return an integer that represents the index.

func (*IndexExpression) Evaluate

func (e *IndexExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate indexes the input system.Collection and returns the item located at the given index. If the index is negative or out of bounds, returns an empty collection. Raises an error if the contained expression does not evaluate to an expression, or raises an error itself.

type IsExpression

type IsExpression struct {
	Expr Expression
	Type reflection.TypeSpecifier
}

IsExpression enables evaluation of an "is" type expression.

func (*IsExpression) Evaluate

func (e *IsExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate evaluates the contained expression with respect to singleton evaluation of collections, and determines whether or not it is the given type.

type LiteralExpression

type LiteralExpression struct {
	Literal system.Any
}

LiteralExpression abstracts FHIRPath system types, that are returned from parsing literals.

func (*LiteralExpression) Evaluate

Evaluate returns the contained literal, without raising an error. Returns an empty collection if the literal is nil, representing a Null literal.

type NegationExpression

type NegationExpression struct {
	Expr Expression
}

NegationExpression enables negation of number values (Integer, Decimal, Quantity).

func (*NegationExpression) Evaluate

func (e *NegationExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate negates the contained expression, that is evaluated with respect to singleton evaluation. If the contained value is not a number, returns an error.

type Operator

type Operator string

Operator represents a valid expression operator.

type TypeExpression

type TypeExpression struct {
	Type string
}

TypeExpression contains the FHIR Type identifier string, to be able to filter the items in the input collection that have the given type.

func (*TypeExpression) Evaluate

func (e *TypeExpression) Evaluate(ctx *Context, input system.Collection) (system.Collection, error)

Evaluate filters the messages in the input that are identified by the Type defined in the expression.

Directories

Path Synopsis
Package exprtest provides some useful test dummies to make it easier to mock expressions to return a desired result.
Package exprtest provides some useful test dummies to make it easier to mock expressions to return a desired result.

Jump to

Keyboard shortcuts

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