Path: redb-route/redb.Route/Expressions/PlaceholderExpression.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/PlaceholderExpression.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Expressions;
///
/// A text with {{key}} / {{key:default}} placeholders, resolved through the route context's configuration
/// (IConfiguration, then the context's properties) the way an endpoint URI is. The value a route sets from
/// configuration: <setHeader name="x" value="{{ldap.user}}"/> in markup, new PlaceholderExpression("{{ldap.user}}")
/// in C#. Resolved once per route context and then reused; a key with no value and no default fails the exchange with
/// the same message an endpoint URI gives.
///
public sealed class PlaceholderExpression : Expression
{
private (IRouteContext? Context, string Value)? _resolved;
/// Creates the expression over .
public PlaceholderExpression(string text)
{
ArgumentNullException.ThrowIfNull(text);
Text = text;
}
/// The text as written, placeholders included.
public string Text { get; }
///
public override T Evaluate(IExchange exchange)
{
var value = Resolve(exchange.Context);
if (value is T typed)
return typed;
return (T)RouteDecimal.ChangeType(value, typeof(T), System.Globalization.CultureInfo.InvariantCulture);
}
/// The text with its placeholders resolved through ; without one, defaults only.
public string Resolve(IRouteContext? context)
{
if (_resolved is { } cached && ReferenceEquals(cached.Context, context))
return cached.Value;
var value = context is RouteContext live
? live.ResolvePlaceholders(Text)
: PropertyPlaceholderResolver.Resolve(Text, _ => null);
_resolved = (context, value);
return value;
}
///
/// Always: a value from configuration is read-only.
public override void SetValue(IExchange exchange, object value)
=> throw new NotSupportedException("A placeholder expression cannot be written to.");
///
public override string ToTemplateString() => Text;
}