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

Commit

Permalink
DSL identifiers (if present) will now be loaded when extending a JSON…
Browse files Browse the repository at this point in the history
… workspace (see #328).
  • Loading branch information
simonbrowndotje committed Aug 26, 2023
1 parent d609e3f commit 1af4ce1
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 212 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ targetCompatibility = 11

description = 'Structurizr DSL'
group = 'com.structurizr'
version = '1.32.1'
version = '1.33.0'

test {
useJUnitPlatform()
Expand Down
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## 1.32.1 (unreleased)
## 1.33.0 (unreleased)

- DSL identifiers (if present) will now be loaded when extending a JSON workspace (see https://github.com/structurizr/dsl/discussions/328).
- Fixes https://github.com/structurizr/dsl/issues/324 (Groups with no curly braces breaks diagrams).

## 1.32.0 (28th July 2023)
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/structurizr/dsl/WorkspaceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.structurizr.Workspace;
import com.structurizr.model.CreateImpliedRelationshipsUnlessAnyRelationshipExistsStrategy;
import com.structurizr.model.Element;
import com.structurizr.model.Relationship;
import com.structurizr.util.WorkspaceUtils;

import java.io.File;
Expand All @@ -11,6 +13,8 @@ final class WorkspaceParser extends AbstractParser {
private static final String GRAMMAR_STANDALONE = "workspace [name] [description]";
private static final String GRAMMAR_EXTENDS = "workspace extends <file|url>";

private static final String STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME = "structurizr.dsl.identifier";

private static final int FIRST_INDEX = 1;
private static final int SECOND_INDEX = 2;

Expand All @@ -37,6 +41,7 @@ Workspace parse(DslParserContext context, Tokens tokens) {
if (source.endsWith(".json")) {
String json = readFromUrl(source);
workspace = WorkspaceUtils.fromJson(json);
registerIdentifiers(workspace, context);
} else {
String dsl = readFromUrl(source);
StructurizrDslParser structurizrDslParser = new StructurizrDslParser();
Expand All @@ -60,6 +65,7 @@ Workspace parse(DslParserContext context, Tokens tokens) {

if (source.endsWith(".json")) {
workspace = WorkspaceUtils.loadWorkspaceFromJson(file);
registerIdentifiers(workspace, context);
} else {
StructurizrDslParser structurizrDslParser = new StructurizrDslParser();
structurizrDslParser.parse(context, file);
Expand All @@ -85,6 +91,32 @@ Workspace parse(DslParserContext context, Tokens tokens) {
return workspace;
}

private void registerIdentifiers(Workspace workspace, DslParserContext context) {
for (Element element : workspace.getModel().getElements()) {
if (element.getProperties().containsKey(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME)) {
String identifier = element.getProperties().get(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME);
try {
context.identifiersRegister.validateIdentifierName(identifier);
context.identifiersRegister.register(identifier, element);
} catch (Exception e) {
// ignore, don't register the identifier
}
}
}

for (Relationship relationship : workspace.getModel().getRelationships()) {
if (relationship.getProperties().containsKey(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME)) {
String identifier = relationship.getProperties().get(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME);
try {
context.identifiersRegister.validateIdentifierName(identifier);
context.identifiersRegister.register(identifier, relationship);
} catch (Exception e) {
// ignore, don't register the identifier
}
}
}
}

void parseName(DslContext context, Tokens tokens) {
// name <name>
if (tokens.hasMoreThan(FIRST_INDEX)) {
Expand Down
25 changes: 3 additions & 22 deletions src/test/dsl/extend/extend-workspace-from-dsl-file.dsl
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
workspace extends workspace.dsl {
name "A new name"
description "A new description"

model {
softwareSystem = softwareSystem "Software System" {
!ref softwareSystem1 {
webapp = container "Web Application"
}
}

views {
systemContext softwareSystem "SystemContext" "An example of a System Context diagram." {
include *
autoLayout
}

styles {
element "Software System" {
background #1168bd
color #ffffff
}
element "Person" {
shape person
background #08427b
color #ffffff
}
}
user -> softwareSystem1 "Uses"
}

}
25 changes: 3 additions & 22 deletions src/test/dsl/extend/extend-workspace-from-dsl-url.dsl
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
workspace extends https://raw.githubusercontent.com/structurizr/dsl/master/src/test/dsl/extend/workspace.dsl {
name "A new name"
description "A new description"

model {
softwareSystem = softwareSystem "Software System" {
!ref softwareSystem1 {
webapp = container "Web Application"
}
}

views {
systemContext softwareSystem "SystemContext" "An example of a System Context diagram." {
include *
autoLayout
}

styles {
element "Software System" {
background #1168bd
color #ffffff
}
element "Person" {
shape person
background #08427b
color #ffffff
}
}
user -> softwareSystem1 "Uses"
}

}
29 changes: 8 additions & 21 deletions src/test/dsl/extend/extend-workspace-from-json-file.dsl
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
workspace extends workspace.json {
name "A new name"
description "A new description"

model {
softwareSystem = softwareSystem "Software System" {
webapp = container "Web Application"
// !ref with DSL identifier
!ref softwareSystem1 {
webapp1 = container "Web Application 1"
}
}

views {
systemContext softwareSystem "SystemContext" "An example of a System Context diagram." {
include *
autoLayout
// !ref with canonical name
!ref "SoftwareSystem://Software System 1" {
webapp2 = container "Web Application 2"
}

styles {
element "Software System" {
background #1168bd
color #ffffff
}
element "Person" {
shape person
background #08427b
color #ffffff
}
}
user -> softwareSystem1 "Uses"
}

}
29 changes: 8 additions & 21 deletions src/test/dsl/extend/extend-workspace-from-json-url.dsl
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
workspace extends https://raw.githubusercontent.com/structurizr/dsl/master/src/test/dsl/extend/workspace.json {
name "A new name"
description "A new description"

model {
softwareSystem = softwareSystem "Software System" {
webapp = container "Web Application"
// !ref with DSL identifier
!ref softwareSystem1 {
webapp1 = container "Web Application 1"
}
}

views {
systemContext softwareSystem "SystemContext" "An example of a System Context diagram." {
include *
autoLayout
// !ref with canonical name
!ref "SoftwareSystem://Software System 1" {
webapp2 = container "Web Application 2"
}

styles {
element "Software System" {
background #1168bd
color #ffffff
}
element "Person" {
shape person
background #08427b
color #ffffff
}
}
user -> softwareSystem1 "Uses"
}

}
9 changes: 4 additions & 5 deletions src/test/dsl/extend/workspace.dsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
workspace "Getting Started" "This is a model of my software system." {
workspace {

model {
user = person "User" "A user of my software system."
softwareSystem = softwareSystem "Software System" "My software system."

user -> softwareSystem "Uses"
user = person "User"
softwareSystem1 = softwareSystem "Software System 1"
softwareSystem "Software System 2"
}

}
36 changes: 23 additions & 13 deletions src/test/dsl/extend/workspace.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
{
"id" : 0,
"name" : "Getting Started",
"description" : "This is a model of my software system.",
"name" : "Name",
"description" : "Description",
"properties" : {
"structurizr.dsl" : "d29ya3NwYWNlIHsKCiAgICBtb2RlbCB7CiAgICAgICAgdXNlciA9IHBlcnNvbiAiVXNlciIKICAgICAgICBzb2Z0d2FyZVN5c3RlbTEgPSBzb2Z0d2FyZVN5c3RlbSAiU29mdHdhcmUgU3lzdGVtIDEiCiAgICAgICAgc29mdHdhcmVTeXN0ZW0gIlNvZnR3YXJlIFN5c3RlbSAyIgogICAgfQoKfQo="
},
"configuration" : { },
"model" : {
"people" : [ {
"id" : "1",
"tags" : "Element,Person",
"properties" : {
"structurizr.dsl.identifier" : "user"
},
"name" : "User",
"description" : "A user of my software system.",
"relationships" : [ {
"id" : "3",
"tags" : "Relationship",
"sourceId" : "1",
"destinationId" : "2",
"description" : "Uses"
} ],
"location" : "Unspecified"
} ],
"softwareSystems" : [ {
"id" : "2",
"tags" : "Element,Software System",
"name" : "Software System",
"description" : "My software system.",
"location" : "Unspecified"
"properties" : {
"structurizr.dsl.identifier" : "softwaresystem1"
},
"name" : "Software System 1",
"location" : "Unspecified",
"documentation" : { }
}, {
"id" : "3",
"tags" : "Element,Software System",
"properties" : {
"structurizr.dsl.identifier" : "9c50bae2-474c-4fb6-9cc0-ef1da359673b"
},
"name" : "Software System 2",
"location" : "Unspecified",
"documentation" : { }
} ]
},
"documentation" : { },
Expand Down
Loading

0 comments on commit 1af4ce1

Please sign in to comment.