Skip to content

Commit

Permalink
Fixes #5 - get_matches may exit before scanning all includes
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Hopkins committed Dec 18, 2018
1 parent 8499e66 commit 29a2281
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
26 changes: 14 additions & 12 deletions jsonrouter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ def __repr__(self):
def get_matches(self, json_data):
m = {}
for var in self.vars:
match = var.get_matches(json_data)
if match == False:
matches = var.get_matches(json_data)
if matches == False:
# Field not found or did not match criteria
return

if match:
if matches:
# Merge dict together for all matches
m = {**m, **match}
for match in matches:
m = {**m, **match}

return m

Expand Down Expand Up @@ -223,13 +224,14 @@ def get_matches(self, json_data):
for exclude in self.excludes:
if exclude.match(match):
return False


matches = []
for include in self.includes:
m = include.match(match)
if m:
return get_groups(self.name, m)
else:
return False
matches.append(get_groups(self.name, m))

return matches if matches else False


class JsonMatchEngine(object):
Expand Down Expand Up @@ -276,10 +278,10 @@ def route_matches(self, records):
router = self.registered_routers.get(rname)
if router:
router(matched_rule)
else:
# Will error with name not registered or None if name field not supplied
raise KeyError(
"'{name}' is not a registered router".format(name=rname))
else:
# Will error with name not registered or None if name field not supplied
raise KeyError(
"'{name}' is not a registered router".format(name=rname))

return matched_rules

Expand Down
38 changes: 37 additions & 1 deletion tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,40 @@ def test_match_includes():

print(json.dumps(matches, indent=4))
assert len(matches) == 1



configs_multiple_includes = yaml.load('''rules:
- name: multiple-includes
routers:
- name: print-router
vars:
# - name: title
# jsonpath: $.title
- name: created_at
jsonpath: $.created_at
- name: urgency
jsonpath: $.urgency
includes:
- low
- high
''')
def test_match_multiple_includes():
eng = JsonMatchEngine(configs_multiple_includes, registered_routers)
matches = eng.route_matches(records)

print(json.dumps(matches, indent=4))
assert len(matches) == 3


configs_bad_router = yaml.load('''rules:
- name: bad-router-name
routers:
- name: bogus-router
vars:
- name: created_at
jsonpath: $.created_at
''')
def test_bad_router_name():
with pytest.raises(KeyError):
eng = JsonMatchEngine(configs_bad_router, registered_routers)
matches = eng.route_matches(records)

0 comments on commit 29a2281

Please sign in to comment.