Skip to content

Commit 67e79ed

Browse files
yt-msMidnighter
authored andcommitted
refactor: make hydration consistent about adding to the model
1 parent 89556e9 commit 67e79ed

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

src/structurizr/model/component.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
if TYPE_CHECKING:
2929
from .container import Container
30+
from .model import Model
3031

3132

3233
__all__ = ("Component", "ComponentIO")
@@ -108,12 +109,19 @@ def __init__(
108109
self.tags.add(Tags.COMPONENT)
109110

110111
@classmethod
111-
def hydrate(cls, component_io: ComponentIO, container: "Container") -> "Component":
112-
"""Create and hydrate a new `Component` instance from its IO."""
113-
return cls(
112+
def hydrate(
113+
cls, component_io: ComponentIO, container: "Container", model: "Model"
114+
) -> "Component":
115+
"""Create and hydrate a new `Component` instance from its IO.
116+
117+
This will also automatically register with the model.
118+
"""
119+
component = cls(
114120
**cls.hydrate_arguments(component_io),
115121
parent=container,
116122
technology=component_io.technology,
117123
# TODO: code_elements=map(CodeElement.hydrate, component_io.components),
118124
size=component_io.size,
119125
)
126+
model += component
127+
return component

src/structurizr/model/container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def hydrate(
126126
model += container
127127

128128
for component_io in container_io.components:
129-
component = Component.hydrate(component_io, container=container)
129+
component = Component.hydrate(
130+
component_io, container=container, model=model
131+
)
130132
container += component
131133

132134
return container

src/structurizr/model/model.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,13 @@ def hydrate(cls, model_io: ModelIO) -> "Model":
149149
)
150150

151151
for person_io in model_io.people:
152-
model += Person.hydrate(person_io)
152+
Person.hydrate(person_io, model=model)
153153

154154
for software_system_io in model_io.software_systems:
155-
model += SoftwareSystem.hydrate(software_system_io, model=model)
155+
SoftwareSystem.hydrate(software_system_io, model=model)
156156

157157
for deployment_node_io in model_io.deployment_nodes:
158-
DeploymentNode.hydrate(
159-
deployment_node_io, model=model
160-
) # Auto-registers with the model
158+
DeploymentNode.hydrate(deployment_node_io, model=model)
161159

162160
for element in model.get_elements():
163161
for relationship in element.relationships:

src/structurizr/model/person.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""Provide a person model."""
1717

1818

19-
from typing import Optional
19+
from typing import TYPE_CHECKING, Optional
2020

2121
from pydantic import Field
2222

@@ -26,6 +26,9 @@
2626
from .tags import Tags
2727

2828

29+
if TYPE_CHECKING:
30+
from .model import Model
31+
2932
__all__ = ("PersonIO", "Person")
3033

3134

@@ -60,12 +63,17 @@ def __init__(self, *, location: Location = Location.Unspecified, **kwargs) -> No
6063
self.tags.add(Tags.PERSON)
6164

6265
@classmethod
63-
def hydrate(cls, person_io: PersonIO) -> "Person":
64-
"""Create a new person and hydrate from its IO."""
65-
return cls(
66+
def hydrate(cls, person_io: PersonIO, model: "Model") -> "Person":
67+
"""Create a new person and hydrate from its IO.
68+
69+
This will also automatically register with the model.
70+
"""
71+
person = cls(
6672
**cls.hydrate_arguments(person_io),
6773
location=person_io.location,
6874
)
75+
model += person
76+
return person
6977

7078
def interacts_with(
7179
self, destination: "Person", description: str, **kwargs

src/structurizr/model/software_system.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ def get_container_with_name(self, name: str) -> Container:
121121
def hydrate(
122122
cls, software_system_io: SoftwareSystemIO, model: "Model"
123123
) -> "SoftwareSystem":
124-
"""Create a new SoftwareSystem instance and hydrate it from its IO."""
124+
"""Create a new SoftwareSystem instance and hydrate it from its IO.
125+
126+
This will also automatically register with the model.
127+
"""
125128
software_system = cls(
126129
**cls.hydrate_arguments(software_system_io),
127130
location=software_system_io.location,
128131
)
132+
model += software_system
129133

130134
for container_io in software_system_io.containers:
131135
Container.hydrate(

tests/unit/model/test_model_item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ def test_tag_order_is_preserved_to_and_from_io(empty_model: Model):
7676
element_io = SoftwareSystemIO.from_orm(element)
7777
assert element_io.tags == ["Element", "Software System", "tag3", "tag2", "tag1"]
7878
assert element_io.dict()["tags"] == "Element,Software System,tag3,tag2,tag1"
79-
element2 = SoftwareSystem.hydrate(element_io, empty_model)
79+
element2 = SoftwareSystem.hydrate(element_io, Model())
8080
assert list(element2.tags) == ["Element", "Software System", "tag3", "tag2", "tag1"]

0 commit comments

Comments
 (0)