|
14 | 14 | ) |
15 | 15 |
|
16 | 16 | from ..lookups import builtin_lookup |
17 | | -from ..query_utils import key_transform_build_path, process_lhs, process_rhs |
| 17 | +from ..query_utils import process_lhs, process_rhs |
| 18 | + |
| 19 | + |
| 20 | +def build_json_mql_path(lhs, key_trasnforms): |
| 21 | + # Build the MQL path using the collected key transforms. |
| 22 | + result = lhs |
| 23 | + for key in key_trasnforms: |
| 24 | + get_field = {"$getField": {"input": result, "field": key}} |
| 25 | + # Handle array indexing if the key is a digit. If key is something |
| 26 | + # like '001', it's not an array index despite isdigit() returning True. |
| 27 | + if key.isdigit() and str(int(key)) == key: |
| 28 | + result = { |
| 29 | + "$cond": { |
| 30 | + "if": {"$isArray": result}, |
| 31 | + "then": {"$arrayElemAt": [result, int(key)]}, |
| 32 | + "else": get_field, |
| 33 | + } |
| 34 | + } |
| 35 | + else: |
| 36 | + result = get_field |
| 37 | + return result |
18 | 38 |
|
19 | 39 |
|
20 | 40 | def contained_by(self, compiler, connection): # noqa: ARG001 |
@@ -89,7 +109,7 @@ def key_transform(self, compiler, connection): |
89 | 109 | key_transforms.insert(0, previous.key_name) |
90 | 110 | previous = previous.lhs |
91 | 111 | lhs_mql = previous.as_mql(compiler, connection) |
92 | | - return key_transform_build_path(key_transforms, lhs_mql) |
| 112 | + return build_json_mql_path(lhs_mql, key_transforms) |
93 | 113 |
|
94 | 114 |
|
95 | 115 | def key_transform_in(self, compiler, connection): |
|
0 commit comments