Path: redb-route/redb.Route/Core/PropertyPlaceholderResolver.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/Core/PropertyPlaceholderResolver.cs
----
using System.Text.RegularExpressions;
namespace redb.Route.Core;
///
/// Resolves Apache Camel-style property placeholders inside a string — typically an endpoint URI —
/// at route-compile time. Supports {{key}} (required) and {{key:default}} (with a fallback
/// value). This is the redb.Route analogue of Camel's PropertiesComponent: it lets endpoint URIs
/// (and any other DSL string routed through it) externalise their configuration instead of hard-coding
/// hosts, queue names, ports or paths.
///
/// The value lookup is supplied by the caller as a , keeping this type pure
/// and testable. wires the lookup to IConfiguration (which already layers
/// environment variables, appsettings and user-secrets with the container's own precedence — so no
/// Camel-style env:/sys: prefix functions are needed) and, as a container-free fallback,
/// the context's own properties.
///
///
/// Deliberately minimal: no nested placeholders ({{a{{b}}}}), no optional-parameter removal
/// ({{?key}}), no encryption or multi-location precedence. Resolution is single-pass, so it can
/// never loop. A placeholder with neither a value nor a default fails fast at compile time.
///
///
public static class PropertyPlaceholderResolver
{
// {{ key }} or {{ key : default }}. The key excludes braces and the ':' separator; the optional
// default (everything up to the closing braces) may contain ':' so URLs work as defaults. Lazy so
// the first "}}" terminates the match — a nested inner placeholder simply isn't matched as one.
private static readonly Regex Pattern = new(
@"\{\{\s*([^{}:]+?)\s*(?::\s*([^{}]*?)\s*)?\}\}",
RegexOptions.Compiled);
///
/// Fast pre-check: true only if could contain a placeholder. Lets callers
/// skip resolution (and the regex) for the overwhelming majority of URIs that have none — which is also
/// why the feature never touches existing routes: {{ is not legal in a bare URI.
///
public static bool HasPlaceholder(string? text)
=> text is not null && text.Contains("{{", StringComparison.Ordinal);
///
/// Replaces every {{key}} / {{key:default}} in with the value
/// returned by for that key (or the default when the lookup yields none).
///
/// The string to resolve (e.g. an endpoint URI).
/// Returns the configured value for a key, or null/empty if unknown.
/// The resolved string, or unchanged when it has no placeholders.
///
/// A placeholder had no resolvable value and no default — surfaced at compile time so a misconfigured
/// deployment fails loudly rather than building a broken endpoint URI.
///
public static string Resolve(string text, Func lookup)
{
ArgumentNullException.ThrowIfNull(lookup);
if (!HasPlaceholder(text))
return text;
return Pattern.Replace(text, match =>
{
var key = match.Groups[1].Value;
var value = lookup(key);
if (!string.IsNullOrEmpty(value))
return value;
if (match.Groups[2].Success)
return match.Groups[2].Value;
throw new InvalidOperationException(
$"Unresolved property placeholder '{{{{{key}}}}}': no value was found for key '{key}' " +
$"and no default was given (use '{{{{{key}:default}}}}'). Provide the value via IConfiguration " +
$"(appsettings/environment) or context.SetProperty(\"{key}\", ...).");
});
}
}