This commit is contained in:
anhduy-tech
2026-01-09 08:21:01 +07:00
parent 8df7572ed0
commit 2b6498133f
10 changed files with 59 additions and 1 deletions

View File

@@ -86,6 +86,48 @@ def resolve_value(expr, context):
target_list.append(element_to_append)
return target_list
# =============================================
# 2.2. Hàm tổng hợp list: $agg(list, operation, field?)
# =============================================
agg_match = re.match(r"^\$agg\(([^,]+),\s*'([^']+)'(?:,\s*['\"]?([^'\"]+)['\"]?)?\)$", expr.strip())
if agg_match:
list_expr = agg_match.group(1).strip()
operation = agg_match.group(2).strip()
field_expr = agg_match.group(3).strip() if agg_match.group(3) else None
# 1. Resolve the list
target_list = resolve_value(list_expr, context)
if target_list is None:
target_list = []
if not isinstance(target_list, list):
return 0
# 2. Perform operation
if operation == 'count':
return len(target_list)
if operation == 'sum':
if not field_expr:
return 0
total = 0
for item in target_list:
value = 0
if isinstance(item, dict):
value = item.get(field_expr)
else:
value = getattr(item, field_expr, 0)
try:
total += float(value or 0)
except (ValueError, TypeError):
pass
return total
print(f" [ERROR] Unknown $agg operation: {operation}")
return 0
# =============================================
# 3. Helper: Lấy giá trị từ context theo đường dẫn dotted
# =============================================