Skip to content

Commit c6fb645

Browse files
Improves archetypes support - defaults can be overridden, adds support for relationships.
1 parent 677d5e1 commit c6fb645

32 files changed

+354
-215
lines changed

structurizr-core/src/main/java/com/structurizr/model/Relationship.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void setDescription(String description) {
113113
}
114114

115115
/**
116-
* Gets the technology associated with this relationship (e.g. HTTPS, JDBC, etc).
116+
* Gets the technology associated with this relationship (e.g. HTTPS, etc).
117117
*
118118
* @return the technology as a String,
119119
* or null if a technology is not specified

structurizr-dsl/src/main/java/com/structurizr/dsl/Archetype.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.structurizr.dsl;
22

33
import com.structurizr.util.StringUtils;
4-
import com.structurizr.util.TagUtils;
54

65
import java.util.LinkedHashSet;
76
import java.util.Set;
@@ -10,8 +9,8 @@ final class Archetype {
109

1110
private final String name;
1211
private final String type;
13-
private String description;
14-
private String technology;
12+
private String description = "";
13+
private String technology = "";
1514
private final Set<String> tags = new LinkedHashSet<>();
1615

1716
Archetype(String name, String type) {

structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentArchetypeDslContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.structurizr.dsl;
22

3-
final class ComponentArchetypeDslContext extends ArchetypeDslContext {
3+
final class ComponentArchetypeDslContext extends ElementArchetypeDslContext {
44

55
ComponentArchetypeDslContext(Archetype archetype) {
66
super(archetype);

structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentParser.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class ComponentParser extends AbstractParser {
1212
private final static int TECHNOLOGY_INDEX = 3;
1313
private final static int TAGS_INDEX = 4;
1414

15-
Component parse(ContainerDslContext context, Tokens tokens) {
15+
Component parse(ContainerDslContext context, Tokens tokens, Archetype archetype) {
1616
// component <name> [description] [technology] [tags]
1717

1818
if (tokens.hasMoreThan(TAGS_INDEX)) {
@@ -35,20 +35,23 @@ Component parse(ContainerDslContext context, Tokens tokens) {
3535
component = container.addComponent(name);
3636
}
3737

38+
String description = archetype.getDescription();
3839
if (tokens.includes(DESCRIPTION_INDEX)) {
39-
String description = tokens.get(DESCRIPTION_INDEX);
40-
component.setDescription(description);
40+
description = tokens.get(DESCRIPTION_INDEX);
4141
}
42+
component.setDescription(description);
4243

44+
String technology = archetype.getTechnology();
4345
if (tokens.includes(TECHNOLOGY_INDEX)) {
44-
String technology = tokens.get(TECHNOLOGY_INDEX);
45-
component.setTechnology(technology);
46+
technology = tokens.get(TECHNOLOGY_INDEX);
4647
}
48+
component.setTechnology(technology);
4749

50+
String[] tags = archetype.getTags().toArray(new String[0]);
4851
if (tokens.includes(TAGS_INDEX)) {
49-
String tags = tokens.get(TAGS_INDEX);
50-
component.addTags(tags.split(","));
52+
tags = tokens.get(TAGS_INDEX).split(",");
5153
}
54+
component.addTags(tags);
5255

5356
if (context.hasGroup()) {
5457
component.setGroup(context.getGroup().getName());

structurizr-dsl/src/main/java/com/structurizr/dsl/ContainerArchetypeDslContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.structurizr.dsl;
22

3-
final class ContainerArchetypeDslContext extends ArchetypeDslContext {
3+
final class ContainerArchetypeDslContext extends ElementArchetypeDslContext {
44

55
ContainerArchetypeDslContext(Archetype archetype) {
66
super(archetype);

structurizr-dsl/src/main/java/com/structurizr/dsl/ContainerParser.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class ContainerParser extends AbstractParser {
1212
private final static int TECHNOLOGY_INDEX = 3;
1313
private final static int TAGS_INDEX = 4;
1414

15-
Container parse(SoftwareSystemDslContext context, Tokens tokens) {
15+
Container parse(SoftwareSystemDslContext context, Tokens tokens, Archetype archetype) {
1616
// container <name> [description] [technology] [tags]
1717

1818
if (tokens.hasMoreThan(TAGS_INDEX)) {
@@ -35,20 +35,23 @@ Container parse(SoftwareSystemDslContext context, Tokens tokens) {
3535
container = softwareSystem.addContainer(name);
3636
}
3737

38+
String description = archetype.getDescription();
3839
if (tokens.includes(DESCRIPTION_INDEX)) {
39-
String description = tokens.get(DESCRIPTION_INDEX);
40-
container.setDescription(description);
40+
description = tokens.get(DESCRIPTION_INDEX);
4141
}
42+
container.setDescription(description);
4243

44+
String technology = archetype.getTechnology();
4345
if (tokens.includes(TECHNOLOGY_INDEX)) {
44-
String technology = tokens.get(TECHNOLOGY_INDEX);
45-
container.setTechnology(technology);
46+
technology = tokens.get(TECHNOLOGY_INDEX);
4647
}
48+
container.setTechnology(technology);
4749

50+
String[] tags = archetype.getTags().toArray(new String[0]);
4851
if (tokens.includes(TAGS_INDEX)) {
49-
String tags = tokens.get(TAGS_INDEX);
50-
container.addTags(tags.split(","));
52+
tags = tokens.get(TAGS_INDEX).split(",");
5153
}
54+
container.addTags(tags);
5255

5356
if (context.hasGroup()) {
5457
container.setGroup(context.getGroup().getName());

structurizr-dsl/src/main/java/com/structurizr/dsl/DeploymentNodeArchetypeDslContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.structurizr.dsl;
22

3-
final class DeploymentNodeArchetypeDslContext extends ArchetypeDslContext {
3+
final class DeploymentNodeArchetypeDslContext extends ElementArchetypeDslContext {
44

55
DeploymentNodeArchetypeDslContext(Archetype archetype) {
66
super(archetype);

structurizr-dsl/src/main/java/com/structurizr/dsl/DeploymentNodeParser.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class DeploymentNodeParser extends AbstractParser {
1212
private static final int TAGS_INDEX = 4;
1313
private static final int INSTANCES_INDEX = 5;
1414

15-
DeploymentNode parse(DeploymentEnvironmentDslContext context, Tokens tokens) {
15+
DeploymentNode parse(DeploymentEnvironmentDslContext context, Tokens tokens, Archetype archetype) {
1616
// deploymentNode <name> [description] [technology] [tags] [instances]
1717

1818
if (tokens.hasMoreThan(INSTANCES_INDEX)) {
@@ -26,23 +26,23 @@ DeploymentNode parse(DeploymentEnvironmentDslContext context, Tokens tokens) {
2626
DeploymentNode deploymentNode = null;
2727
String name = tokens.get(NAME_INDEX);
2828

29-
String description = "";
29+
String description = archetype.getDescription();
3030
if (tokens.includes(DESCRIPTION_INDEX)) {
3131
description = tokens.get(DESCRIPTION_INDEX);
3232
}
3333

34-
String technology = "";
34+
String technology = archetype.getTechnology();
3535
if (tokens.includes(TECHNOLOGY_INDEX)) {
3636
technology = tokens.get(TECHNOLOGY_INDEX);
3737
}
3838

3939
deploymentNode = context.getWorkspace().getModel().addDeploymentNode(context.getEnvironment(), name, description, technology);
4040

41-
String tags = "";
41+
String[] tags = archetype.getTags().toArray(new String[0]);
4242
if (tokens.includes(TAGS_INDEX)) {
43-
tags = tokens.get(TAGS_INDEX);
44-
deploymentNode.addTags(tags.split(","));
43+
tags = tokens.get(TAGS_INDEX).split(",");
4544
}
45+
deploymentNode.addTags(tags);
4646

4747
String instances = "1";
4848
if (tokens.includes(INSTANCES_INDEX)) {
@@ -58,7 +58,7 @@ DeploymentNode parse(DeploymentEnvironmentDslContext context, Tokens tokens) {
5858
return deploymentNode;
5959
}
6060

61-
DeploymentNode parse(DeploymentNodeDslContext context, Tokens tokens) {
61+
DeploymentNode parse(DeploymentNodeDslContext context, Tokens tokens, Archetype archetype) {
6262
// deploymentNode <name> [description] [technology] [tags] [instances]
6363

6464
if (tokens.hasMoreThan(INSTANCES_INDEX)) {
@@ -72,24 +72,24 @@ DeploymentNode parse(DeploymentNodeDslContext context, Tokens tokens) {
7272
DeploymentNode deploymentNode = null;
7373
String name = tokens.get(NAME_INDEX);
7474

75-
String description = "";
75+
String description = archetype.getDescription();
7676
if (tokens.includes(DESCRIPTION_INDEX)) {
7777
description = tokens.get(DESCRIPTION_INDEX);
7878
}
7979

80-
String technology = "";
80+
String technology = archetype.getTechnology();
8181
if (tokens.includes(TECHNOLOGY_INDEX)) {
8282
technology = tokens.get(TECHNOLOGY_INDEX);
8383
}
8484

8585
DeploymentNode parent = context.getDeploymentNode();
8686
deploymentNode = parent.addDeploymentNode(name, description, technology);
8787

88-
String tags = "";
88+
String[] tags = archetype.getTags().toArray(new String[0]);
8989
if (tokens.includes(TAGS_INDEX)) {
90-
tags = tokens.get(TAGS_INDEX);
91-
deploymentNode.addTags(tags.split(","));
90+
tags = tokens.get(TAGS_INDEX).split(",");
9291
}
92+
deploymentNode.addTags(tags);
9393

9494
String instances = "1";
9595
if (tokens.includes(INSTANCES_INDEX)) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.structurizr.dsl;
2+
3+
abstract class ElementArchetypeDslContext extends ArchetypeDslContext {
4+
5+
ElementArchetypeDslContext(Archetype archetype) {
6+
super(archetype);
7+
}
8+
9+
}

structurizr-dsl/src/main/java/com/structurizr/dsl/ExplicitRelationshipParser.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ExplicitRelationshipParser extends AbstractRelationshipParser {
1717
private final static int TECHNOLOGY_INDEX = 4;
1818
private final static int TAGS_INDEX = 5;
1919

20-
Relationship parse(DslContext context, Tokens tokens) {
20+
Relationship parse(DslContext context, Tokens tokens, Archetype archetype) {
2121
// <identifier> -> <identifier> [description] [technology] [tags]
2222

2323
if (tokens.hasMoreThan(TAGS_INDEX)) {
@@ -40,25 +40,25 @@ Relationship parse(DslContext context, Tokens tokens) {
4040
throw new RuntimeException("The destination element \"" + destinationId + "\" does not exist");
4141
}
4242

43-
String description = "";
43+
String description = archetype.getDescription();
4444
if (tokens.includes(DESCRIPTION_INDEX)) {
4545
description = tokens.get(DESCRIPTION_INDEX);
4646
}
4747

48-
String technology = "";
48+
String technology = archetype.getTechnology();
4949
if (tokens.includes(TECHNOLOGY_INDEX)) {
5050
technology = tokens.get(TECHNOLOGY_INDEX);
5151
}
5252

53-
String[] tags = new String[0];
53+
String[] tags = archetype.getTags().toArray(new String[0]);
5454
if (tokens.includes(TAGS_INDEX)) {
5555
tags = tokens.get(TAGS_INDEX).split(",");
5656
}
5757

5858
return createRelationship(sourceElement, description, technology, tags, destinationElement);
5959
}
6060

61-
Set<Relationship> parse(ElementsDslContext context, Tokens tokens) {
61+
Set<Relationship> parse(ElementsDslContext context, Tokens tokens, Archetype archetype) {
6262
// <identifier> -> <identifier> [description] [technology] [tags]
6363

6464
if (tokens.hasMoreThan(TAGS_INDEX)) {
@@ -81,17 +81,17 @@ Set<Relationship> parse(ElementsDslContext context, Tokens tokens) {
8181
throw new RuntimeException("The destination element \"" + destinationId + "\" does not exist");
8282
}
8383

84-
String description = "";
84+
String description = archetype.getDescription();
8585
if (tokens.includes(DESCRIPTION_INDEX)) {
8686
description = tokens.get(DESCRIPTION_INDEX);
8787
}
8888

89-
String technology = "";
89+
String technology = archetype.getTechnology();
9090
if (tokens.includes(TECHNOLOGY_INDEX)) {
9191
technology = tokens.get(TECHNOLOGY_INDEX);
9292
}
9393

94-
String[] tags = new String[0];
94+
String[] tags = archetype.getTags().toArray(new String[0]);
9595
if (tokens.includes(TAGS_INDEX)) {
9696
tags = tokens.get(TAGS_INDEX).split(",");
9797
}

0 commit comments

Comments
 (0)