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

Commit

Permalink
refactor: better handling of tags in FilteredView
Browse files Browse the repository at this point in the history
  • Loading branch information
yt-ms authored and Midnighter committed Jun 10, 2021
1 parent e859849 commit aa24ffb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/structurizr/view/filtered_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"""Provide a filtered view."""

from enum import Enum
from typing import Iterable, List, Optional
from typing import Iterable, List, Optional, Union

from pydantic import Field
from ordered_set import OrderedSet
from pydantic import Field, validator

from .abstract_view import AbstractView, AbstractViewIO
from .static_view import StaticView
Expand All @@ -42,12 +43,22 @@ class FilteredViewIO(AbstractViewIO):
upon the set of tag
tags: The set of tags to include/exclude elements/relationships when rendering
this filtered view.
Note that unlike Model Items, when filtered view tags are serialised to JSON then
they are serialised as an array rather than comma-separated.
"""

base_view_key: str = Field(alias="baseViewKey")
mode: FilterMode
tags: List[str]

@validator("tags", pre=True)
def split_tags(cls, tags: Union[str, Iterable[str]]) -> List[str]:
"""Convert comma-separated tag list into list if needed."""
if isinstance(tags, str):
return tags.split(",")
return list(tags)


class FilteredView(AbstractView):
"""
Expand Down Expand Up @@ -78,7 +89,7 @@ def __init__(
self._base_view_key = base_view_key
self.view = view
self.mode = mode
self.tags = set(tags)
self.tags = OrderedSet(tags)

@property
def base_view_key(self) -> str:
Expand Down
4 changes: 3 additions & 1 deletion src/structurizr/view/view_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def hydrate(cls, views: ViewSetIO, model: "Model") -> "ViewSet":
cls._hydrate_view(view, model=model)
dynamic_views.append(view)

filtered_views = [FilteredView.hydrate(view_io) for view_io in views.filtered_views]
filtered_views = [
FilteredView.hydrate(view_io) for view_io in views.filtered_views
]

result = cls(
model=model,
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/view/test_filtered_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ def test_serialisation():
assert view2.description == "test"
assert view2.mode == FilterMode.Exclude
assert view2.tags == {"v1"}


def test_tags_are_serialised_as_an_array():
"""Ensure that tags are serialised as an array, not comma-separated."""
container_view = ContainerView(key="static_key", description="container")
filtered_view = FilteredView(
key="filter1",
view=container_view,
description="test",
mode=FilterMode.Exclude,
tags=["v1", "test"],
)
io = FilteredViewIO.from_orm(filtered_view).json()
assert '"tags": ["v1", "test"]' in io

0 comments on commit aa24ffb

Please sign in to comment.