From a761733364cb84a7332b771f66c309695d2e42ac Mon Sep 17 00:00:00 2001 From: measty <20169086+measty@users.noreply.github.com> Date: Sat, 20 Jan 2024 16:41:06 +0000 Subject: [PATCH 1/3] fix json_extract construction --- tiatoolbox/annotation/dsl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tiatoolbox/annotation/dsl.py b/tiatoolbox/annotation/dsl.py index 4aa6536bd..9684fa7bc 100644 --- a/tiatoolbox/annotation/dsl.py +++ b/tiatoolbox/annotation/dsl.py @@ -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) 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 + f'"{key_str}"') def get( self: SQLJSONDictionary, From a8096e5438fe224326427e21994e3cae1bbb63b9 Mon Sep 17 00:00:00 2001 From: measty <20169086+measty@users.noreply.github.com> Date: Sat, 20 Jan 2024 17:50:39 +0000 Subject: [PATCH 2/3] fix integer property case --- tests/test_dsl.py | 4 ++-- tiatoolbox/annotation/dsl.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_dsl.py b/tests/test_dsl.py index c56b8d859..e2be631c8 100644 --- a/tests/test_dsl.py +++ b/tests/test_dsl.py @@ -105,8 +105,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))""" ) diff --git a/tiatoolbox/annotation/dsl.py b/tiatoolbox/annotation/dsl.py index 9684fa7bc..6e3e00db7 100644 --- a/tiatoolbox/annotation/dsl.py +++ b/tiatoolbox/annotation/dsl.py @@ -269,10 +269,10 @@ def __str__(self: SQLJSONDictionary) -> str: 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, From 656110e3e8ef7d6f8576609f60899e4bd2e87099 Mon Sep 17 00:00:00 2001 From: measty <20169086+measty@users.noreply.github.com> Date: Sat, 27 Jan 2024 01:40:54 +0000 Subject: [PATCH 3/3] add test for key containing a . --- tests/test_dsl.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_dsl.py b/tests/test_dsl.py index e2be631c8..46bf2f677 100644 --- a/tests/test_dsl.py +++ b/tests/test_dsl.py @@ -46,6 +46,7 @@ "neg": -1, "bool": True, "nesting": {"fib": [1, 1, 2, 3, 5], "foo": {"bar": "baz"}}, + "dot.key": 3.14, } @@ -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