Skip to content

Commit

Permalink
Adds discovered components into the DSL parser identifiers register.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 22, 2024
1 parent 726f6eb commit 6c8689a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ private void findDependencies(com.structurizr.component.Type type) {
/**
* Find components, using all configured rules, in the order they were added.
*/
public void findComponents() {
public Set<Component> findComponents() {
Set<DiscoveredComponent> discoveredComponents = new LinkedHashSet<>();
Map<DiscoveredComponent, Component> componentMap = new HashMap<>();
Set<Component> componentSet = new LinkedHashSet<>();

for (ComponentFinderStrategy componentFinderStrategy : componentFinderStrategies) {
Set<DiscoveredComponent> set = componentFinderStrategy.findComponents(typeRepository);
Expand All @@ -119,6 +120,7 @@ public void findComponents() {
component.setDescription(discoveredComponent.getDescription());
component.setTechnology(discoveredComponent.getTechnology());
componentMap.put(discoveredComponent, component);
componentSet.add(component);
}

// find dependencies between all components
Expand All @@ -141,6 +143,8 @@ public void findComponents() {
Component component = componentMap.get(discoveredComponent);
discoveredComponent.getComponentFinderStrategy().visit(component);
}

return componentSet;
}

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

import com.structurizr.component.ComponentFinderBuilder;
import com.structurizr.model.Component;
import com.structurizr.model.Container;

import java.util.Set;

final class ComponentFinderDslContext extends DslContext {

private final ComponentFinderBuilder componentFinderBuilder = new ComponentFinderBuilder();

ComponentFinderDslContext(Container container) {
private final StructurizrDslParser dslParser;

ComponentFinderDslContext(StructurizrDslParser dslParser, Container container) {
this.dslParser = dslParser;
componentFinderBuilder.forContainer(container);
}

Expand All @@ -26,7 +32,10 @@ ComponentFinderBuilder getComponentFinderBuilder() {

@Override
void end() {
componentFinderBuilder.build().findComponents();
Set<Component> components = componentFinderBuilder.build().findComponents();
for (Component component : components) {
dslParser.registerIdentifier(IdentifiersRegister.toIdentifier(component.getName()), component);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,13 @@ void validateIdentifierName(String identifier) {
}
}

static String toIdentifier(String s) {
String identifierName = s.replaceAll("[^a-zA-Z0-9_-]", "");
if (identifierName.startsWith("-")) {
identifierName = identifierName.substring(1);
}

return identifierName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void parse(List<String> lines, File dslFile, boolean include) throws Structurizr
} else if (COMPONENT_FINDER_TOKEN.equalsIgnoreCase(firstToken) && inContext(ContainerDslContext.class)) {
if (!restricted) {
if (shouldStartContext(tokens)) {
startContext(new ComponentFinderDslContext(getContext(ContainerDslContext.class).getContainer()));
startContext(new ComponentFinderDslContext(this, getContext(ContainerDslContext.class).getContainer()));
}
}

Expand Down Expand Up @@ -1121,12 +1121,12 @@ public IdentifiersRegister getIdentifiersRegister() {
return identifiersRegister;
}

private void registerIdentifier(String identifier, Element element) {
void registerIdentifier(String identifier, Element element) {
identifiersRegister.register(identifier, element);
element.addProperty(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME, identifiersRegister.findIdentifier(element));
}

private void registerIdentifier(String identifier, Relationship relationship) {
void registerIdentifier(String identifier, Relationship relationship) {
identifiersRegister.register(identifier, relationship);
relationship.addProperty(STRUCTURIZR_DSL_IDENTIFIER_PROPERTY_NAME, identifiersRegister.findIdentifier(relationship));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1161,42 +1161,49 @@ void springPetClinic() throws Exception {
assertEquals("org.springframework.samples.petclinic.system.WelcomeController", welcomeController.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java").getAbsolutePath(), welcomeController.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java", welcomeController.getUrl());
assertSame(welcomeController, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.welcomecontroller"));

Component ownerController = webApplication.getComponentWithName("Owner Controller");
assertNotNull(ownerController);
assertEquals("org.springframework.samples.petclinic.owner.OwnerController", ownerController.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java").getAbsolutePath(), ownerController.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java", ownerController.getUrl());
assertSame(ownerController, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.ownerController"));

Component petController = webApplication.getComponentWithName("Pet Controller");
assertNotNull(petController);
assertEquals("org.springframework.samples.petclinic.owner.PetController", petController.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/owner/PetController.java").getAbsolutePath(), petController.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/PetController.java", petController.getUrl());
assertSame(petController, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.petcontroller"));

Component vetController = webApplication.getComponentWithName("Vet Controller");
assertNotNull(vetController);
assertEquals("org.springframework.samples.petclinic.vet.VetController", vetController.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/vet/VetController.java").getAbsolutePath(), vetController.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/vet/VetController.java", vetController.getUrl());
assertSame(vetController, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.vetcontroller"));

Component visitController = webApplication.getComponentWithName("Visit Controller");
assertNotNull(visitController);
assertEquals("org.springframework.samples.petclinic.owner.VisitController", visitController.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/owner/VisitController.java").getAbsolutePath(), visitController.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java", visitController.getUrl());
assertSame(visitController, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.visitcontroller"));

Component ownerRepository = webApplication.getComponentWithName("Owner Repository");
assertNotNull(ownerRepository);
assertEquals("org.springframework.samples.petclinic.owner.OwnerRepository", ownerRepository.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java").getAbsolutePath(), ownerRepository.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java", ownerRepository.getUrl());
assertSame(ownerRepository, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.ownerrepository"));

Component vetRepository = webApplication.getComponentWithName("Vet Repository");
assertNotNull(vetRepository);
assertEquals("org.springframework.samples.petclinic.vet.VetRepository", vetRepository.getProperties().get("component.type"));
assertEquals(new File(springPetClinicHome, "src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java").getAbsolutePath(), vetRepository.getProperties().get("component.src"));
assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java", vetRepository.getUrl());
assertSame(vetRepository, parser.getIdentifiersRegister().getElement("springPetClinic.webApplication.vetrepository"));

assertTrue(welcomeController.getRelationships().isEmpty());

Expand Down

0 comments on commit 6c8689a

Please sign in to comment.