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

Commit

Permalink
Adds the ability to specify the workspace scope via a scope keyword…
Browse files Browse the repository at this point in the history
… inside the workspace `configuration`.
  • Loading branch information
simonbrowndotje committed Nov 19, 2023
1 parent 7edf472 commit 680e616
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include <directory> on Windows).
- Adds a `getDslParser()` method to the `StructurizrDslPluginContext` class (https://github.com/structurizr/dsl/issues/361).
- Adds the ability to specify the workspace scope via a `scope` keyword inside the workspace `configuration`.
- Updates structurizr/java to [v1.28.0](https://github.com/structurizr/java/releases/tag/v1.28.0).

## 1.33.0 (27th October 2023)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

final class ConfigurationDslContext extends DslContext {

private static final int FIRST_PROPERTY_INDEX = 1;

private static final String PRIVATE = "private";
private static final String PUBLIC = "public";

@Override
protected String[] getPermittedTokens() {
return new String[] {
StructurizrDslTokens.SCOPE_TOKEN,
StructurizrDslTokens.VISIBILITY_TOKEN,
StructurizrDslTokens.USERS_TOKEN,
StructurizrDslTokens.PROPERTIES_TOKEN
Expand Down
41 changes: 34 additions & 7 deletions src/main/java/com/structurizr/dsl/ConfigurationParser.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
package com.structurizr.dsl;

import com.structurizr.configuration.Visibility;
import com.structurizr.configuration.WorkspaceScope;

final class ConfigurationParser extends AbstractParser {

private static final String GRAMMAR = "visibility <private|public>";
private static final String SCOPE_GRAMMAR = "scope <landscape|softwaresystem|none>";
private static final String SCOPE_LANDSCAPE = "landscape";
private static final String SCOPE_SOFTWARE_SYSTEM = "softwaresystem";
private static final String SCOPE_NONE = "none";

private static final String VISIBILITY_GRAMMAR = "visibility <private|public>";
private static final String VISIBILITY_PRIVATE = "private";
private static final String VISIBILITY_PUBLIC = "public";

private static final int FIRST_PROPERTY_INDEX = 1;

private static final String PRIVATE = "private";
private static final String PUBLIC = "public";
void parseScope(DslContext context, Tokens tokens) {
if (tokens.hasMoreThan(FIRST_PROPERTY_INDEX)) {
throw new RuntimeException("Too many tokens, expected: " + SCOPE_GRAMMAR);
}

if (tokens.includes(FIRST_PROPERTY_INDEX)) {
String scope = tokens.get(1).toLowerCase();

if (scope.equalsIgnoreCase(SCOPE_LANDSCAPE)) {
context.getWorkspace().getConfiguration().setScope(WorkspaceScope.Landscape);
} else if (scope.equalsIgnoreCase(SCOPE_SOFTWARE_SYSTEM)) {
context.getWorkspace().getConfiguration().setScope(WorkspaceScope.SoftwareSystem);
} else if (scope.equalsIgnoreCase(SCOPE_NONE)) {
context.getWorkspace().getConfiguration().setScope(null);
} else {
throw new RuntimeException("The scope \"" + scope + "\" is not valid");
}
} else {
throw new RuntimeException("Expected: " + SCOPE_GRAMMAR);
}
}

void parseVisibility(DslContext context, Tokens tokens) {
if (tokens.hasMoreThan(FIRST_PROPERTY_INDEX)) {
throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
throw new RuntimeException("Too many tokens, expected: " + VISIBILITY_GRAMMAR);
}

if (tokens.includes(FIRST_PROPERTY_INDEX)) {
String visibility = tokens.get(1).toLowerCase();

if (visibility.equalsIgnoreCase(PRIVATE)) {
if (visibility.equalsIgnoreCase(VISIBILITY_PRIVATE)) {
context.getWorkspace().getConfiguration().setVisibility(Visibility.Private);
} else if (visibility.equalsIgnoreCase(PUBLIC)) {
} else if (visibility.equalsIgnoreCase(VISIBILITY_PUBLIC)) {
context.getWorkspace().getConfiguration().setVisibility(Visibility.Public);
} else {
throw new RuntimeException("The visibility \"" + visibility + "\" is not valid");
}
} else {
throw new RuntimeException("Expected: " + GRAMMAR);
throw new RuntimeException("Expected: " + VISIBILITY_GRAMMAR);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/structurizr/dsl/StructurizrDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ void parse(List<String> lines, File dslFile) throws StructurizrDslParserExceptio
} else if (CONFIGURATION_TOKEN.equalsIgnoreCase(firstToken) && inContext(WorkspaceDslContext.class)) {
startContext(new ConfigurationDslContext());

} else if (SCOPE_TOKEN.equalsIgnoreCase(firstToken) && inContext(ConfigurationDslContext.class)) {
new ConfigurationParser().parseScope(getContext(), tokens);

} else if (VISIBILITY_TOKEN.equalsIgnoreCase(firstToken) && inContext(ConfigurationDslContext.class)) {
new ConfigurationParser().parseVisibility(getContext(), tokens);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class StructurizrDslTokens {
static final String PERSPECTIVES_TOKEN = "perspectives";
static final String WORKSPACE_TOKEN = "workspace";
static final String EXTENDS_TOKEN = "extends";
static final String SCOPE_TOKEN = "scope";
static final String MODEL_TOKEN = "model";
static final String VIEWS_TOKEN = "views";
static final String ENTERPRISE_TOKEN = "enterprise";
Expand Down
1 change: 1 addition & 0 deletions src/test/dsl/test.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ workspace "Name" "Description" {
}

visibility public
scope softwaresystem
}

}
37 changes: 37 additions & 0 deletions src/test/java/com/structurizr/dsl/ConfigurationParserTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.structurizr.dsl;

import com.structurizr.configuration.Visibility;
import com.structurizr.configuration.WorkspaceScope;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,6 +11,42 @@ class ConfigurationParserTests extends AbstractTests {

private final ConfigurationParser parser = new ConfigurationParser();

@Test
void test_parseScope_ThrowsAnException_WhenThereAreTooManyTokens() {
try {
parser.parseScope(context(), tokens("scope", "landscape", "extra"));
fail();
} catch (Exception e) {
assertEquals("Too many tokens, expected: scope <landscape|softwaresystem|none>", e.getMessage());
}
}

@Test
void test_parseScope_ThrowsAnException_WhenTheScopeIsMissing() {
try {
parser.parseScope(context(), tokens("scope"));
fail();
} catch (Exception e) {
assertEquals("Expected: scope <landscape|softwaresystem|none>", e.getMessage());
}
}

@Test
void test_parseScope_ThrowsAnException_WhenTheScopeIsNotValid() {
try {
parser.parseScope(context(), tokens("scope", "container"));
fail();
} catch (Exception e) {
assertEquals("The scope \"container\" is not valid", e.getMessage());
}
}

@Test
void test_parseScope_SetsTheScope() {
parser.parseScope(context(), tokens("scope", "softwaresystem"));
assertEquals(WorkspaceScope.SoftwareSystem, workspace.getConfiguration().getScope());
}

@Test
void test_parseVisibility_ThrowsAnException_WhenThereAreTooManyTokens() {
try {
Expand Down

0 comments on commit 680e616

Please sign in to comment.