From 579b61c06ff6fd0a5cbce8a86a50ad700a5314fa Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sun, 2 Apr 2023 09:35:52 +0100 Subject: [PATCH] Fixes https://github.com/structurizr/dsl/issues/241 (Allow styles defined in an extending workspace to override those in the base workspace). --- docs/changelog.md | 4 ++++ src/main/java/com/structurizr/dsl/ElementStyleParser.java | 7 ++++++- .../java/com/structurizr/dsl/RelationshipStyleParser.java | 7 ++++++- .../java/com/structurizr/dsl/ElementStyleParserTests.java | 6 ++++++ .../com/structurizr/dsl/RelationshipStyleParserTests.java | 6 ++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 9feaef4..587ae01 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,9 @@ # Changelog +## (unreleased) + +- Fixes https://github.com/structurizr/dsl/issues/241 (Allow styles defined in an extending workspace to override those in the base workspace). + ## 1.30.0 (31st March 2023) - Allows `deploymentEnvironment` to be used without starting a new context (i.e. without `{` and `}`) (see https://github.com/structurizr/cli/discussions/112). diff --git a/src/main/java/com/structurizr/dsl/ElementStyleParser.java b/src/main/java/com/structurizr/dsl/ElementStyleParser.java index cdea135..1769ec5 100644 --- a/src/main/java/com/structurizr/dsl/ElementStyleParser.java +++ b/src/main/java/com/structurizr/dsl/ElementStyleParser.java @@ -27,7 +27,12 @@ ElementStyle parseElementStyle(DslContext context, Tokens tokens) { } Workspace workspace = context.getWorkspace(); - return workspace.getViews().getConfiguration().getStyles().addElementStyle(tag); + ElementStyle elementStyle = workspace.getViews().getConfiguration().getStyles().getElementStyle(tag); + if (elementStyle == null) { + elementStyle = workspace.getViews().getConfiguration().getStyles().addElementStyle(tag); + } + + return elementStyle; } else { throw new RuntimeException("Expected: element {"); } diff --git a/src/main/java/com/structurizr/dsl/RelationshipStyleParser.java b/src/main/java/com/structurizr/dsl/RelationshipStyleParser.java index bfb295f..6c260c3 100644 --- a/src/main/java/com/structurizr/dsl/RelationshipStyleParser.java +++ b/src/main/java/com/structurizr/dsl/RelationshipStyleParser.java @@ -26,7 +26,12 @@ RelationshipStyle parseRelationshipStyle(DslContext context, Tokens tokens) { } Workspace workspace = context.getWorkspace(); - return workspace.getViews().getConfiguration().getStyles().addRelationshipStyle(tag); + RelationshipStyle relationshipStyle = workspace.getViews().getConfiguration().getStyles().getRelationshipStyle(tag); + if (relationshipStyle == null) { + relationshipStyle = workspace.getViews().getConfiguration().getStyles().addRelationshipStyle(tag); + } + + return relationshipStyle; } else { throw new RuntimeException("Expected: relationship {"); } diff --git a/src/test/java/com/structurizr/dsl/ElementStyleParserTests.java b/src/test/java/com/structurizr/dsl/ElementStyleParserTests.java index d5cf245..5216eb3 100644 --- a/src/test/java/com/structurizr/dsl/ElementStyleParserTests.java +++ b/src/test/java/com/structurizr/dsl/ElementStyleParserTests.java @@ -60,6 +60,12 @@ void test_parseElementStyle_CreatesAnElementStyle() { assertNotNull(style); } + @Test + void test_parseElementStyle_FindsAnExistingElementStyle() { + ElementStyle style = workspace.getViews().getConfiguration().getStyles().addElementStyle("Tag"); + assertSame(style, parser.parseElementStyle(context(), tokens("element", "Tag"))); + } + @Test void test_parseShape_ThrowsAnException_WhenThereAreTooManyTokens() { try { diff --git a/src/test/java/com/structurizr/dsl/RelationshipStyleParserTests.java b/src/test/java/com/structurizr/dsl/RelationshipStyleParserTests.java index 14ce429..8736c5a 100644 --- a/src/test/java/com/structurizr/dsl/RelationshipStyleParserTests.java +++ b/src/test/java/com/structurizr/dsl/RelationshipStyleParserTests.java @@ -59,6 +59,12 @@ void test_parseRelationshipStyle_CreatesAnRelationshipStyle() { assertNotNull(style); } + @Test + void test_parseRelationshipStyle_FindsAnExistingRelationshipStyle() { + RelationshipStyle style = workspace.getViews().getConfiguration().getStyles().addRelationshipStyle("Tag"); + assertSame(style, parser.parseRelationshipStyle(context(), tokens("relationship", "Tag"))); + } + @Test void test_parseThickness_ThrowsAnException_WhenThereAreTooManyTokens() { try {