import re import json import pandas as pd import pprint from copy import deepcopy def debug_print_db_struct(db_struct): pprint.pprint( db_struct, width=80, indent=2 ) # Ajusta el parámetro 'width' según necesidad def expand_udt_references(db_struct, udts): """ Recursively expand UDT references in the given DB structure using the UDT definitions. This function specifically expands fields designated as 'type' which reference UDTs. """ if isinstance(db_struct, dict): for key, value in list(db_struct.items()): if isinstance(value, dict): # Recurse into dictionaries expand_udt_references(value, udts) elif isinstance(value, str) and key == "type": # Only expand 'type' fields type_name = value.strip( '"' ) # Remove quotes which may wrap UDT names with spaces if type_name in udts: # Replace the UDT reference with its deeply copied definition db_struct["is_udt_definition"] = True db_struct["fields"] = deepcopy(udts[type_name]) print(f"Expanded UDT '{type_name}' at field '{key}'") elif isinstance(db_struct, list): for item in db_struct: expand_udt_references(item, udts) def handle_array_types(db_struct): """ Handle array types to expand them into multiple fields as sub-elements while preserving comments. Modifies the structure in place by expanding array definitions within their original field. """ if isinstance(db_struct, dict): for key, value in list(db_struct.items()): if isinstance(value, dict): # Recursively process nested dictionaries handle_array_types(value) if isinstance(value, dict) and "type" in value: match = re.match(r"Array\[(\d+)\.\.(\d+)\] of (\w+)", value["type"]) if match: lower_bound, upper_bound, base_type = ( int(match.group(1)), int(match.group(2)), match.group(3), ) comment = value.get("comment", "") value["array_definition"] = True # Instead of popping the original key, initialize a sub-dictionary value["Array"] = ( {} ) # Initialize a sub-dictionary for array elements for i in range(lower_bound, upper_bound + 1): element_key = f"[{i}]" value["Array"][element_key] = { "type": base_type, "comment": comment, "is_array_element": True, } # Optionally, modify or remove the original type designation if necessary # value.pop('type', None) # Uncomment this if you want to remove the 'type' from the original # Continue the recursive handling if it's not an array definition elif isinstance(value, dict): handle_array_types(value) def expand_dbs(udts, dbs): """ Expand all UDT references in all DBs and then handle array types. """ for db_name, db_content in dbs.items(): print(f"Expanding DB: {db_name}") expand_udt_references(db_content, udts) handle_array_types(db_content) print(f"Completed expansion for DB: {db_name}")