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

Commit

Permalink
Fixes #359.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Nov 19, 2023
1 parent d8ae7cf commit 2fcd0ee
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.34.0 (unreleased)

- Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include <directory> on Windows).
- Fixes https://github.com/structurizr/dsl/issues/359 (Add url for relationship in dynamic view).
- 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`.
- Adds support for specifying perspective values.
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/structurizr/dsl/DynamicViewContentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.structurizr.model.Relationship;
import com.structurizr.model.StaticStructureElement;
import com.structurizr.view.DynamicView;
import com.structurizr.view.RelationshipView;

final class DynamicViewContentParser extends AbstractParser {

Expand All @@ -19,7 +20,7 @@ final class DynamicViewContentParser extends AbstractParser {

private static final int RELATIONSHIP_IDENTIFIER_INDEX = 0;

void parseRelationship(DynamicViewDslContext context, Tokens tokens) {
RelationshipView parseRelationship(DynamicViewDslContext context, Tokens tokens) {
DynamicView view = context.getView();

if (tokens.size() > 1 && StructurizrDslTokens.RELATIONSHIP_TOKEN.equals(tokens.get(RELATIONSHIP_TOKEN_INDEX))) {
Expand Down Expand Up @@ -64,13 +65,13 @@ void parseRelationship(DynamicViewDslContext context, Tokens tokens) {
}

if (sourceElement instanceof StaticStructureElement && destinationElement instanceof StaticStructureElement) {
view.add((StaticStructureElement) sourceElement, description, technology, (StaticStructureElement) destinationElement);
return view.add((StaticStructureElement) sourceElement, description, technology, (StaticStructureElement) destinationElement);
} else if (sourceElement instanceof StaticStructureElement && destinationElement instanceof CustomElement) {
view.add((StaticStructureElement) sourceElement, description, technology, (CustomElement) destinationElement);
return view.add((StaticStructureElement) sourceElement, description, technology, (CustomElement) destinationElement);
} else if (sourceElement instanceof CustomElement && destinationElement instanceof StaticStructureElement) {
view.add((CustomElement) sourceElement, description, technology, (StaticStructureElement) destinationElement);
return view.add((CustomElement) sourceElement, description, technology, (StaticStructureElement) destinationElement);
} else if (sourceElement instanceof CustomElement && destinationElement instanceof CustomElement) {
view.add((CustomElement) sourceElement, description, technology, (CustomElement) destinationElement);
return view.add((CustomElement) sourceElement, description, technology, (CustomElement) destinationElement);
}
} else {
// <relationship identifier> [description] [technology]
Expand All @@ -90,8 +91,10 @@ void parseRelationship(DynamicViewDslContext context, Tokens tokens) {
description = tokens.get(RELATIONSHIP_IDENTIFIER_INDEX+1);
}

view.add(relationship, description);
return view.add(relationship, description);
}

throw new RuntimeException("The specified relationship could not be added");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.structurizr.dsl;

import com.structurizr.view.RelationshipView;

class DynamicViewRelationshipContext extends DslContext {

private final RelationshipView relationshipView;

DynamicViewRelationshipContext(RelationshipView relationshipView) {
super();

this.relationshipView = relationshipView;
}

RelationshipView getRelationshipView() {
return relationshipView;
}

@Override
protected String[] getPermittedTokens() {
return new String[] {
StructurizrDslTokens.URL_TOKEN
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.structurizr.dsl;

final class DynamicViewRelationshipParser extends AbstractParser {

private final static int URL_INDEX = 1;

void parseUrl(DynamicViewRelationshipContext context, Tokens tokens) {
// url <url>
if (tokens.hasMoreThan(URL_INDEX)) {
throw new RuntimeException("Too many tokens, expected: url <url>");
}

if (!tokens.includes(URL_INDEX)) {
throw new RuntimeException("Expected: url <url>");
}

String url = tokens.get(URL_INDEX);
context.getRelationshipView().setUrl(url);
}

}
9 changes: 8 additions & 1 deletion src/main/java/com/structurizr/dsl/StructurizrDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,19 @@ void parse(List<String> lines, File dslFile) throws StructurizrDslParserExceptio
new ImageViewContentParser(restricted).parseImage(getContext(ImageViewDslContext.class), dslFile, tokens);

} else if (inContext(DynamicViewDslContext.class)) {
new DynamicViewContentParser().parseRelationship(getContext(DynamicViewDslContext.class), tokens);
RelationshipView relationshipView = new DynamicViewContentParser().parseRelationship(getContext(DynamicViewDslContext.class), tokens);

if (inContext(DynamicViewParallelSequenceDslContext.class)) {
getContext(DynamicViewParallelSequenceDslContext.class).hasRelationships(true);
}

if (shouldStartContext(tokens)) {
startContext(new DynamicViewRelationshipContext(relationshipView));
}

} else if (URL_TOKEN.equalsIgnoreCase(firstToken) && inContext(DynamicViewRelationshipContext.class)) {
new DynamicViewRelationshipParser().parseUrl(getContext(DynamicViewRelationshipContext.class), tokens.withoutContextStartToken());

} else if (THEME_TOKEN.equalsIgnoreCase(firstToken) && (inContext(ViewsDslContext.class) || inContext(StylesDslContext.class))) {
new ThemeParser().parseTheme(getContext(), tokens);

Expand Down
4 changes: 4 additions & 0 deletions src/test/dsl/test.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ workspace "Name" "Description" {
description "Description"

user -> homePageController "Requests via web browser"
homePageController -> user {
url "https://structurizr.com"
}

autoLayout

properties {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.structurizr.dsl;

import com.structurizr.model.Relationship;
import com.structurizr.model.SoftwareSystem;
import com.structurizr.view.DynamicView;
import com.structurizr.view.RelationshipView;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

class DynamicViewRelationshipParserTests extends AbstractTests {

private final DynamicViewRelationshipParser parser = new DynamicViewRelationshipParser();

@Test
void test_parseUrl_ThrowsAnException_WhenThereAreTooManyTokens() {
try {
DynamicViewRelationshipContext context = new DynamicViewRelationshipContext(null);
parser.parseUrl(context, tokens("url", "url", "extra"));
fail();
} catch (Exception e) {
assertEquals("Too many tokens, expected: url <url>", e.getMessage());
}
}

@Test
void test_parseUrl_ThrowsAnException_WhenNoUrlIsSpecified() {
try {
DynamicViewRelationshipContext context = new DynamicViewRelationshipContext(null);
parser.parseUrl(context, tokens("url"));
fail();
} catch (Exception e) {
assertEquals("Expected: url <url>", e.getMessage());
}
}

@Test
void test_parseUrl_SetsTheUrl_WhenAUrlIsSpecified() {
SoftwareSystem a = model.addSoftwareSystem("A");
SoftwareSystem b = model.addSoftwareSystem("B");
Relationship r = a.uses(b, "Uses");

DynamicView dynamicView = workspace.getViews().createDynamicView("key", "Description");
RelationshipView rv = dynamicView.add(r);
DynamicViewRelationshipContext context = new DynamicViewRelationshipContext(rv);
parser.parseUrl(context, tokens("url", "http://example.com"));

assertEquals("http://example.com", rv.getUrl());
}

}

0 comments on commit 2fcd0ee

Please sign in to comment.