Skip to content

Commit 128b62e

Browse files
yt-msMidnighter
authored andcommitted
refactor: protect SoftareSystem.containers from external modification.
1 parent a917ea9 commit 128b62e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/structurizr/model/software_system.py

Lines changed: 10 additions & 5 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, List, Set
19+
from typing import TYPE_CHECKING, Iterable, List, Set
2020

2121
from pydantic import Field
2222

@@ -66,14 +66,19 @@ def __init__(self, *, location: Location = Location.Unspecified, **kwargs) -> No
6666
"""Initialise a new SoftwareSystem."""
6767
super().__init__(**kwargs)
6868
self.location = location
69-
self.containers: Set[Container] = set()
69+
self._containers: Set[Container] = set()
7070

7171
# TODO: canonical_name
7272
# TODO: parent
7373

7474
self.tags.add(Tags.ELEMENT)
7575
self.tags.add(Tags.SOFTWARE_SYSTEM)
7676

77+
@property
78+
def containers(self) -> Iterable[Container]:
79+
"""Return read-only list of child containers."""
80+
return list(self._containers)
81+
7782
def add_container(
7883
self, name: str, description: str, technology: str = "", **kwargs
7984
) -> Container:
@@ -88,7 +93,7 @@ def __iadd__(self, container: Container) -> "SoftwareSystem":
8893
"""Add a new container to this system and register with its model."""
8994
# TODO: once we move past python 3.6 change to proper return type via
9095
# __future__.annotations
91-
if container in self.containers:
96+
if container in self._containers:
9297
return self
9398

9499
if self.get_container_with_name(container.name):
@@ -103,13 +108,13 @@ def __iadd__(self, container: Container) -> "SoftwareSystem":
103108
f"Container with name {container.name} already has parent "
104109
f"{container.parent}. Cannot add to {self}."
105110
)
106-
self.containers.add(container)
111+
self._containers.add(container)
107112
self.model.add(container)
108113
return self
109114

110115
def get_container_with_name(self, name: str) -> Container:
111116
"""Return the container with the given name, or None."""
112-
return next((c for c in self.containers if c.name == name), None)
117+
return next((c for c in self._containers if c.name == name), None)
113118

114119
@classmethod
115120
def hydrate(

0 commit comments

Comments
 (0)