diff --git a/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py b/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py index 8d86368..feb17b6 100644 --- a/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py +++ b/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py @@ -18,10 +18,28 @@ def run(manifest, catalog, **kwargs): def replace_column_name(column_name: str) -> str: + """Replace column names containing special characters. + To prevent mermaid from not being able to render column names that may contain special characters. + + Args: + column_name (str): column name + + Returns: + str: Column name with special characters substituted + """ return column_name.replace(" ", "-").replace(".", "__") def match_complex_column_type(column_type: str) -> Optional[str]: + """Returns the root type from nested complex types. + As an example, if the input is `Struct`, return `Struct`. + + Args: + column_type (str): column type + + Returns: + Optional[str]: Returns the root type if the input type is a nested complex type, otherwise returns `None` for primitive types + """ pattern = r"(\w+)<(\w+\s+\w+(\s*,\s*\w+\s+\w+)*)>" match = re.match(pattern, column_type) if match: @@ -31,6 +49,15 @@ def match_complex_column_type(column_type: str) -> Optional[str]: def replace_column_type(column_type: str) -> str: + """If the type string contains special characters that cannot be drawn by mermaid, replace them with strings that can be drawn. + If the type string contains a nested complex type, omit it to make it easier to read. + + Args: + column_type (str): column type + + Returns: + str: Type of column with special characters are substituted or omitted + """ # Some specific DWHs may have types that cannot be drawn in mermaid, such as `Struct`. # These types may be nested and can be very long, so omit them complex_column_type = match_complex_column_type(column_type)