Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 482cea5

Browse files
yt-msMidnighter
authored andcommitted
refactor: remove Model from hydration where possible
1 parent 4781632 commit 482cea5

14 files changed

+47
-77
lines changed

src/structurizr/mixin/model_ref_mixin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from weakref import ref
2121

2222

23-
if TYPE_CHECKING:
23+
if TYPE_CHECKING: # pragma: no cover
2424
from ..model import Model
2525

2626

@@ -40,6 +40,11 @@ def model(self) -> "Model":
4040
"""Return the referenced model."""
4141
return self.get_model()
4242

43+
@property
44+
def is_in_model(self) -> bool:
45+
"""Return whether a model has been set."""
46+
return self._model() is not None
47+
4348
def get_model(self) -> "Model":
4449
"""
4550
Retrieve the model instance from the reference.

src/structurizr/model/component.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
if TYPE_CHECKING: # pragma: no cover
2929
from .container import Container
30-
from .model import Model
3130

3231

3332
__all__ = ("Component", "ComponentIO")
@@ -109,9 +108,7 @@ def __init__(
109108
self.tags.add(Tags.COMPONENT)
110109

111110
@classmethod
112-
def hydrate(
113-
cls, component_io: ComponentIO, container: "Container", model: "Model"
114-
) -> "Component":
111+
def hydrate(cls, component_io: ComponentIO, container: "Container") -> "Component":
115112
"""Create and hydrate a new `Component` instance from its IO.
116113
117114
This will also automatically register with the model.
@@ -123,5 +120,4 @@ def hydrate(
123120
# TODO: code_elements=map(CodeElement.hydrate, component_io.components),
124121
size=component_io.size,
125122
)
126-
model += component
127123
return component

src/structurizr/model/container.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626

2727
if TYPE_CHECKING: # pragma: no cover
28-
from .model import Model
2928
from .software_system import SoftwareSystem
3029

3130

@@ -117,23 +116,16 @@ def hydrate(
117116
cls,
118117
container_io: ContainerIO,
119118
software_system: "SoftwareSystem",
120-
model: "Model",
121119
) -> "Container":
122-
"""Hydrate a new Container instance from its IO.
123-
124-
This will also automatically register with the model.
125-
"""
120+
"""Hydrate a new Container instance from its IO."""
126121
container = cls(
127122
**cls.hydrate_arguments(container_io),
128123
parent=software_system,
129124
technology=container_io.technology,
130125
)
131-
model += container
132126

133127
for component_io in container_io.components:
134-
component = Component.hydrate(
135-
component_io, container=container, model=model
136-
)
128+
component = Component.hydrate(component_io, container=container)
137129
container += component
138130

139131
return container
@@ -164,8 +156,9 @@ def __iadd__(self, component: Component) -> "Container":
164156
f"{component.parent}. Cannot add to {self}."
165157
)
166158
self._components.add(component)
167-
model = self.model
168-
model += component
159+
if self.is_in_model:
160+
model = self.model
161+
model += component
169162
return self
170163

171164
def get_component_with_name(self, name: str) -> Component:

src/structurizr/model/deployment_node.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ def _add_child_deployment_node(self, node: "DeploymentNode"):
280280
f"{node.parent}. Cannot add to {self}."
281281
)
282282
self._children.add(node)
283-
model = self.model
284-
model += node
283+
if self.is_in_model:
284+
model = self.model
285+
model += node
285286

286287
@classmethod
287288
def hydrate(
@@ -290,15 +291,11 @@ def hydrate(
290291
model: "Model",
291292
parent: "DeploymentNode" = None,
292293
) -> "DeploymentNode":
293-
"""Hydrate a new DeploymentNode instance from its IO.
294-
295-
This will also automatically register with the model.
296-
"""
294+
"""Hydrate a new DeploymentNode instance from its IO."""
297295
node = cls(
298296
**cls.hydrate_arguments(deployment_node_io),
299297
parent=parent,
300298
)
301-
model += node
302299

303300
for child_io in deployment_node_io.children:
304301
child_node = DeploymentNode.hydrate(child_io, model=model, parent=node)
@@ -315,9 +312,7 @@ def hydrate(
315312
node._software_system_instances.add(instance)
316313

317314
for infra_node_io in deployment_node_io.infrastructure_nodes:
318-
infra_node = InfrastructureNode.hydrate(
319-
infra_node_io, model=model, parent=node
320-
)
315+
infra_node = InfrastructureNode.hydrate(infra_node_io, parent=node)
321316
node._infrastructure_nodes.add(infra_node)
322317

323318
return node

src/structurizr/model/infrastructure_node.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
if TYPE_CHECKING: # pragma: no cover
2323
from .deployment_node import DeploymentNode
24-
from .model import Model
2524

2625

2726
__all__ = ("InfrastructureNode", "InfrastructureNodeIO")
@@ -69,17 +68,12 @@ def __init__(
6968
def hydrate(
7069
cls,
7170
node_io: InfrastructureNodeIO,
72-
model: "Model",
7371
parent: "DeploymentNode",
7472
) -> "InfrastructureNode":
75-
"""Hydrate a new InfrastructureNode instance from its IO.
76-
77-
This will also automatically register with the model.
78-
"""
73+
"""Hydrate a new InfrastructureNode instance from its IO."""
7974
node = cls(
8075
**cls.hydrate_arguments(node_io),
8176
technology=node_io.technology,
8277
parent=parent,
8378
)
84-
model += node
8579
return node

src/structurizr/model/model.py

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

155155
for person_io in model_io.people:
156-
Person.hydrate(person_io, model=model)
156+
model += Person.hydrate(person_io)
157157

158158
for software_system_io in model_io.software_systems:
159-
SoftwareSystem.hydrate(software_system_io, model=model)
159+
model += SoftwareSystem.hydrate(software_system_io)
160160

161161
for deployment_node_io in model_io.deployment_nodes:
162-
DeploymentNode.hydrate(deployment_node_io, model=model)
162+
model += DeploymentNode.hydrate(deployment_node_io, model=model)
163163

164164
for element in model.get_elements():
165165
for relationship in element.relationships:
@@ -368,6 +368,8 @@ def _add_element(self, element: Element) -> None:
368368
self._elements_by_id[element.id] = element
369369
element.set_model(self)
370370
self._id_generator.found(element.id)
371+
for child in element.child_elements:
372+
self += child
371373

372374
def _add_relationship(
373375
self, relationship: Relationship, create_implied_relationships: bool

src/structurizr/model/person.py

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

1818

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

2121
from pydantic import Field
2222

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

2828

29-
if TYPE_CHECKING: # pragma: no cover
30-
from .model import Model
31-
3229
__all__ = ("PersonIO", "Person")
3330

3431

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

6562
@classmethod
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-
"""
63+
def hydrate(cls, person_io: PersonIO) -> "Person":
64+
"""Create a new person and hydrate from its IO."""
7165
person = cls(
7266
**cls.hydrate_arguments(person_io),
7367
location=person_io.location,
7468
)
75-
model += person
7669
return person
7770

7871
def interacts_with(

src/structurizr/model/software_system.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""Provide a software system element model."""
1717

1818

19-
from typing import TYPE_CHECKING, Iterable, List, Set
19+
from typing import Iterable, List, Set
2020

2121
from pydantic import Field
2222

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

2828

29-
if TYPE_CHECKING: # pragma: no cover
30-
from .model import Model
31-
32-
3329
__all__ = ("SoftwareSystem", "SoftwareSystemIO")
3430

3531

@@ -114,33 +110,27 @@ def __iadd__(self, container: Container) -> "SoftwareSystem":
114110
f"{container.parent}. Cannot add to {self}."
115111
)
116112
self._containers.add(container)
117-
model = self.model
118-
model += container
113+
if self.is_in_model:
114+
model = self.model
115+
model += container
119116
return self
120117

121118
def get_container_with_name(self, name: str) -> Container:
122119
"""Return the container with the given name, or None."""
123120
return next((c for c in self._containers if c.name == name), None)
124121

125122
@classmethod
126-
def hydrate(
127-
cls, software_system_io: SoftwareSystemIO, model: "Model"
128-
) -> "SoftwareSystem":
129-
"""Create a new SoftwareSystem instance and hydrate it from its IO.
130-
131-
This will also automatically register with the model.
132-
"""
123+
def hydrate(cls, software_system_io: SoftwareSystemIO) -> "SoftwareSystem":
124+
"""Create a new SoftwareSystem instance and hydrate it from its IO."""
133125
software_system = cls(
134126
**cls.hydrate_arguments(software_system_io),
135127
location=software_system_io.location,
136128
)
137-
model += software_system
138129

139130
for container_io in software_system_io.containers:
140131
software_system += Container.hydrate(
141132
container_io,
142133
software_system=software_system,
143-
model=model,
144134
)
145135

146136
return software_system

src/structurizr/model/software_system_instance.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ def hydrate(
6767
model: "Model",
6868
parent: "DeploymentNode",
6969
) -> "SoftwareSystemInstance":
70-
"""Hydrate a new SoftwareSystemInstance instance from its IO.
71-
72-
This will also automatically register with the model.
73-
"""
70+
"""Hydrate a new SoftwareSystemInstance instance from its IO."""
7471
system = model.get_element(system_instance_io.software_system_id)
7572
instance = cls(
7673
**cls.hydrate_arguments(system_instance_io),
7774
software_system=system,
7875
parent=parent,
7976
)
80-
model += instance
8177
return instance

tests/unit/model/test_deployment_node.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def test_deployment_node_serialization_of_recursive_nodes(model_with_node):
128128
new_child = new_top_node.children[0]
129129
assert new_child.name == "child"
130130
assert new_child.parent is new_top_node
131-
assert new_child.model is model_with_node
132131

133132

134133
def test_deployment_node_add_container(model_with_node):
@@ -190,7 +189,6 @@ def test_deployment_node_serialising_container(model_with_node):
190189
instance = node2.container_instances[0]
191190
assert instance.instance_id == 1
192191
assert instance.container is container
193-
assert instance.model is model_with_node
194192
assert instance.parent is node2
195193

196194

@@ -226,7 +224,6 @@ def test_deployment_node_serialising_software_system(model_with_node):
226224
instance = node2.software_system_instances[0]
227225
assert instance.instance_id == 1
228226
assert instance.software_system is system
229-
assert instance.model is model_with_node
230227
assert instance.parent is node2
231228

232229

@@ -258,5 +255,4 @@ def test_deployment_node_serialising_infrastructure_nodes(model_with_node):
258255
assert len(node2.infrastructure_nodes) == 1
259256
infra_node = node2.infrastructure_nodes[0]
260257
assert infra_node.name == "infraNode"
261-
assert infra_node.model is model_with_node
262258
assert infra_node.parent is node2

0 commit comments

Comments
 (0)