Skip to content

Commit

Permalink
Adds a way to configure whether the DSL source is retained via a work…
Browse files Browse the repository at this point in the history
…space property named `structurizr.dsl.source` - `true` (default) or `false`.
  • Loading branch information
simonbrowndotje committed Oct 6, 2024
1 parent f103a7a commit 34cd149
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- structurizr-dsl: Adds `supportingTypes implementation-suffix <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)

Expand Down
15 changes: 6 additions & 9 deletions structurizr-dsl/src/main/java/com/structurizr/dsl/DslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 "";
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
7 changes: 7 additions & 0 deletions structurizr-dsl/src/test/resources/dsl/source-child.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
workspace extends source-parent.dsl {

model {
b = softwareSystem "B"
}

}
11 changes: 11 additions & 0 deletions structurizr-dsl/src/test/resources/dsl/source-not-retained.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
workspace {

properties {
structurizr.dsl.source false
}

model {
a = softwareSystem "A"
}

}
7 changes: 7 additions & 0 deletions structurizr-dsl/src/test/resources/dsl/source-parent.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
workspace {

model {
a = softwareSystem "A"
}

}

0 comments on commit 34cd149

Please sign in to comment.