Path: redb-route/redb.Route.Firebase/Firestore/FirestoreQueryHelper.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.Firebase/Firestore/FirestoreQueryHelper.cs ---- using Google.Cloud.Firestore; namespace redb.Route.Firebase; /// /// Shared Firestore query building logic used by both producer and consumer. /// Eliminates code duplication for Where/OrderBy filter parsing. /// internal static class FirestoreQueryHelper { /// /// Parses "field==value;field2>10" into chained Firestore query filters. /// Supports: ==, !=, <, <=, >, >=, array-contains /// internal static Query ApplyWhereFilters(Query query, string where) { var conditions = where.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); foreach (var condition in conditions) { var (field, op, value) = ParseCondition(condition); query = op switch { "==" => query.WhereEqualTo(field, ParseValue(value)), "!=" => query.WhereNotEqualTo(field, ParseValue(value)), ">=" => query.WhereGreaterThanOrEqualTo(field, ParseValue(value)), "<=" => query.WhereLessThanOrEqualTo(field, ParseValue(value)), ">" => query.WhereGreaterThan(field, ParseValue(value)), "<" => query.WhereLessThan(field, ParseValue(value)), "array-contains" => query.WhereArrayContains(field, ParseValue(value)), _ => throw new InvalidOperationException($"Unknown Firestore filter operator: {op}") }; } return query; } /// /// Parses "fieldName [desc]" into an OrderBy clause. /// internal static Query ApplyOrderBy(Query query, string orderBy) { var parts = orderBy.Split(' ', StringSplitOptions.RemoveEmptyEntries); var field = parts[0]; var desc = parts.Length > 1 && parts[1].Equals("desc", StringComparison.OrdinalIgnoreCase); return desc ? query.OrderByDescending(field) : query.OrderBy(field); } internal static (string field, string op, string value) ParseCondition(string condition) { // `array-contains` only as a SPACED token: a field whose NAME contains the // substring ("my-array-contains-x==5") must not tear the condition apart. const string arrayContainsToken = " array-contains "; var acIdx = condition.IndexOf(arrayContainsToken, StringComparison.Ordinal); if (acIdx > 0) return (condition[..acIdx].Trim(), "array-contains", condition[(acIdx + arrayContainsToken.Length)..].Trim()); string[] operators = ["!=", ">=", "<=", "==", ">", "<"]; foreach (var op in operators) { var idx = condition.IndexOf(op, StringComparison.Ordinal); if (idx > 0) return (condition[..idx].Trim(), op, condition[(idx + op.Length)..].Trim()); } throw new InvalidOperationException($"Cannot parse Firestore where condition: {condition}"); } internal static object ParseValue(string value) { // Quoted literal: 'text' — strictly a string, no type coercion ('007' stays "007"). if (value.Length >= 2 && value[0] == '\'' && value[^1] == '\'') return value[1..^1]; if (int.TryParse(value, out var intVal)) return intVal; if (long.TryParse(value, out var longVal)) return longVal; if (double.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out var dblVal)) return dblVal; if (bool.TryParse(value, out var boolVal)) return boolVal; return value; } }