Path: redb-route/redb.Route/Predicates/RouteTruthiness.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/RouteTruthiness.cs
----
namespace redb.Route.Predicates;
///
/// The single truthiness rule of the DSL. Every place that turns a value into a boolean — the
/// condition positions (Filter, When, LoopWhile, Validate), the word-logic operators
/// (AND/OR/XOR/NOT) and the logical() function — goes through
/// this one method, so a value cannot be true in one position and false in another.
///
/// The rule: a bool is itself; a number is "non-zero means true"; a string is parsed as a boolean
/// word first (true/false, 1/0, yes/no, on/off, y/n) and
/// otherwise read as "non-empty means true"; null is false; any other object exists and is
/// therefore true.
///
///
/// History note: until 2026-08-28 three different rules coexisted — this one (without the numeric
/// and word cases), the AST operand conversion, and the hand-written logical branch — and they
/// disagreed on 0, "0" and "no": Filter("${header.zero}") passed a
/// message while Filter("logical(header.zero)") dropped it. The merge is recorded in the
/// changelog; the characterisation snapshot pins every form the merge moved.
///
///
internal static class RouteTruthiness
{
/// Converts an evaluated expression result to a boolean by the DSL truthiness rule.
/// The evaluated expression result.
/// The boolean reading of .
internal static bool ToBoolean(object? value) => value switch
{
null => false,
bool b => b,
string s => StringToBoolean(s),
sbyte n => n != 0,
byte n => n != 0,
short n => n != 0,
ushort n => n != 0,
int n => n != 0,
uint n => n != 0,
long n => n != 0,
ulong n => n != 0,
float n => n != 0f,
double n => n != 0d,
decimal n => n != 0m,
_ => true
};
///
/// Reads a string as a boolean: a recognised boolean word wins, anything else is
/// "non-empty means true".
///
private static bool StringToBoolean(string value)
=> TryParseBooleanWord(value, out var parsed) ? parsed : value.Length > 0;
///
/// Tries to read a string as an explicit boolean word. Shared with the expression engine so
/// the words recognised by logical(), by equality coercion and by the condition
/// positions never drift apart. The set is English-only by design: a routing language must
/// not change meaning with the author's locale (the historical set also recognised Russian
/// words; they now read as ordinary non-empty strings).
///
/// The string to parse.
/// The parsed boolean when recognised.
/// true when the string is a recognised boolean word.
internal static bool TryParseBooleanWord(string value, out bool result)
{
result = false;
if (string.IsNullOrEmpty(value))
return false;
if (bool.TryParse(value, out result))
return true;
switch (value.Trim().ToLowerInvariant())
{
case "1" or "yes" or "y" or "on":
result = true;
return true;
case "0" or "no" or "n" or "off":
result = false;
return true;
default:
return false;
}
}
}