Optimized path mapping

This commit is contained in:
Kevin Veen-Birkenbach 2025-01-17 02:14:48 +01:00
parent 8c7dc02bd5
commit dc11dc799b

View File

@ -46,7 +46,7 @@ class ConfigurationResolver:
raise ValueError(f"Expected 'children' to be a list, but got {type(value).__name__} instead.") raise ValueError(f"Expected 'children' to be a list, but got {type(value).__name__} instead.")
for item in value: for item in value:
if "link" in item: if "link" in item:
loaded_link = self._find_entry(root_config, item['link'].lower(), False) loaded_link = self._find_entry(root_config, self._mapped_key(item['link']), False)
if isinstance(loaded_link, list): if isinstance(loaded_link, list):
self._replace_in_list_by_list(value,item,loaded_link) self._replace_in_list_by_list(value,item,loaded_link)
else: else:
@ -55,15 +55,15 @@ class ConfigurationResolver:
self._recursive_resolve(value, root_config) self._recursive_resolve(value, root_config)
elif key == "link": elif key == "link":
try: try:
loaded = self._find_entry(root_config, value.lower(), True) loaded = self._find_entry(root_config, self._mapped_key(value), True)
if isinstance(loaded, list) and len(loaded) > 2: if isinstance(loaded, list) and len(loaded) > 2:
loaded = self._find_entry(root_config, value.lower(), False) loaded = self._find_entry(root_config, self._mapped_key(value), False)
current_config.clear() current_config.clear()
current_config.update(loaded) current_config.update(loaded)
except Exception as e: except Exception as e:
raise ValueError( raise ValueError(
f"Error resolving link '{value}': {str(e)}. " f"Error resolving link '{value}': {str(e)}. "
f"Current path: {key}, Current config: {current_config}" + (f", Loaded: {loaded}" if 'loaded' in locals() or 'loaded' in globals() else "") f"Current part: {key}, Current config: {current_config}" + (f", Loaded: {loaded}" if 'loaded' in locals() or 'loaded' in globals() else "")
) )
else: else:
self._recursive_resolve(value, root_config) self._recursive_resolve(value, root_config)
@ -76,9 +76,12 @@ class ConfigurationResolver:
current = current["children"] current = current["children"]
return current return current
def _mapped_key(self,name):
return name.replace(" ", "").lower()
def _find_by_name(self,current, part): def _find_by_name(self,current, part):
return next( return next(
(item for item in current if isinstance(item, dict) and item.get("name", "").lower() == part), (item for item in current if isinstance(item, dict) and self._mapped_key(item.get("name", "")) == part),
None None
) )
@ -108,7 +111,8 @@ class ConfigurationResolver:
) )
elif isinstance(current, dict): elif isinstance(current, dict):
# Case-insensitive dictionary lookup # Case-insensitive dictionary lookup
key = next((k for k in current if k.lower() == part), None) key = next((k for k in current if self._mapped_key(k) == part), None)
# If no fitting key was found search in the children
if key is None: if key is None:
current = self._find_by_name(current["children"],part) current = self._find_by_name(current["children"],part)
if not current: if not current: