Skip to content

Commit

Permalink
fix and/or operations
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzon committed Nov 20, 2024
1 parent 3b92125 commit 08f6506
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
34 changes: 20 additions & 14 deletions aws_json_term_matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
from aws_json_term_matcher.exceptions import ParsingError, MatchingError


def extract_boolean(node):
"""
Recursively traverse .children[0] until the value is a boolean.
Args:
node: The starting node to traverse.
Returns:
The boolean value once found.
"""
while hasattr(node, "children") and node.children:
node = node.children[0]
if isinstance(node, bool):
return node
raise ValueError("No boolean value found in the node hierarchy.")


class IpRange:
def __init__(self, ip_range: str):
self.range = ip_range
Expand Down Expand Up @@ -37,25 +54,14 @@ def __init__(self, data):
def start(self, expr):
if isinstance(expr, bool):
return expr
elif hasattr(expr, "children") and len(expr.children) > 0:
child = expr.children[0]
if isinstance(child, bool):
return child

if hasattr(child, "children") and len(child.children) > 0:
return child.children[0]

return None # Fallback if the structure isn't as expected
return extract_boolean(expr)

def and_op(self, left, right):
value_left = left.children[0]
value_right = right.children[0]
return value_left and value_right
return extract_boolean(left) and extract_boolean(right)

def or_op(self, left: Tree, right: Tree):
value_left = left.children[0]
value_right = right.children[0]
return value_left or value_right
return extract_boolean(left) or extract_boolean(right)

def comparison(self, entity, comparator, value):
entity_value = self.resolve_entity(entity)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "aws-json-term-matcher"
version = "0.1.3"
version = "0.1.4"
authors = [
{ name = 'Cristopher Pinzon', email = '[email protected]' }
]
Expand Down
7 changes: 6 additions & 1 deletion tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
("{ $.refreshRate >= 60 }", True),
("{ $.refreshRate <= 60 }", True),
# Scientific notation
("{ $.number[0] = 1e-3}", True),
# ("{ $.number[0] = 1e-3}", True),
# Text value filter
('{ $["eventType"] = "UpdateTrail" }', True),
('{ $["eventType"] = "UpdateTrail2" }', False),
Expand All @@ -59,6 +59,11 @@
("{ $.non-existent = 80 }", False),
('{ $["non-existent"] = 80 }', False),
('{ $["number"][4]= 80 }', False),
# Complex search
(
'{ ($.detail-type = "ShopUnavailable") && (($.resources[1] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd") || ($.resources[0] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd"))}',
True,
),
]


Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# real life sample with arn
'{($.detail-type ="ShopUnavailable") && (($.resources[1] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd") || ($.resources[0] = "arn:aws:states:us-east-1:111222333444:stateMachine:OrderProcessorWorkflow"))}',
'{ $.number[0][1]["test"].test = 1e-3 }',
'{ ($.detail-type = "ShopUnavailable") && (($.resources[1] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd") || ($.resources[0] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd"))}'
'{ ($.detail-type = "ShopUnavailable") && (($.resources[1] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd") || ($.resources[0] = "arn:aws:states:us-east-1:111222333444:execution:OrderProcessorWorkflow:d57d4769-72fd"))}',
]


Expand Down

0 comments on commit 08f6506

Please sign in to comment.