|
| 1 | +/* |
| 2 | + * Copyright 2026, the Chromium project authors. Please see the AUTHORS file |
| 3 | + * for details. All rights reserved. Use of this source code is governed by a |
| 4 | + * BSD-style license that can be found in the LICENSE file. |
| 5 | + */ |
| 6 | + |
| 7 | +package io.flutter.plugins.firebase.firestore.utils; |
| 8 | + |
| 9 | +import androidx.annotation.NonNull; |
| 10 | +import com.google.firebase.Timestamp; |
| 11 | +import com.google.firebase.firestore.Blob; |
| 12 | +import com.google.firebase.firestore.DocumentReference; |
| 13 | +import com.google.firebase.firestore.GeoPoint; |
| 14 | +import com.google.firebase.firestore.VectorValue; |
| 15 | +import com.google.firebase.firestore.pipeline.BooleanExpression; |
| 16 | +import com.google.firebase.firestore.pipeline.Expression; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** Helper utilities for parsing expressions and handling common patterns. */ |
| 21 | +class ExpressionHelpers { |
| 22 | + |
| 23 | + /** |
| 24 | + * Parses an "and" expression from a list of expression maps. Uses Expression.and() with varargs |
| 25 | + * signature. |
| 26 | + * |
| 27 | + * @param exprMaps List of expression maps to combine with AND |
| 28 | + * @param parser Reference to ExpressionParsers for recursive parsing |
| 29 | + */ |
| 30 | + @SuppressWarnings("unchecked") |
| 31 | + static BooleanExpression parseAndExpression( |
| 32 | + @NonNull List<Map<String, Object>> exprMaps, @NonNull ExpressionParsers parser) { |
| 33 | + if (exprMaps == null || exprMaps.isEmpty()) { |
| 34 | + throw new IllegalArgumentException("'and' requires at least one expression"); |
| 35 | + } |
| 36 | + |
| 37 | + BooleanExpression first = parser.parseBooleanExpression(exprMaps.get(0)); |
| 38 | + if (exprMaps.size() == 1) { |
| 39 | + return first; |
| 40 | + } |
| 41 | + |
| 42 | + BooleanExpression[] rest = new BooleanExpression[exprMaps.size() - 1]; |
| 43 | + for (int i = 1; i < exprMaps.size(); i++) { |
| 44 | + rest[i - 1] = parser.parseBooleanExpression(exprMaps.get(i)); |
| 45 | + } |
| 46 | + return Expression.and(first, rest); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Parses an "or" expression from a list of expression maps. Uses Expression.or() with varargs |
| 51 | + * signature. |
| 52 | + * |
| 53 | + * @param exprMaps List of expression maps to combine with OR |
| 54 | + * @param parser Reference to ExpressionParsers for recursive parsing |
| 55 | + */ |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + static BooleanExpression parseOrExpression( |
| 58 | + @NonNull List<Map<String, Object>> exprMaps, @NonNull ExpressionParsers parser) { |
| 59 | + if (exprMaps == null || exprMaps.isEmpty()) { |
| 60 | + throw new IllegalArgumentException("'or' requires at least one expression"); |
| 61 | + } |
| 62 | + |
| 63 | + BooleanExpression first = parser.parseBooleanExpression(exprMaps.get(0)); |
| 64 | + if (exprMaps.size() == 1) { |
| 65 | + return first; |
| 66 | + } |
| 67 | + |
| 68 | + BooleanExpression[] rest = new BooleanExpression[exprMaps.size() - 1]; |
| 69 | + for (int i = 1; i < exprMaps.size(); i++) { |
| 70 | + rest[i - 1] = parser.parseBooleanExpression(exprMaps.get(i)); |
| 71 | + } |
| 72 | + return Expression.or(first, rest); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parses a constant value based on its type to match Android SDK constant() overloads. Valid |
| 77 | + * types: String, Number, Boolean, Date, Timestamp, GeoPoint, byte[], Blob, DocumentReference, |
| 78 | + * VectorValue |
| 79 | + */ |
| 80 | + static Expression parseConstantValue(@NonNull Object value) { |
| 81 | + |
| 82 | + if (value instanceof String) { |
| 83 | + return Expression.constant((String) value); |
| 84 | + } else if (value instanceof Number) { |
| 85 | + return Expression.constant((Number) value); |
| 86 | + } else if (value instanceof Boolean) { |
| 87 | + return Expression.constant((Boolean) value); |
| 88 | + } else if (value instanceof java.util.Date) { |
| 89 | + return Expression.constant((java.util.Date) value); |
| 90 | + } else if (value instanceof Timestamp) { |
| 91 | + return Expression.constant((Timestamp) value); |
| 92 | + } else if (value instanceof GeoPoint) { |
| 93 | + return Expression.constant((GeoPoint) value); |
| 94 | + } else if (value instanceof byte[]) { |
| 95 | + return Expression.constant((byte[]) value); |
| 96 | + } else if (value instanceof List) { |
| 97 | + // Handle List<int> from Dart which comes as List<Integer> or List<Number> |
| 98 | + // This represents byte[] (byte array) for constant expressions |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + List<?> list = (List<?>) value; |
| 101 | + // Check if all elements are numbers (for byte array) |
| 102 | + boolean isByteArray = true; |
| 103 | + for (Object item : list) { |
| 104 | + if (!(item instanceof Number)) { |
| 105 | + isByteArray = false; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + if (isByteArray && !list.isEmpty()) { |
| 110 | + byte[] byteArray = new byte[list.size()]; |
| 111 | + for (int i = 0; i < list.size(); i++) { |
| 112 | + byteArray[i] = ((Number) list.get(i)).byteValue(); |
| 113 | + } |
| 114 | + return Expression.constant(byteArray); |
| 115 | + } |
| 116 | + // If not a byte array, fall through to error |
| 117 | + } else if (value instanceof Blob) { |
| 118 | + return Expression.constant((Blob) value); |
| 119 | + } else if (value instanceof DocumentReference) { |
| 120 | + return Expression.constant((DocumentReference) value); |
| 121 | + } else if (value instanceof VectorValue) { |
| 122 | + return Expression.constant((VectorValue) value); |
| 123 | + } |
| 124 | + |
| 125 | + throw new IllegalArgumentException( |
| 126 | + "Constant value must be one of: String, Number, Boolean, Date, Timestamp, " |
| 127 | + + "GeoPoint, byte[], Blob, DocumentReference, or VectorValue. Got: " |
| 128 | + + value.getClass().getName()); |
| 129 | + } |
| 130 | + |
| 131 | + /** Safely extracts a single expression from args map. */ |
| 132 | + @SuppressWarnings("unchecked") |
| 133 | + static Map<String, Object> extractExpression( |
| 134 | + @NonNull Map<String, Object> args, @NonNull String key) { |
| 135 | + Map<String, Object> exprMap = (Map<String, Object>) args.get(key); |
| 136 | + if (exprMap == null) { |
| 137 | + throw new IllegalArgumentException("Missing required expression argument: " + key); |
| 138 | + } |
| 139 | + return exprMap; |
| 140 | + } |
| 141 | + |
| 142 | + /** Safely extracts a list of expressions from args map. */ |
| 143 | + @SuppressWarnings("unchecked") |
| 144 | + static List<Map<String, Object>> extractExpressionList( |
| 145 | + @NonNull Map<String, Object> args, @NonNull String key) { |
| 146 | + List<Map<String, Object>> exprMaps = (List<Map<String, Object>>) args.get(key); |
| 147 | + if (exprMaps == null || exprMaps.isEmpty()) { |
| 148 | + throw new IllegalArgumentException("Missing or empty expression list: " + key); |
| 149 | + } |
| 150 | + return exprMaps; |
| 151 | + } |
| 152 | +} |
0 commit comments