From 8310b9b0e4245804abd39d7ba9fe073ef76b179c Mon Sep 17 00:00:00 2001 From: Chris Blades <41635127+cblades-tc@users.noreply.github.com> Date: Tue, 19 Nov 2024 10:09:58 -0500 Subject: [PATCH] [transform] refactor how the custom predefined function works (#336) --- release_notes.md | 2 +- .../ti_transform/ti_predefined_functions.py | 50 ++++++++++++------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/release_notes.md b/release_notes.md index 0acf48e34..b660ceff1 100644 --- a/release_notes.md +++ b/release_notes.md @@ -7,7 +7,7 @@ - APP-4620 - [transform] Added structured/contextualized exceptions to transform - APP-4632 - [transform] Added support for attribute.pinned field - APP-4640 - [api] Updated API hashing method to use SHA256 for FIPS compliance - +- APP-4656 - [transform] refactor how the custom predefined function works ## 4.0.6 diff --git a/tcex/api/tc/ti_transform/ti_predefined_functions.py b/tcex/api/tc/ti_transform/ti_predefined_functions.py index 82b0e6a9e..e9eb17a82 100644 --- a/tcex/api/tc/ti_transform/ti_predefined_functions.py +++ b/tcex/api/tc/ti_transform/ti_predefined_functions.py @@ -100,10 +100,39 @@ class ProcessingFunctions: def __init__(self, tcex) -> None: """.""" self.tcex = tcex + self.custom_fns = {} - def custom(self, value, description: str): + @custom_function_definition( + { + 'name': 'custom', + 'label': 'Custom', + 'help': 'Describe a custom processing function.', + 'params': [ + { + 'default': None, + 'name': 'name', + 'label': 'Name', + 'type': 'str', + 'help': 'A unique name for this custom processing function.', + 'required': True, + }, + { + 'default': None, + 'name': 'description', + 'label': 'Description', + 'type': 'str', + 'help': 'Describe the custom processing function.', + 'required': True, + }, + ], + } + ) + def custom(self, value, name: str, description: str, ti_dict, transform, **kwargs): """Allow for custom processing to be described.""" - raise RuntimeError(f'Custom function not implemented: {description}') + fn = self.custom_fns.get(name.lower()) + if not fn: + raise NotImplementedError(f'Custom function not implemented: {description}') + return fn(value, ti_dict=ti_dict, transform=transform, **kwargs) def static_map(self, value, mapping: dict): """Map values to static values. @@ -122,23 +151,6 @@ def value_in(self, value, values: str, delimiter: str = ','): return value if value in [v.strip() for v in json.loads(values).split(delimiter)] else None - @custom_function_definition( - { - 'name': 'format_table', - 'label': 'Format Objects as Markdown Table', - 'help': 'Format a list of objects as a markdown table.', - 'params': [ - { - 'default': None, - 'name': 'column_order', - 'label': 'Column Order', - 'type': 'str', - 'help': 'The order of the columns.', - 'required': False, - } - ], - } - ) def format_table(self, value, column_order: str): """Format a markdown table.