Skip to content

Commit

Permalink
chore(defs_replace_recursively): Swap order of defs/items
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Sep 19, 2024
1 parent 349da8a commit 65f0384
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
19 changes: 11 additions & 8 deletions pkg-py/src/brand_yaml/_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def resolve_with_values(self):
return self

logger.debug("validating model and resolving with_ values")
defs_replace_recursively(self.with_, self, name="with_")
defs_replace_recursively(self, defs=self.with_, name="with_")
return self


Expand Down Expand Up @@ -112,15 +112,15 @@ def defs_get(
)

if isinstance(with_value, (dict, BaseModel)):
defs_replace_recursively(defs, with_value, level=level)
defs_replace_recursively(with_value, defs=defs, level=level)
return with_value
else:
return with_value


def defs_replace_recursively(
defs: Any,
items: dict | BaseModel | None = None,
items: dict | BaseModel | None,
defs: Any = None,
level: int = 0,
name: str | None = None,
exclude: str | None = None,
Expand All @@ -133,12 +133,12 @@ def defs_replace_recursively(
Parameters
----------
items
A dictionary or pydantic model in which values should be replaced.
defs
A dictionary of definitions.
items
A dictionary or pydantic model to replace values in.
level
The current recursion level. Used internally and for logging.
Expand All @@ -149,6 +149,9 @@ def defs_replace_recursively(
refer to definitions in `defs`, the are replaced with copies of the
definition.
"""
if defs is None:
defs = items

if level == 0:
logger.debug("Checking for circular references")
check_circular_references(defs, name=name)
Expand Down Expand Up @@ -184,8 +187,8 @@ def defs_replace_recursively(
# TODO: we may want to avoid recursing into child BrandWith instances
logger.debug(level_indent(f"recursing into {key}", level))
defs_replace_recursively(
defs,
value,
defs=defs,
level=level + 1,
exclude=exclude,
name=name,
Expand Down
6 changes: 3 additions & 3 deletions pkg-py/src/brand_yaml/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def create_brand_palette(cls, value: dict[str, str] | None):
# We resolve `color.palette` on load or on replacement only
# TODO: Replace with class with getter/setters
# Retain original values, return resolved values, and re-validate on update.
defs_replace_recursively(value, value, name="palette")
defs_replace_recursively(value, name="palette")

return value

Expand All @@ -114,16 +114,16 @@ def _color_defs(self, resolved: bool = False) -> dict[str, str]:
)

if resolved:
defs_replace_recursively(defs, defs)
defs_replace_recursively(defs)
return defs
else:
return defs

@model_validator(mode="after")
def resolve_palette_values(self):
defs_replace_recursively(
self._color_defs(resolved=False),
self,
defs=self._color_defs(resolved=False),
name="color",
exclude="palette",
)
Expand Down
4 changes: 2 additions & 2 deletions pkg-py/src/brand_yaml/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def validate_images(
# We resolve `logo.images` on load or on replacement only
# TODO: Replace with class with getter/setters
# Retain original values, return resolved values, and re-validate on update.
defs_replace_recursively(value, value, name="images")
defs_replace_recursively(value, name="images")

return value

Expand All @@ -86,8 +86,8 @@ def resolve_image_values(self):
}
)
defs_replace_recursively(
full_defs,
self,
defs=full_defs,
name="logo",
exclude="images",
)
Expand Down

0 comments on commit 65f0384

Please sign in to comment.