Skip to content

Commit

Permalink
java DomainEvent.ftl improvement.
Browse files Browse the repository at this point in the history
Signed-off-by: hurelhuyag <hurelhuyag@gmail.com>
  • Loading branch information
hurelhuyag committed May 14, 2021
1 parent d188d51 commit 091fe22
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.vlingo.xoom.schemata.codegen;

import io.vlingo.xoom.schemata.codegen.ast.FieldDefinition;
import io.vlingo.xoom.schemata.codegen.ast.types.BasicArrayType;
import io.vlingo.xoom.schemata.codegen.ast.types.TypeDefinition;
import io.vlingo.xoom.schemata.codegen.ast.values.ListValue;
import io.vlingo.xoom.schemata.codegen.ast.values.SingleValue;
import io.vlingo.xoom.schemata.codegen.ast.values.Value;

import java.util.List;
Expand All @@ -13,38 +16,73 @@ public class DomainEventArguments {
public final String version;
public final List<Property> properties;
public final String typeName;
public final boolean createDefaultConstructor;
public final boolean createRequiredArgsConstructor;

public DomainEventArguments(String language, String fqdn, String version, TypeDefinition node) {
this.namespace = extractNamespace(fqdn);
this.typeName = node.typeName;
this.version = version;
this.properties = extractProperties(language, node);
this.createDefaultConstructor = properties.stream()
.filter(property -> !property.name.equals("eventType"))
.filter(property -> !property.name.equals("eventVersion"))
.filter(property -> !property.name.equals("occurredOn"))
.allMatch(property -> property.value != null);
this.createRequiredArgsConstructor = properties.stream()
.anyMatch(property -> !property.name.equals("eventType")
&& !property.name.equals("eventVersion")
&& !property.name.equals("occurredOn"));
}

public static class Property {
public final String type;
public final boolean array;
public final String name;
public final Object value;

public Property(String type, String name, Object value) {
public Property(String type, boolean array, String name, Object value) {
this.type = type;
this.array = array;
this.name = name;
this.value = value;
}

@Override
public String toString() {
return "Property{" +
"type='" + type + '\'' +
", array=" + array +
", name='" + name + '\'' +
", value=" + value +
'}';
}
}

private String extractNamespace(String fqdn) {
int i = fqdn.indexOf(':', fqdn.indexOf(':') + 1) + 1;
int j = fqdn.indexOf(':', i);
return fqdn.substring(i, j) + ".model";
return fqdn.substring(i, j) + ".event";
}

private List<Property> extractProperties(String language, TypeDefinition root) {
TypeMap typeMap = TypeMap.valueOf(language);
return root.children.stream()
.map(node -> (FieldDefinition) node)
.map(node -> new Property(typeMap.typeOf(node.type.name()), node.name,
node.defaultValue.map(Value::value).orElse(null)))
.map(node -> {
String type = typeMap.typeOf(node.type.name());
boolean array = node.type instanceof BasicArrayType;
Object defaultValue = array
? node.defaultValue
.filter(value -> value instanceof ListValue)
.map(value -> (ListValue<List<SingleValue>>)value)
.map(listValue -> listValue.value.stream()
.map(Value::value)
.collect(Collectors.toList()))
.orElse(null)
: node.defaultValue.map(Value::value).orElse(null);
return new Property(type, array, node.name, defaultValue);
})
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.vlingo.xoom.schemata.codegen.backend;

import freemarker.template.*;
import io.vlingo.xoom.codegen.CodeGenerationException;
import io.vlingo.xoom.common.Outcome;
import io.vlingo.xoom.common.Success;
import io.vlingo.xoom.schemata.errors.SchemataBusinessException;
import io.vlingo.xoom.turbo.codegen.CodeGenerationException;

import java.io.IOException;
import java.io.StringWriter;
Expand Down
35 changes: 33 additions & 2 deletions src/main/resources/codegen/java/DomainEvent.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,50 @@ package ${namespace};
import io.vlingo.xoom.common.version.SemanticVersion;
import io.vlingo.xoom.lattice.model.DomainEvent;

<#macro printValue value array type>
<@compress single_line=true>
<#if array>
new ${type}[] {<#list value as v>
${v}<#if v?has_next>, </#if>
</#list>}
<#else>
${value}
</#if>
</@compress>
</#macro>

public final class ${typeName} extends DomainEvent {

<#list properties as p>
public <#if !p.value??>final </#if>${p.type} ${p.name}<#if p.value??> = ${p.value}</#if>;
public <#if !p.value??>final </#if>${p.type}<#if p.array>[]</#if> ${p.name}<#if p.value??> = <@printValue p.value p.array p.type /></#if>;
</#list>

public ${typeName}(<#list properties as p><#if p.name != 'eventVersion'>final ${p.type} ${p.name}<#if p?has_next>, </#if></#if></#list>) {
<#if createDefaultConstructor>
public ${typeName}() {
<#list properties as p>
<#if p.name = 'eventVersion'>
this.${p.name} = SemanticVersion.toValue("${version}");
<#elseif p.name = 'eventType'>
this.${p.name} = "${typeName}";
<#elseif p.name = 'occurredOn'>
this.${p.name} = System.currentTimeMillis();
</#if>
</#list>
}
</#if>
<#if createRequiredArgsConstructor>
public ${typeName}(<#list properties as p><#if p.name != 'eventVersion' && p.name != 'eventType' && p.name != 'occurredOn'>final ${p.type}<#if p.array>[]</#if> ${p.name}<#if p?has_next>, </#if></#if></#list>) {
<#list properties as p>
<#if p.name = 'eventVersion'>
this.${p.name} = SemanticVersion.toValue("${version}");
<#elseif p.name = 'eventType'>
this.${p.name} = "${typeName}";
<#elseif p.name = 'occurredOn'>
this.${p.name} = System.currentTimeMillis();
<#else>
this.${p.name} = ${p.name};
</#if>
</#list>
}
</#if>
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testThatGeneratesABasicType() throws ExecutionException, Interrupted
assertTrue(result.contains("public SalutationHappened(final String toWhom, final String text) {"));
assertTrue(result.contains("this.eventType = \"SalutationHappened\";"));
assertTrue(result.contains("this.occurredOn = System.currentTimeMillis();"));
assertTrue(result.contains("this.eventVersion = io.vlingo.xoom.common.version.SemanticVersion.toValue(\"0.0.1\");"));
assertTrue(result.contains("this.eventVersion = SemanticVersion.toValue(\"0.0.1\");"));
assertTrue(result.contains("this.toWhom = toWhom;"));
assertTrue(result.contains("this.text = text;"));
assertFalse(result.contains("public SalutationHappened()"));
Expand Down Expand Up @@ -64,7 +64,7 @@ public void testThatGeneratesABasicTypeWithDefaultValuesAndComputedFields() thro

assertTrue(result.contains("this.eventType = \"SalutationHappened\";"));
assertTrue(result.contains("this.occurredOn = System.currentTimeMillis();"));
assertTrue(result.contains("this.eventVersion = io.vlingo.xoom.common.version.SemanticVersion.toValue(\"0.0.1\");"));
assertTrue(result.contains("this.eventVersion = SemanticVersion.toValue(\"0.0.1\");"));

assertTrue(result.contains("public boolean booleanAttribute = true;"));
assertTrue(result.contains("public byte byteAttribute = 4;"));
Expand Down Expand Up @@ -120,7 +120,7 @@ public void testThatGeneratesBasicTypeArrayFieldsWithDefaults() throws Execution
assertTrue(result.contains("public int[] intAttribute = new int[] { 4242, 424242, 42424242 }"));
assertTrue(result.contains("public long[] longAttribute = new long[] { 42L, 4242L, 424242L }"));
assertTrue(result.contains("public short[] shortAttribute = new short[] { 258, 259, 260 }"));
assertTrue(result.contains("public String[] stringAttribute = new java.lang.String[] { \"foo\", \"bar\", \"baz\" }"));
assertTrue(result.contains("public String[] stringAttribute = new String[] { \"foo\", \"bar\", \"baz\" }"));
}

@Test
Expand All @@ -144,14 +144,17 @@ public void testThatGeneratesAComposedTypeWithVersionedData() throws ExecutionEx
registerType("types/price", "Org:Unit:Context:Schema:Price", "1.0.0");
final String result = compileSpecAndUnwrap(compilerWithJavaBackend(),typeDefinition("price-changed"), "Org:Unit:Context:Schema:PriceChanged", "0.5.1");

assertTrue(result.contains("import io.vlingo.xoom.common.version.SemanticVersion;"));
assertTrue(result.contains("import io.vlingo.xoom.common.version.SemanticVersion;"));
assertTrue(result.contains("import io.vlingo.xoom.lattice.model.DomainEvent;"));
assertTrue(result.contains("public final class PriceChanged extends DomainEvent {"));
assertTrue(result.contains("public final long occurredOn;"));
assertTrue(result.contains("public final int eventVersion;"));
assertTrue(result.contains("public final Price oldPrice;"));
assertTrue(result.contains("public final Price newPrice;"));
assertTrue(result.contains("public PriceChanged(final Price oldPrice, final Price newPrice) {"));
assertTrue(result.contains("this.occurredOn = System.currentTimeMillis();"));
assertTrue(result.contains("this.eventVersion = io.vlingo.xoom.common.version.SemanticVersion.toValue(\"0.5.1\");"));
assertTrue(result.contains("this.eventVersion = SemanticVersion.toValue(\"0.5.1\");"));
assertTrue(result.contains("this.oldPrice = oldPrice;"));
assertTrue(result.contains("this.newPrice = newPrice;"));
}
Expand Down

0 comments on commit 091fe22

Please sign in to comment.