Ausdruckssprache
Ausdruckssprache
Abschnitt betitelt „Ausdruckssprache“Werten Sie dynamische Ausdrücke mit expr-lang-Syntax aus. Kompilieren und führen Sie sichere Ausdrücke für Filterung, Validierung und Regelauswertung ohne vollständige Lua-Ausführung aus.
Konfiguration
Abschnitt betitelt „Konfiguration“Ausdruck-Cache wird beim Start konfiguriert:
lua: expr: cache_enabled: true # Ausdruck-Caching aktivieren capacity: 5000 # Cache-Kapazitätlocal expr = require("expr")Ausdrücke auswerten
Abschnitt betitelt „Ausdrücke auswerten“Werten Sie einen Ausdruck-String aus und geben Sie das Ergebnis zurück. Verwendet internen LRU-Cache für kompilierte Ausdrücke:
-- Einfache Mathematiklocal result = expr.eval("1 + 2 * 3") -- 7
-- Mit Variablenlocal total = expr.eval("price * quantity", { price = 29.99, quantity = 3}) -- 89.97
-- Boolesche Ausdrückelocal is_adult = expr.eval("age >= 18", {age = 21}) -- true
-- String-Operationenlocal greeting = expr.eval('name + " is " + status', { name = "Alice", status = "online"}) -- "Alice is online"
-- Ternärer Operatorlocal label = expr.eval('score > 90 ? "A" : score > 80 ? "B" : "C"', { score = 85}) -- "B"
-- Array-Operationenlocal has_admin = expr.eval('"admin" in roles', { roles = {"user", "admin", "viewer"}}) -- true| Parameter | Typ | Beschreibung |
|---|---|---|
expression | string | Ausdruck in expr-lang-Syntax |
env | table | Variablenumgebung für Ausdruck (optional) |
Gibt zurück: any, error
Ausdrücke kompilieren
Abschnitt betitelt „Ausdrücke kompilieren“Kompilieren Sie einen Ausdruck in ein wiederverwendbares Program-Objekt für wiederholte Auswertung:
-- Einmal kompilieren für wiederholte Verwendunglocal discount_calc, err = expr.compile("price * (1 - discount_rate)")if err then return nil, errend
-- Mit verschiedenen Eingaben wiederverwendenlocal price1 = discount_calc:run({price = 100, discount_rate = 0.1}) -- 90local price2 = discount_calc:run({price = 50, discount_rate = 0.2}) -- 40local price3 = discount_calc:run({price = 200, discount_rate = 0.15}) -- 170| Parameter | Typ | Beschreibung |
|---|---|---|
expression | string | Ausdruck in expr-lang-Syntax |
env | table | Typhinweis-Umgebung für Kompilierung (optional) |
Gibt zurück: Program, error
Kompilierte Programme ausführen
Abschnitt betitelt „Kompilierte Programme ausführen“Führen Sie einen kompilierten Ausdruck mit bereitgestellter Umgebung aus:
-- Validierungsregellocal validator, _ = expr.compile("len(password) >= 8 and len(password) <= 128")
local valid1 = validator:run({password = "short"}) -- falselocal valid2 = validator:run({password = "securepass123"}) -- true
-- Preisregellocal pricer, _ = expr.compile([[ base_price * quantity * (1 - bulk_discount) + shipping]])
local order_total = pricer:run({ base_price = 25.00, quantity = 10, bulk_discount = 0.15, shipping = 12.50}) -- 225.00| Parameter | Typ | Beschreibung |
|---|---|---|
env | table | Variablenumgebung für Ausdruck (optional) |
Gibt zurück: any, error
Eingebaute Funktionen
Abschnitt betitelt „Eingebaute Funktionen“Expr-lang bietet viele eingebaute Funktionen:
-- Mathematische Funktionenexpr.eval("max(1, 5, 3)") -- 5expr.eval("min(10, 2, 8)") -- 2expr.eval("abs(-42)") -- 42expr.eval("ceil(3.2)") -- 4expr.eval("floor(3.8)") -- 3
-- String-Funktionenexpr.eval('len("hello")') -- 5expr.eval('upper("hello")') -- "HELLO"expr.eval('lower("HELLO")') -- "hello"expr.eval('trim(" hi ")') -- "hi"expr.eval('contains("hello", "ell")') -- true
-- Array-Funktionenexpr.eval("len(items)", {items = {1,2,3}}) -- 3expr.eval("sum(values)", {values = {1,2,3,4}}) -- 10| Bedingung | Art | Wiederholbar |
|---|---|---|
| Ausdruck ist leer | errors.INVALID | nein |
| Ausdruck-Syntax ungültig | errors.INTERNAL | nein |
| Ausdrucksauswertung schlägt fehl | errors.INTERNAL | nein |
| Ergebniskonvertierung schlägt fehl | errors.INTERNAL | nein |
Siehe Fehlerbehandlung für die Arbeit mit Fehlern.