Skip to content

Commit

Permalink
epic: refactors @naturalId to favor new native format in zdl.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Dec 13, 2024
1 parent a62bc55 commit 743032f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public Map findAggregateCommandsForMethod(Map method, Options options) {
if(aggregate != null && command != null) {
return Map.of("templateFile", "aggregates-commands-methodBody", "aggregatesCommandsForMethod", aggregatesCommandsForMethod);
}
if(aggregate != null && crudMethod != null) {
return Map.of("templateFile", "aggregates-crud-methodBody", "aggregatesCommandsForMethod", aggregatesCommandsForMethod);
if (aggregate != null && crudMethod != null) {
// TODO: return Map.of("templateFile", "aggregates-crud-methodBody", "aggregatesCommandsForMethod", aggregatesCommandsForMethod);
}
if(entity != null && crudMethod != null) {
return Map.of("templateFile", "entities-crud-methodBody", "entity", entity, "crudMethod", crudMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public void processMethodEntity(Map<String, Object> zdlModel) {
var methods = JSONPath.get(zdlModel, "$.services[*].methods[*]", List.<Map>of());
for (Map method : methods) {
var serviceAggregates = JSONPath.get(zdlModel, "$.services." + method.get("serviceName") + ".aggregates", List.<String>of());
var entitiesForServices = serviceAggregates.stream().map(e -> (String) JSONPath.get(zdlModel, "$.aggregates." + e + ".aggregateRoot")).toList();
String entity = null;
String aggregate = null;
if(serviceAggregates.size() == 1) {
entity = serviceAggregates.get(0);
} else {
var returnType = JSONPath.get(method, "$.returnType");
if(serviceAggregates.contains(returnType)) {
if(serviceAggregates.contains(returnType) || entitiesForServices.contains(returnType)) {
entity = (String) returnType;
} else {
var entityForId = JSONPath.get(method, "$.options.entityForId");
Expand All @@ -66,11 +67,16 @@ public void processMethodEntity(Map<String, Object> zdlModel) {
}

// check if entity is in fact and aggregate
var aggregateRoot = (String) JSONPath.get(zdlModel, "$.allEntitiesAndEnums." + entity + ".aggregateRoot");
var aggregateRoot = (String) JSONPath.get(zdlModel, "$.aggregates." + entity + ".aggregateRoot");
if(aggregateRoot != null) {
aggregate = entity;
entity = aggregateRoot;
}
// check if entity is the root of an aggregate and this service is for the aggregate itself
var aggregateEntity = JSONPath.get(zdlModel, "$.aggregates[*][?(@.aggregateRoot == '" + entity + "')].name", List.<String>of());
if(aggregateEntity.size() == 1 && serviceAggregates.contains(aggregateEntity.get(0))) {
aggregate = aggregateEntity.get(0);
}

if(entity != null) {
method.put("entity", entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void should_return_single_service_entity() throws Exception {
var aggregatesMapForMethod = aggregateCommandsForMethod("MyServiceForEntity", "someMethod");

Assertions.assertEquals(1, aggregatesMapForMethod.size());
assertEquals(null, "$[0].aggregate", aggregatesMapForMethod);
assertEquals(null, "$[0].aggregate.name", aggregatesMapForMethod);
assertEquals("MyEntity", "$[0].entity.name", aggregatesMapForMethod);
}

Expand All @@ -26,7 +26,7 @@ public void should_return_single_service_entity_crud() throws Exception {
var aggregatesMapForMethod = aggregateCommandsForMethod("MyServiceForEntity", "createMyEntity");

Assertions.assertEquals(1, aggregatesMapForMethod.size());
assertEquals(null, "$[0].aggregate", aggregatesMapForMethod);
assertEquals(null, "$[0].aggregate.name", aggregatesMapForMethod);
assertEquals("MyEntity", "$[0].entity.name", aggregatesMapForMethod);
assertEquals("createMyEntity", "$[0].crudMethod", aggregatesMapForMethod);
}
Expand All @@ -36,7 +36,7 @@ public void should_return_command_returnType() throws Exception {
var aggregatesMapForMethod = aggregateCommandsForMethod("MyServiceForEntities", "shouldResolveByReturnType");

Assertions.assertEquals(1, aggregatesMapForMethod.size());
assertEquals(null, "$[0].aggregate", aggregatesMapForMethod);
assertEquals(null, "$[0].aggregate.name", aggregatesMapForMethod);
assertEquals("MyEntity2", "$[0].entity.name", aggregatesMapForMethod);
}

Expand All @@ -45,7 +45,7 @@ public void should_return_command_returnType_entity_crud() throws Exception {
var aggregatesMapForMethod = aggregateCommandsForMethod("MyServiceForAggregate", "createMyEntity");

Assertions.assertEquals(1, aggregatesMapForMethod.size());
assertEquals(null, "$[0].aggregate", aggregatesMapForMethod);
assertEquals("MyAggregate", "$[0].aggregate.name", aggregatesMapForMethod);
assertEquals("MyEntity", "$[0].entity.name", aggregatesMapForMethod);
assertEquals("createMyEntity", "$[0].crudMethod", aggregatesMapForMethod);
}
Expand All @@ -55,17 +55,17 @@ public void should_return_command_returnType_aggregate_crud() throws Exception {
var aggregatesMapForMethod = aggregateCommandsForMethod("MyServiceForAggregate", "createMyAggregate");

Assertions.assertEquals(1, aggregatesMapForMethod.size());
assertEquals(null, "$[0].aggregate.name", aggregatesMapForMethod);
assertEquals(null, "$[0].crudMethod", aggregatesMapForMethod);
assertEquals("MyEntity", "$[0].entity.name", aggregatesMapForMethod);
assertEquals("MyAggregate", "$[0].aggregate.name", aggregatesMapForMethod);
}

@Test
public void should_return_command_returnType_aggregate() throws Exception {
var aggregatesMapForMethod = aggregateCommandsForMethod("MyServiceForAggregates", "shouldResolveByReturnType");

Assertions.assertEquals(1, aggregatesMapForMethod.size());
assertEquals(null, "$[0].aggregate", aggregatesMapForMethod);
assertEquals("MyAggregate2", "$[0].aggregate.name", aggregatesMapForMethod);
assertEquals("MyEntity2", "$[0].entity.name", aggregatesMapForMethod);
}

Expand Down

0 comments on commit 743032f

Please sign in to comment.