expr

package
v0.0.0-...-83b8ea0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package expr is part of the go-fastreport library, a pure Go port of FastReport .NET.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuiltinFunctions

func BuiltinFunctions() map[string]any

BuiltinFunctions returns a map of built-in function implementations for use in expr-lang/expr evaluation environments.

Available functions:

IIF(condition, trueVal, falseVal) – conditional expression
Format(value, fmt)               – string formatting
DateDiff(date1, date2, unit)     – date difference (unit: "days","hours","minutes","seconds")
Str(value)                       – convert to string
Int(value)                       – convert to int
Float(value)                     – convert to float64
Len(s)                           – string length
Upper(s)                         – uppercase
Lower(s)                         – lowercase

Aggregate functions (Sum, Count, Avg, Min, Max) are registered externally by the engine because they require access to data source rows.

func ContainsExpression

func ContainsExpression(text string) bool

ContainsExpression returns true if text contains at least one [expression] with non-empty content between the brackets.

func ExtractExpressions

func ExtractExpressions(text string) []string

ExtractExpressions returns only the expression strings from text.

func UnescapeBrackets

func UnescapeBrackets(text string) string

UnescapeBrackets converts FastReport escape sequences in literal text. "[[" → "[" and "]]" → "]" per the .NET FastReport convention.

Types

type Env

type Env map[string]any

Env is the evaluation environment (variable bindings).

type EvalResult

type EvalResult struct {
	Value any
	Err   error
}

EvalResult holds the result of expression evaluation.

type Evaluator

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

Evaluator compiles and evaluates FastReport expressions.

func NewEvaluator

func NewEvaluator(env Env) *Evaluator

NewEvaluator creates a new Evaluator with the given environment.

func (*Evaluator) Eval

func (e *Evaluator) Eval(expression string) (any, error)

Eval evaluates an expression string and returns the result. For simple variable references (identifiers without operators), it first looks up env directly for performance. For complex expressions it uses expr-lang/expr with a merged environment that includes built-in functions.

func (*Evaluator) EvalText

func (e *Evaluator) EvalText(text string) (string, error)

EvalText evaluates all [expressions] in text, replacing them with their string representations and returning the full evaluated string.

func (*Evaluator) GetVar

func (e *Evaluator) GetVar(name string) (any, bool)

GetVar gets a variable from the environment.

func (*Evaluator) SetVar

func (e *Evaluator) SetVar(name string, value any)

SetVar sets a variable in the evaluation environment.

type Token

type Token struct {
	// IsExpr is true when this token contains an expression (bracketed).
	IsExpr bool
	// Value is the literal text or expression string (without brackets).
	Value string
}

Token represents a piece of parsed text.

func Parse

func Parse(text string) []Token

Parse splits text into literal and expression tokens. Default brackets are "[" and "]". Example: "Hello [Name]!" → [{false,"Hello "}, {true,"Name"}, {false,"!"}]

func ParseWithBrackets

func ParseWithBrackets(text, open, close string) []Token

ParseWithBrackets is like Parse but uses custom bracket characters. It handles nested brackets by tracking bracket depth. Escaped sequences "[[" and "]]" (double-open / double-close) are treated as literal single bracket characters in the output.

Jump to

Keyboard shortcuts

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