Path: redb-route/redb.Route/Expressions/StringExpression.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/StringExpression.cs
----
using System.Text.RegularExpressions;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Expressions;
///
/// Expression that evaluates a string expression template via .
/// Supports ${...} template interpolation and raw expression strings.
///
/// Unifies the two expression worlds: typed objects and string-based
/// ${...} templates. Inherits all predicate methods from
/// (isEqualTo, contains, isGreaterThan, etc.), so string expressions can be used anywhere
/// an is expected.
///
///
///
/// // In RouteBuilder:
/// .SetBody(Expr("${header.source}"))
/// .Filter(Expr("${header.amount}").isGreaterThan(1000))
/// .SetHeader("full", Expr("${header.first} ${header.last}"))
///
///
///
public sealed class StringExpression : Expression
{
private readonly string _template;
private readonly bool _isTemplate;
private readonly Func? _compiledValue;
private readonly Func? _compiledTemplate;
///
/// Initializes a new instance with the specified expression template.
///
///
/// Expression string. Can be a ${...} template (e.g. "${header.x} world")
/// or a raw expression (e.g. "header.amount + 100").
///
public StringExpression(string template)
{
ArgumentException.ThrowIfNullOrEmpty(template, nameof(template));
_template = template;
// Determine mode: template interpolation (contains ${...}) vs raw value expression.
// A whole-string placeholder is a third case: it keeps the CLR type of its value, so
// "${header.a}" with an int header yields the int, not "10". Mixed templates render text.
_isTemplate = TemplateScanner.HasPlaceholder(template);
if (_isTemplate && ExpressionResolver.TryGetSinglePlaceholder(template, out var placeholder))
{
_isTemplate = false;
_compiledValue = ExpressionResolver.GetCompiledPlaceholder(placeholder);
}
else if (_isTemplate)
{
_compiledTemplate = ExpressionResolver.GetCompiledTemplate(template);
}
else
{
_compiledValue = ExpressionResolver.GetCompiledValueExpression(template);
}
}
/// Gets the original expression template string.
public string Template => _template;
///
public override T Evaluate(IExchange exchange)
{
ArgumentNullException.ThrowIfNull(exchange, nameof(exchange));
object? result;
if (_isTemplate)
{
// "${header.x} world" → string interpolation
result = _compiledTemplate!(exchange);
}
else
{
// "header.amount + 100" → value expression
result = _compiledValue!(exchange);
}
if (result is null)
return default!;
if (result is T typed)
return typed;
// A whole-string placeholder keeps the CLR type; when the caller wants text it is rendered the
// same way a hole in a mixed template is — culture-invariant, dates as ISO 8601.
if (typeof(T) == typeof(string))
return (T)(object)ExpressionResolver.TemplateText(result);
return (T)RouteDecimal.ChangeType(result, typeof(T), System.Globalization.CultureInfo.InvariantCulture);
}
///
/// String expressions are read-only — they compute a value but have no target to write to.
///
/// Always thrown.
public override void SetValue(IExchange exchange, object value)
=> throw new NotSupportedException(
$"StringExpression '{_template}' is read-only. " +
"Use Body(), Header(name), or Property(name) for settable expressions.");
///
public override string ToString() => $"Expr(\"{_template}\")";
///
public override string ToTemplateString() => _template;
}