| 122 | } |
| 123 | |
| 124 | fn evaluate_condition(&self, condition: &str, input: &Value) -> Result<bool> { |
| 125 | let condition = condition.trim(); |
| 126 | |
| 127 | // Handle .field == "value" |
| 128 | if condition.contains("==") { |
| 129 | let parts: Vec<&str> = condition.splitn(2, "==").collect(); |
| 130 | if parts.len() == 2 { |
| 131 | let left = parts[0].trim(); |
| 132 | let right = parts[1].trim().trim_matches('"'); |
| 133 | |
| 134 | let left_val = self.execute(left, input.clone())?; |
| 135 | if let Some(val) = left_val.first() { |
| 136 | return Ok(val.as_str().map(|s| s == right).unwrap_or(false)); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Handle .field != "value" |
| 142 | if condition.contains("!=") { |
| 143 | let parts: Vec<&str> = condition.splitn(2, "!=").collect(); |
| 144 | if parts.len() == 2 { |
| 145 | let left = parts[0].trim(); |
| 146 | let right = parts[1].trim().trim_matches('"'); |
| 147 | |
| 148 | let left_val = self.execute(left, input.clone())?; |
| 149 | if let Some(val) = left_val.first() { |
| 150 | return Ok(val.as_str().map(|s| s != right).unwrap_or(true)); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Handle .field | test("pattern") |
| 156 | if condition.contains("test(") { |
| 157 | if let Some(pipe_pos) = condition.find('|') { |
| 158 | let field = condition[..pipe_pos].trim(); |
| 159 | let test_part = condition[pipe_pos + 1..].trim(); |
| 160 | |
| 161 | if test_part.starts_with("test(") { |
| 162 | if let Some(end) = test_part.rfind(')') { |
| 163 | let pattern = test_part[5..end].trim().trim_matches('"'); |
| 164 | let field_val = self.execute(field, input.clone())?; |
| 165 | |
| 166 | if let Some(val) = field_val.first() { |
| 167 | if let Some(s) = val.as_str() { |
| 168 | return Ok(s.contains(pattern)); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Default to false for unknown conditions |
| 177 | Ok(false) |
| 178 | } |
| 179 | |
| 180 | pub fn execute_on_array(&self, query: &str, inputs: Vec<Value>) -> Result<Vec<Value>> { |
| 181 | let array = Value::Array(inputs); |