Path: redb-route/redb.Route/Predicates/PredicateFactory.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/Predicates/PredicateFactory.cs ---- using redb.Route.Abstractions; using redb.Route.Expressions; namespace redb.Route.Predicates; /// /// The single place that turns a DSL condition string into an . /// Every string-taking condition overload (Filter, When, LoopWhile, Validate) routes through it, /// so a condition behaves identically no matter where it was written. /// /// A condition is a boolean expression, which is a different position in the language from a /// value: SetBody("black and white") must stay a literal, while /// Filter("header.a>10") must be a comparison. The two positions therefore compile /// differently, and this type is the boundary between them. /// /// public static class PredicateFactory { /// /// Builds a predicate from a condition string. Three shapes, one rule each: /// /// a ${...} template is interpolated to a string and read for truthiness; /// a condition carrying a comparison or word-logic operator is a boolean expression /// and is parsed by the AST parser, which tokenises the input: whitespace between tokens /// carries no meaning (header.a>10, header.a > 10 and a tab- or /// newline-separated form are one expression) and an operator inside a quoted literal is /// not an operator; /// anything else is a value read for truthiness, which keeps a bare accessor such as /// property.cfg.enabled resolving exactly as it always has. /// /// Compilation happens here, while the route is being built, so a condition the parser cannot /// accept fails at build time instead of turning into a constant answer on live traffic. /// /// The condition string as written by the route author. /// A predicate that evaluates the condition against an exchange. /// Thrown when is null, empty or whitespace. /// Thrown when the condition cannot be compiled. public static IPredicate FromString(string condition) { ArgumentException.ThrowIfNullOrWhiteSpace(condition); if (ExpressionResolver.IsTemplate(condition)) { var template = ExpressionResolver.GetCompiledTemplate(condition); return new LambdaPredicate(exchange => RouteTruthiness.ToBoolean(template(exchange))); } var compiled = ExpressionResolver.ContainsComparisonOrWordLogicOperator(condition) ? ExpressionResolver.GetCompiledValueExpressionWithAst(condition) : ExpressionResolver.GetCompiledValueExpression(condition); return new LambdaPredicate(exchange => RouteTruthiness.ToBoolean(compiled(exchange))); } /// /// Builds a predicate from an expression instance used in a condition position. /// /// An expression born from a string (Expr("header.amount>1000")) is a condition /// string that happened to be wrapped, so it takes the same path as /// : compiled as a condition, whitespace-insensitive, fail-fast. /// Reading it as a value first would resurrect the original silent-drop defect: the value /// dialect turned the unspaced comparison into a lookup of a header named /// amount>1000, which never exists. /// /// /// An expression that implements answers the condition itself. /// A query language brings its own notion of a match that evaluating-then-reading-truthiness /// cannot reconstruct: XPath("/order/discount") in a condition asks whether the path /// matched, and flattening the node-set to a value first would answer with the node's content /// instead — <discount>0</discount> would read as "no match". Asking the /// expression keeps that decision where the language's semantics live. /// /// /// Any other expression is evaluated and read through . /// /// /// The expression written in a condition position. /// A predicate that evaluates the expression against an exchange. public static IPredicate FromExpression(IExpression expression) { ArgumentNullException.ThrowIfNull(expression); if (expression is StringExpression text) return FromString(text.Template); // An expression that is a predicate too (Method()) answers the condition itself, asynchronously included. if (expression is IPredicate predicate) return predicate; if (expression is IPredicateExpression self) return new LambdaPredicate(self.Matches); return new LambdaPredicate(exchange => RouteTruthiness.ToBoolean(expression.Evaluate(exchange))); } }