Skip to content

Commit

Permalink
more robust index selection
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzon committed Oct 16, 2024
1 parent ac91e92 commit 51ac4fa
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
4 changes: 2 additions & 2 deletions aws_json_term_matcher/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ comparison: entity COMPARATOR value
COMPARATOR: "=" | "!=" | ">" | ">=" | "<" | "<="

entity: "$" selection
selection: attribute_access | index_access | selection "." selection
selection: (attribute_access | index_access)+
attribute_access: "." NAME | "[" ESCAPED_STRING "]"
index_access: attribute_access"[" INT "]" | "[" INT "]"
index_access: "[" INT "]"

value: ESCAPED_STRING | NUMBER | SCIENTIFIC | IP | WILDCARD_IP

Expand Down
7 changes: 1 addition & 6 deletions aws_json_term_matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ def _resolve(node):
) # Attribute accessed like ["attr"]

elif node.data == "index_access":
# Handles indices like $[0] or $.attribute[1]
attr = node.children[0].children[0].value
keys.append(attr.strip("\"'"))
index = node.children[
1
].value # The index is the second child in the rule
index = node.children[0].value
keys.append(index)

elif node.data == "selection":
Expand Down
5 changes: 2 additions & 3 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'{( $.eventType = "UpdateTrail") || (($.eventType = "UpdateTrail2") && ($.eventType[2] = "uts")) }',
# 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 }',
]


Expand Down Expand Up @@ -54,7 +55,5 @@ def test_parse_filter(filter_definition):

@pytest.mark.parametrize("filter_definition", error_cases)
def test_error_message(filter_definition):
with pytest.raises(ParsingError) as e:
with pytest.raises(ParsingError):
parse_filter(filter_definition)

assert str(e)

0 comments on commit 51ac4fa

Please sign in to comment.