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

Commit

Permalink
Fixes #324.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 23, 2023
1 parent b84b048 commit d609e3f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 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.0'
version = '1.32.1'

test {
useJUnitPlatform()
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.32.1 (unreleased)

- Fixes https://github.com/structurizr/dsl/issues/324 (Groups with no curly braces breaks diagrams).

## 1.32.0 (28th July 2023)

- Adds the ability to specify the workspace `visibility` (private/public) via the workspace configuration.
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/structurizr/dsl/GroupParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ class GroupParser {
private static final String GRAMMAR = "group <name> {";

private final static int NAME_INDEX = 1;
private final static int BRACE_INDEX = 2;

ElementGroup parse(GroupableDslContext dslContext, Tokens tokens) {
// group <name>
// group <name> {

if (tokens.hasMoreThan(NAME_INDEX)) {
if (tokens.hasMoreThan(BRACE_INDEX)) {
throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
}

if (!tokens.includes(NAME_INDEX)) {
if (!tokens.includes(BRACE_INDEX)) {
throw new RuntimeException("Expected: " + GRAMMAR);
}

if (!DslContext.CONTEXT_START_TOKEN.equalsIgnoreCase(tokens.get(BRACE_INDEX))) {
throw new RuntimeException("Expected: " + GRAMMAR);
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/structurizr/dsl/StructurizrDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,37 +355,37 @@ void parse(List<String> lines, File dslFile) throws StructurizrDslParserExceptio
registerIdentifier(identifier, component);

} else if (GROUP_TOKEN.equalsIgnoreCase(firstToken) && inContext(ModelDslContext.class)) {
ElementGroup group = new GroupParser().parse(getContext(ModelDslContext.class), tokens.withoutContextStartToken());
ElementGroup group = new GroupParser().parse(getContext(ModelDslContext.class), tokens);

startContext(new ModelDslContext(group));
registerIdentifier(identifier, group);
} else if (GROUP_TOKEN.equalsIgnoreCase(firstToken) && inContext(EnterpriseDslContext.class)) {
ElementGroup group = new GroupParser().parse(getContext(EnterpriseDslContext.class), tokens.withoutContextStartToken());
ElementGroup group = new GroupParser().parse(getContext(EnterpriseDslContext.class), tokens);

startContext(new EnterpriseDslContext(group));
registerIdentifier(identifier, group);
} else if (GROUP_TOKEN.equalsIgnoreCase(firstToken) && inContext(SoftwareSystemDslContext.class)) {
ElementGroup group = new GroupParser().parse(getContext(SoftwareSystemDslContext.class), tokens.withoutContextStartToken());
ElementGroup group = new GroupParser().parse(getContext(SoftwareSystemDslContext.class), tokens);

SoftwareSystem softwareSystem = getContext(SoftwareSystemDslContext.class).getSoftwareSystem();
group.setParent(softwareSystem);
startContext(new SoftwareSystemDslContext(softwareSystem, group));
registerIdentifier(identifier, group);
} else if (GROUP_TOKEN.equalsIgnoreCase(firstToken) && inContext(ContainerDslContext.class)) {
ElementGroup group = new GroupParser().parse(getContext(ContainerDslContext.class), tokens.withoutContextStartToken());
ElementGroup group = new GroupParser().parse(getContext(ContainerDslContext.class), tokens);

Container container = getContext(ContainerDslContext.class).getContainer();
group.setParent(container);
startContext(new ContainerDslContext(container, group));
registerIdentifier(identifier, group);
} else if (GROUP_TOKEN.equalsIgnoreCase(firstToken) && inContext(DeploymentEnvironmentDslContext.class)) {
ElementGroup group = new GroupParser().parse(getContext(DeploymentEnvironmentDslContext.class), tokens.withoutContextStartToken());
ElementGroup group = new GroupParser().parse(getContext(DeploymentEnvironmentDslContext.class), tokens);

String environment = getContext(DeploymentEnvironmentDslContext.class).getEnvironment();
startContext(new DeploymentEnvironmentDslContext(environment, group));
registerIdentifier(identifier, group);
} else if (GROUP_TOKEN.equalsIgnoreCase(firstToken) && inContext(DeploymentNodeDslContext.class)) {
ElementGroup group = new GroupParser().parse(getContext(DeploymentNodeDslContext.class), tokens.withoutContextStartToken());
ElementGroup group = new GroupParser().parse(getContext(DeploymentNodeDslContext.class), tokens);

DeploymentNode deploymentNode = getContext(DeploymentNodeDslContext.class).getDeploymentNode();
startContext(new DeploymentNodeDslContext(deploymentNode, group));
Expand Down
7 changes: 7 additions & 0 deletions src/test/dsl/group-without-brace.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
workspace {

model {
group "Name"
}

}
13 changes: 13 additions & 0 deletions src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1161,4 +1161,17 @@ void test_IncludeImpliedRelationship() throws Exception {
assertEquals(1, parser.getWorkspace().getViews().getSystemLandscapeViews().iterator().next().getRelationships().size());
}

@Test
void test_GroupWithoutBrace() throws Exception {
File dslFile = new File("src/test/dsl/group-without-brace.dsl");

try {
StructurizrDslParser parser = new StructurizrDslParser();
parser.parse(dslFile);
fail();
} catch (StructurizrDslParserException e) {
assertEquals("Expected: group <name> { at line 4 of " + dslFile.getAbsolutePath() + ": group \"Name\"", e.getMessage());
}
}

}
18 changes: 14 additions & 4 deletions src/test/java/com/structurizr/dsl/GroupParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GroupParserTests extends AbstractTests {
@Test
void parse_ThrowsAnException_WhenThereAreTooManyTokens() {
try {
parser.parse(null, tokens("group", "name", "extra"));
parser.parse(null, tokens("group", "name", "{", "extra"));
fail();
} catch (Exception e) {
assertEquals("Too many tokens, expected: group <name> {", e.getMessage());
Expand All @@ -28,9 +28,19 @@ void parse_ThrowsAnException_WhenTheNameIsMissing() {
}
}

@Test
void parse_ThrowsAnException_WhenTheBraceIsMissing() {
try {
parser.parse(null, tokens("group", "Name", "foo"));
fail();
} catch (Exception e) {
assertEquals("Expected: group <name> {", e.getMessage());
}
}

@Test
void parse() {
ElementGroup group = parser.parse(context(), tokens("group", "Group 1"));
ElementGroup group = parser.parse(context(), tokens("group", "Group 1", "{"));
assertEquals("Group 1", group.getName());
assertTrue(group.getElements().isEmpty());
}
Expand All @@ -41,7 +51,7 @@ void parse_NestedGroup_ThrowsAnExceptionWhenNestedGroupsAreNotConfigured() {
context.setWorkspace(workspace);

try {
parser.parse(context, tokens("group", "Group 2"));
parser.parse(context, tokens("group", "Group 2", "{"));
fail();
} catch (Exception e) {
assertEquals("To use nested groups, please define a model property named structurizr.groupSeparator", e.getMessage());
Expand All @@ -54,7 +64,7 @@ void parse_NestedGroup() {
ModelDslContext context = new ModelDslContext(new ElementGroup(workspace.getModel(), "Group 1"));
context.setWorkspace(workspace);

ElementGroup group = parser.parse(context, tokens("group", "Group 2"));
ElementGroup group = parser.parse(context, tokens("group", "Group 2", "{"));
assertEquals("Group 1/Group 2", group.getName());
assertTrue(group.getElements().isEmpty());
}
Expand Down

0 comments on commit d609e3f

Please sign in to comment.