Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions tests/test_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"neg": -1,
"bool": True,
"nesting": {"fib": [1, 1, 2, 3, 5], "foo": {"bar": "baz"}},
"dot.key": 3.14,
}


Expand Down Expand Up @@ -105,8 +106,8 @@ def test_prop_or_prop() -> None:
{},
)
assert str(query) == (
'((json_extract(properties, "$.int") == 2) OR '
'(json_extract(properties, "$.int") == 3))'
"""((json_extract(properties, '$."int"') == 2) OR """
"""(json_extract(properties, '$."int"') == 3))"""
)


Expand Down Expand Up @@ -571,3 +572,19 @@ def test_contains_str(
eval_locals,
)
assert bool(check(result)) is True

@staticmethod
def test_key_with_period(
eval_globals: dict[str, object],
eval_locals: Mapping[str, object],
check: Callable,
) -> None:
"""Test key with period."""
query = "props['dot.key']"
result = eval( # skipcq: PYL-W0123 # noqa: S307
query,
eval_globals,
eval_locals,
)

assert check(result) == 3.14
6 changes: 3 additions & 3 deletions tiatoolbox/annotation/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ def __init__(self: SQLJSONDictionary, acc: str | None = None) -> None:

def __str__(self: SQLJSONDictionary) -> str:
"""Return a human-readable, or informal, string representation of an object."""
return f"json_extract(properties, {json.dumps(f'$.{self.acc}')})"
return f"json_extract(properties, '$.{self.acc}')"

def __getitem__(self: SQLJSONDictionary, key: str) -> SQLJSONDictionary:
"""Get an item from the dataset."""
key_str = f"[{key}]" if isinstance(key, (int,)) else str(key)
key_str = f"[{key}]" if isinstance(key, (int,)) else f'"{key}"'

joiner = "." if self.acc and not isinstance(key, int) else ""
return SQLJSONDictionary(acc=self.acc + joiner + f"{key_str}")
return SQLJSONDictionary(acc=self.acc + joiner + key_str)

def get(
self: SQLJSONDictionary,
Expand Down