Skip to content

Commit

Permalink
fix: dev: Refine list flattening.
Browse files Browse the repository at this point in the history
  • Loading branch information
dallinb committed Dec 31, 2024
1 parent 9e48a31 commit f5c31aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
33 changes: 29 additions & 4 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import itertools
import json
import logging
import os
Expand Down Expand Up @@ -317,6 +316,28 @@ def decode_message(self, message: bytes) -> str:

return message.decode('utf-8')

def flatten_list(self, data: list) -> list:
"""
Flatten a possibly deeply nested list.
Parameters
----------
data : list
The list to be flattened.
Returns
-------
list
A flattened list.
"""
flat_list = []
for item in data:
if isinstance(item, list): # Check if the item is a list
flat_list.extend(self.flatten_list(item)) # Recursively flatten it
else:
flat_list.append(item) # Otherwise, add the item to the flat list
return flat_list

def get_data(self, message: object) -> list:
"""
Get the data required from the message to do a comparison.
Expand Down Expand Up @@ -348,8 +369,12 @@ def get_data(self, message: object) -> list:
result = jmespath.search(self.jmespath, message)

if isinstance(result, list):
return list(itertools.chain.from_iterable(result))
logger.debug(f'List result is "{result}".')
result = self.flatten_list(result)
logger.debug(f'Converted result to "{result}".')
return result
elif result is None:
logger.debug(f'No match for JMESPath "{self.jmespath}" in "{message}".')
return []

return [result]
Expand All @@ -369,7 +394,6 @@ def is_data_match(self, message: str) -> bool:
Does the data match.
"""
data = self.get_data(message)
print(f'data is "{data}".')
prog = re.compile(self.regexp)

if data and any(prog.search(element) for element in data):
Expand All @@ -394,14 +418,15 @@ def is_match(self, source_topic_name: str, message: str) -> tuple:
A tuple containing if the rule is a match to the message, the
destination namespaces(s) and the destination topics(s).
"""
print(f'{source_topic_name}:{self.source_topic}')
logger.debug(f'Checking message against rule {self.name()}...')
if source_topic_name == self.source_topic:
if not self.regexp:
return True, self.destination_namespaces, self.destination_topics
elif self.is_data_match(message):
return True, self.destination_namespaces, self.destination_topics

# If we got here, it ain't a match.
logger.debug(f'Rule {self.name()} does not match against the message.')
return (False, None, None)

def name(self, name: str = None) -> str:
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ services:
ROUTER_RULE_COUNTRY_GB: '{ "destination_namespaces": "GB", "destination_topics": "gb.topic", "jmespath": "country", "regexp": "^GB$", "source_subscription": "test", "source_topic": "topic.1"}'
ROUTER_RULE_COUNTRY_FR: '{ "destination_namespaces": "", "destination_topics": "", "jmespath": "country", "regexp": "^$$ISO_3166_1_ALPHA_2$", "source_subscription": "test", "source_topic": "topic.1"}'
ROUTER_RULE_COUNTRY_IE: '{ "destination_namespaces": "IE", "destination_topics": "ie.topic", "jmespath": "country", "regexp": "^IE$", "source_subscription": "test", "source_topic": "topic.2"}'
ROUTER_RULE_GB_TELNO: '{"destination_namespaces":"GB","destination_topics":"gb.topic","jmespath":"details[?telephone_number].telephone_number","regexp":"^\\+44","source_subscription":"test","source_topic":"topic.1"}'
ROUTER_RULE_IE_TELNO: '{"destination_namespaces":"IE","destination_topics":"ie.topic","jmespath":"details[?telephone_number].telephone_number","regexp":"^\\+353","source_subscription":"test","source_topic":"topic.2"}'
ROUTER_RULE_GB_TELNO: '{"destination_namespaces":"GB","destination_topics":"gb.topic","jmespath":"details[?telephone_number].telephone_number","regexp":"^.*44","source_subscription":"test","source_topic":"topic.1"}'
ROUTER_RULE_IE_TELNO: '{"destination_namespaces":"IE","destination_topics":"ie.topic","jmespath":"details[?telephone_number].telephone_number","regexp":"^.*353","source_subscription":"test","source_topic":"topic.2"}'
ROUTER_SOURCE_CONNECTION_STRING: "Endpoint=sb://emulator;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"
image: router:latest
ports:
Expand Down

0 comments on commit f5c31aa

Please sign in to comment.