From 885ea292fcdeae69cf03627dbc5e92d835de59ed Mon Sep 17 00:00:00 2001 From: antoine-de Date: Thu, 29 Aug 2024 15:52:26 +0200 Subject: [PATCH] Handle boolean in ecql like cql_text For the moment ```python assert parse("attr = true") == ast.Equal( ast.Attribute("attr"), ast.Attribute("true"), ) ``` like https://github.com/geopython/pygeofilter/pull/96, use lark terminal priority to ensure boolean are parsed as such and not as attribute. --- pygeofilter/parsers/ecql/grammar.lark | 2 +- pygeofilter/parsers/ecql/parser.py | 2 +- tests/parsers/ecql/test_parser.py | 34 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pygeofilter/parsers/ecql/grammar.lark b/pygeofilter/parsers/ecql/grammar.lark index 75611d9..bb1314a 100644 --- a/pygeofilter/parsers/ecql/grammar.lark +++ b/pygeofilter/parsers/ecql/grammar.lark @@ -109,7 +109,7 @@ period: DATETIME "/" DATETIME envelope: "ENVELOPE" "(" number number number number ")" -BOOLEAN: ( "TRUE" | "FALSE" ) +BOOLEAN.2: ( "TRUE"i | "FALSE"i ) DOUBLE_QUOTED: "\"" /.*?/ "\"" SINGLE_QUOTED: "'" /.*?/ "'" diff --git a/pygeofilter/parsers/ecql/parser.py b/pygeofilter/parsers/ecql/parser.py index 2a1c4f7..b3de9ae 100644 --- a/pygeofilter/parsers/ecql/parser.py +++ b/pygeofilter/parsers/ecql/parser.py @@ -181,7 +181,7 @@ def FLOAT(self, value): return float(value) def BOOLEAN(self, value): - return value == "TRUE" + return value.lower() == "true" def DOUBLE_QUOTED(self, token): return token[1:-1] diff --git a/tests/parsers/ecql/test_parser.py b/tests/parsers/ecql/test_parser.py index 9fa7fd9..5266b23 100644 --- a/tests/parsers/ecql/test_parser.py +++ b/tests/parsers/ecql/test_parser.py @@ -41,6 +41,7 @@ def test_namespace_attribute_eq_literal(): "A", ) + def test_prefixed_attribute_eq_literal(): result = parse("properties.ns:attr = 'A'") assert result == ast.Equal( @@ -48,6 +49,7 @@ def test_prefixed_attribute_eq_literal(): "A", ) + def test_attribute_eq_literal(): result = parse("attr = 'A'") assert result == ast.Equal( @@ -595,3 +597,35 @@ def test_function_attr_string_arg(): ], ), ) + + +def test_attribute_eq_true_uppercase(): + result = parse("attr = TRUE") + assert result == ast.Equal( + ast.Attribute("attr"), + True, + ) + + +def test_attribute_eq_true_lowercase(): + result = parse("attr = true") + assert result == ast.Equal( + ast.Attribute("attr"), + True, + ) + + +def test_attribute_eq_false_uppercase(): + result = parse("attr = FALSE") + assert result == ast.Equal( + ast.Attribute("attr"), + False, + ) + + +def test_attribute_eq_false_lowercase(): + result = parse("attr = false") + assert result == ast.Equal( + ast.Attribute("attr"), + False, + )