Path: redb-route/redb.Route/Core/DynamicValue.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/DynamicValue.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Core;
///
/// A value that is either static (resolved at endpoint creation)
/// or dynamic (resolved per-message from exchange via ${...} expressions).
/// The property type itself is the contract: DynamicValue{T} = "may be an expression".
///
/// The resolved value type.
public readonly struct DynamicValue
{
private readonly T? _static;
private readonly Func? _resolver;
private DynamicValue(T? staticValue, Func? resolver)
{
_static = staticValue;
_resolver = resolver;
}
/// Whether this value contains a dynamic expression.
public bool IsDynamic => _resolver != null;
///
/// Resolves the value. Static = instant return, dynamic = evaluates compiled expression.
///
/// The current exchange (used only for dynamic values).
/// Resolved value.
public T? Resolve(IExchange exchange) =>
_resolver != null ? _resolver(exchange) : _static;
/// Creates a static (constant) value.
/// The constant value.
/// A DynamicValue wrapping the constant.
public static DynamicValue FromStatic(T value) => new(value, null);
/// Creates a dynamic value from a compiled expression resolver.
/// Compiled expression function, cached after first parse.
/// A DynamicValue that evaluates per-message.
public static DynamicValue FromExpression(Func resolver) =>
new(default, resolver ?? throw new ArgumentNullException(nameof(resolver)));
/// Creates a dynamic value from an .
/// The expression to evaluate per-message.
/// A DynamicValue that evaluates the expression on each resolve.
public static DynamicValue FromExpression(IExpression expression)
{
ArgumentNullException.ThrowIfNull(expression);
return new(default, exchange => expression.Evaluate(exchange));
}
/// Implicit conversion from a plain value to a static DynamicValue.
/// The value to wrap.
public static implicit operator DynamicValue(T value) => FromStatic(value);
///
public override string ToString() =>
IsDynamic ? "${expression}" : _static?.ToString() ?? "null";
}