From 34cd1491e0acb52a8e23df62769b9923aa0acaad Mon Sep 17 00:00:00 2001 From: Simon Brown <1009874+simonbrowndotje@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:02:38 +0100 Subject: [PATCH] Adds a way to configure whether the DSL source is retained via a workspace property named `structurizr.dsl.source` - `true` (default) or `false`. --- changelog.md | 1 + .../java/com/structurizr/dsl/DslUtils.java | 15 +++----- .../structurizr/dsl/StructurizrDslParser.java | 5 ++- .../java/com/structurizr/dsl/DslTests.java | 38 +++++++++++++++++++ .../src/test/resources/dsl/source-child.dsl | 7 ++++ .../resources/dsl/source-not-retained.dsl | 11 ++++++ .../src/test/resources/dsl/source-parent.dsl | 7 ++++ 7 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 structurizr-dsl/src/test/resources/dsl/source-child.dsl create mode 100644 structurizr-dsl/src/test/resources/dsl/source-not-retained.dsl create mode 100644 structurizr-dsl/src/test/resources/dsl/source-parent.dsl diff --git a/changelog.md b/changelog.md index 8925cb7c..e08f54ed 100644 --- a/changelog.md +++ b/changelog.md @@ -9,6 +9,7 @@ - structurizr-dsl: Adds `supportingTypes implementation-suffix `. - structurizr-dsl: Fixes https://github.com/structurizr/java/issues/346 (`// comment \` joins lines). - structurizr-dsl: Anonymous identifiers for relationships (i.e. relationships not assigned to an identifier) are excluded from the model, and therefore also excluded from the serialised JSON. +- structurizr-dsl: Adds a way to configure whether the DSL source is retained via a workspace property named `structurizr.dsl.source` - `true` (default) or `false`. ## 3.0.0 (19th September 2024) diff --git a/structurizr-dsl/src/main/java/com/structurizr/dsl/DslUtils.java b/structurizr-dsl/src/main/java/com/structurizr/dsl/DslUtils.java index 16e73789..e20b936f 100644 --- a/structurizr-dsl/src/main/java/com/structurizr/dsl/DslUtils.java +++ b/structurizr-dsl/src/main/java/com/structurizr/dsl/DslUtils.java @@ -11,7 +11,8 @@ */ public class DslUtils { - private static final String STRUCTURIZR_DSL_PROPERTY_NAME = "structurizr.dsl"; + static final String STRUCTURIZR_DSL_PROPERTY_NAME = "structurizr.dsl"; + static final String STRUCTURIZR_DSL_RETAIN_SOURCE_PROPERTY_NAME = "structurizr.dsl.source"; /** * Gets the DSL associated with a workspace. @@ -21,13 +22,11 @@ public class DslUtils { */ public static String getDsl(Workspace workspace) { String base64 = workspace.getProperties().get(STRUCTURIZR_DSL_PROPERTY_NAME); - String dsl = ""; - if (!StringUtils.isNullOrEmpty(base64)) { - dsl = new String(Base64.getDecoder().decode(base64)); + return new String(Base64.getDecoder().decode(base64)); } - return dsl; + return ""; } /** @@ -37,12 +36,10 @@ public static String getDsl(Workspace workspace) { * @param dsl the DSL string */ public static void setDsl(Workspace workspace, String dsl) { - String base64 = ""; if (!StringUtils.isNullOrEmpty(dsl)) { - base64 = Base64.getEncoder().encodeToString(dsl.getBytes(StandardCharsets.UTF_8)); + String base64 = Base64.getEncoder().encodeToString(dsl.getBytes(StandardCharsets.UTF_8)); + workspace.addProperty(STRUCTURIZR_DSL_PROPERTY_NAME, base64); } - - workspace.addProperty(STRUCTURIZR_DSL_PROPERTY_NAME, base64); } } \ No newline at end of file diff --git a/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java index dc11d521..50f30b35 100644 --- a/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java +++ b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java @@ -101,7 +101,10 @@ public void setRestricted(boolean restricted) { */ public Workspace getWorkspace() { if (workspace != null) { - DslUtils.setDsl(workspace, getParsedDsl()); + String value = workspace.getProperties().get(DslUtils.STRUCTURIZR_DSL_RETAIN_SOURCE_PROPERTY_NAME); + if (value == null || value.equalsIgnoreCase("true")) { + DslUtils.setDsl(workspace, getParsedDsl()); + } } return workspace; diff --git a/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java b/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java index 62125ba9..0c73de74 100644 --- a/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java +++ b/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java @@ -1377,4 +1377,42 @@ void test_ImageView_WhenParserIsInRestrictedMode() { } } + @Test + void test_sourceIsRetained() throws Exception { + File parentDslFile = new File("src/test/resources/dsl/source-parent.dsl"); + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(parentDslFile); + Workspace workspace = parser.getWorkspace(); + assertEquals(""" +workspace { + + model { + a = softwareSystem "A" + } + +}""", DslUtils.getDsl(workspace)); + + File childDslFile = new File("src/test/resources/dsl/source-child.dsl"); + parser = new StructurizrDslParser(); + parser.parse(childDslFile); + workspace = parser.getWorkspace(); + assertEquals(""" +workspace extends source-parent.dsl { + + model { + b = softwareSystem "B" + } + +}""", DslUtils.getDsl(workspace)); + } + + @Test + void test_sourceIsNotRetained() throws Exception { + File parentDslFile = new File("src/test/resources/dsl/source-not-retained.dsl"); + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(parentDslFile); + Workspace workspace = parser.getWorkspace(); + assertNull(workspace.getProperties().get(DslUtils.STRUCTURIZR_DSL_PROPERTY_NAME)); + } + } \ No newline at end of file diff --git a/structurizr-dsl/src/test/resources/dsl/source-child.dsl b/structurizr-dsl/src/test/resources/dsl/source-child.dsl new file mode 100644 index 00000000..54ca1a6d --- /dev/null +++ b/structurizr-dsl/src/test/resources/dsl/source-child.dsl @@ -0,0 +1,7 @@ +workspace extends source-parent.dsl { + + model { + b = softwareSystem "B" + } + +} \ No newline at end of file diff --git a/structurizr-dsl/src/test/resources/dsl/source-not-retained.dsl b/structurizr-dsl/src/test/resources/dsl/source-not-retained.dsl new file mode 100644 index 00000000..a09c05b9 --- /dev/null +++ b/structurizr-dsl/src/test/resources/dsl/source-not-retained.dsl @@ -0,0 +1,11 @@ +workspace { + + properties { + structurizr.dsl.source false + } + + model { + a = softwareSystem "A" + } + +} \ No newline at end of file diff --git a/structurizr-dsl/src/test/resources/dsl/source-parent.dsl b/structurizr-dsl/src/test/resources/dsl/source-parent.dsl new file mode 100644 index 00000000..a19e355a --- /dev/null +++ b/structurizr-dsl/src/test/resources/dsl/source-parent.dsl @@ -0,0 +1,7 @@ +workspace { + + model { + a = softwareSystem "A" + } + +} \ No newline at end of file