improve support to meili

This commit is contained in:
2025-07-08 19:59:21 -03:00
parent 4a7e3de9a9
commit 690be35634
9 changed files with 165 additions and 18 deletions

62
http-api/app/meili.py Normal file
View File

@@ -0,0 +1,62 @@
import re
OPERATORS = [
'!=',
'>=',
'<=',
'=',
'>',
'<',
'TO',
'EXISTS',
'IN',
'NOT IN',
]
def remove_quotes(value: str) -> str:
if (value.startswith('"') and value.endswith('"')) or (
value.startswith("'") and value.endswith("'")
):
return value[1:-1]
return value
def parse_condition(condition: str) -> dict[str, str] | None:
for op in OPERATORS:
parts = condition.split(op)
if len(parts) == 2:
attr, value = parts
attr = attr.strip()
value = remove_quotes(value.strip())
return {
'attr': attr,
'op': op,
'value': value,
}
return None
def parse(s: str) -> list[dict]:
filter_expr = re.sub(r'\s+', ' ', s).strip()
if filter_expr == '':
return []
parts = re.split(r'\b(?:AND|OR)\b', filter_expr)
conditions = []
for part in parts:
condition = parse_condition(part.strip())
if condition:
conditions.append(condition)
return conditions
def encode(conditions: list[dict]):
return ' AND '.join(f'{c["attr"]} {c["op"]} {c["value"]}' for c in conditions if c)