Path: redb-route/redb.Route/Expressions/ExpressionResolver.Template.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/ExpressionResolver.Template.cs
----
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using redb.Route.Abstractions;
namespace redb.Route.Expressions;
///
/// The template engine: the one pipeline that gives a ${...} placeholder its meaning.
/// Until 2026-08-28 two independent engines interpreted placeholders — one under
/// , another under ResolveTypedOrTemplate — with
/// complementary holes: the first understood unspaced comparisons and dashed header names but not
/// arithmetic, the second the reverse. Both now route through ,
/// so a placeholder means the same thing wherever it is written.
///
public static partial class ExpressionResolver
{
///
/// Compiles the inner text of a single ${...} placeholder into a typed evaluator.
///
/// The rules, in order: jpath(...) and xpath(...) keep their own syntax (the
/// expression tokenizer does not read $ or / paths); body and
/// contentType are direct accessors; a header. or property. prefix
/// resolves by the literal-name-first rule below; anything else is a full expression compiled
/// by the AST engine — arithmetic, comparisons, functions, ternary and index access included.
///
///
/// Literal-name-first: header.Content-Type is the header of that exact name when it
/// exists; header.user.Age is the member path behind the user header;
/// header.a+1, matching neither, is the expression. The name check happens per
/// exchange, which is what makes a dash usable in a name at all — no grammar can tell a
/// dashed name from a subtraction, but the message can.
///
///
/// The text between ${ and }.
/// An evaluator returning the CLR-typed value of the placeholder.
/// Thrown when the text is not a resolvable placeholder.
internal static Func CompilePlaceholder(string placeholder)
{
ArgumentException.ThrowIfNullOrWhiteSpace(placeholder);
var text = placeholder.Trim();
// ${expr:F4}: a format suffix, format(expr, 'F4') with the invariant culture (findings register row 34 point 4).
if (TrySplitFormatSuffix(text, out var formatted, out var suffix))
{
var value = CompilePlaceholder(formatted);
return exchange => Ast_Format(value(exchange), suffix, null);
}
// ${jsonpath:$.a.b} and ${xpath://a}: Apache Camel's spellings of jpath('...') and xpath('...') (row 34 point 2).
// The path is the rest of the placeholder as written; only a whole placeholder, where its end is known.
foreach (var (prefix, apply) in PathSpellings)
if (text.StartsWith(prefix, StringComparison.Ordinal))
{
var path = text[prefix.Length..].Trim();
if (path.Length == 0)
throw new ExpressionCompilationException($"${{{text}}} names no path: write ${{{prefix}$.a.b}} or ${{{prefix}//a}}.");
return exchange => apply(exchange, path);
}
// ${file:name}: the file language over the file headers (row 34 point 3).
if (text.StartsWith("file:", StringComparison.Ordinal))
return FileKey(text);
if (text.StartsWith("file:", StringComparison.OrdinalIgnoreCase))
throw new ExpressionCompilationException(
$"${{{text}}}: file keys are lowercase: ${{file:{text["file:".Length..].Trim().ToLowerInvariant()}}}.");
// A namespaced call (bean:svc.Check(...)) is the AST's, deterministically: no shortcut below knows it.
if (ContainsNamespacedCall(text))
return GetCompiledValueExpressionWithAst(text);
var jpath = JPathFunctionRegex.Match(text);
if (jpath.Success && jpath.Value.Trim() == text && !HasSecondArgument(jpath.Groups[1].Value))
{
var path = Unquote(jpath.Groups[1].Value.Trim());
return exchange => ApplyJPath(exchange, path);
}
var xpath = XPathFunctionRegex.Match(text);
if (xpath.Success && xpath.Value.Trim() == text && !HasSecondArgument(xpath.Groups[1].Value))
{
var path = Unquote(xpath.Groups[1].Value.Trim());
return exchange => ApplyXPath(exchange, path);
}
if (text == "body")
return exchange => exchange.In.getBody