Skip to content

Commit

Permalink
rename stateTransfer
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Mar 2, 2023
1 parent 3468073 commit 9b5806c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 11 deletions.
4 changes: 2 additions & 2 deletions plugins/jdl-to-asyncapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jbang zw -p io.zenwave360.sdk.plugins.JDLToAsyncAPIPlugin \
idType=integer \
idTypeFormat=int64 \
annotations=aggregate \
payloadStyle=stateTransfer \
payloadStyle=event \
targetFile=src/main/resources/model/asyncapi.yml
```

Expand All @@ -46,7 +46,7 @@ jbang zw -p io.zenwave360.sdk.plugins.JDLToAsyncAPIPlugin \
| `targetFolder` | Target folder for generated output | String | | |
| `targetFile` | Target file | String | asyncapi.yml | |
| `schemaFormat` | Schema format for messages' payload | SchemaFormat | schema | schema, avro |
| `payloadStyle` | Payload Style for messages' payload | PayloadStyle | entity | entity, stateTransfer |
| `payloadStyle` | Payload Style for messages' payload | PayloadStyle | entity | entity, event |
| `idType` | JsonSchema type for id fields and parameters. | String | string | |
| `idTypeFormat` | JsonSchema type format for id fields and parameters. | String | | |
| `entities` | Entities to generate code for | List | [] | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum SchemaFormat {
}

enum PayloadStyle {
entity, stateTransfer
entity, event
}

public String sourceProperty = "jdl";
Expand Down Expand Up @@ -184,7 +184,7 @@ protected List<TemplateOutput> convertToAvro(JDLEntitiesToAvroConverter converte

avroList.add(new TemplateOutput(String.format("%s/%s.avsc", targetFolder, name), avroJson, OutputFormatType.JSON.toString()));

if (payloadStyle == PayloadStyle.stateTransfer && (!skipOperations(entityOrEnum) || entityOrEnum.get("fields") == null)) {
if (payloadStyle == PayloadStyle.event && (!skipOperations(entityOrEnum) || entityOrEnum.get("fields") == null)) {
// creating 'fake' jdl entities for message payloads for created/updated/deleted as { id: <id>, payload: <entity> }

Map<String, Object> fields = new HashMap<>();
Expand Down Expand Up @@ -252,13 +252,13 @@ protected boolean skipOperations(Map entity) {
});

handlebarsEngine.getHandlebars().registerHelper("isStateTransferPayloadStyle", (context, options) -> {
return payloadStyle == PayloadStyle.stateTransfer;
return payloadStyle == PayloadStyle.event;
});

handlebarsEngine.getHandlebars().registerHelper("payloadRef", (context, options) -> {
Map entity = (Map) context;
String messageType = payloadStyle == PayloadStyle.stateTransfer ? options.param(0) : "";
String payloadStyleSuffix = payloadStyle == PayloadStyle.stateTransfer ? "Payload" : "";
String messageType = payloadStyle == PayloadStyle.event ? options.param(0) : "";
String payloadStyleSuffix = payloadStyle == PayloadStyle.event ? "Payload" : "";
if (schemaFormat == SchemaFormat.avro) {
return String.format("avro/%s%s%s.avsc", entity.get("className"), messageType, payloadStyleSuffix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void test_jdl_to_asyncapi_state_transfer_style() throws Exception {
Map<String, Object> model = loadJDLModelFromResource("classpath:io/zenwave360/sdk/resources/jdl/orders-model.jdl");
JDLToAsyncAPIGenerator generator = new JDLToAsyncAPIGenerator();
generator.includeCommands = true;
generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.stateTransfer;
generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.event;
generator.annotations = List.of("aggregate");

List<TemplateOutput> outputTemplates = generator.generate(model);
Expand All @@ -94,7 +94,7 @@ public void test_jdl_to_asyncapi_with_avro_state_transfer_style() throws Excepti
JDLToAsyncAPIGenerator generator = new JDLToAsyncAPIGenerator();
generator.schemaFormat = JDLToAsyncAPIGenerator.SchemaFormat.avro;
generator.includeCommands = true;
generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.stateTransfer;
generator.payloadStyle = JDLToAsyncAPIGenerator.PayloadStyle.event;

List<TemplateOutput> outputTemplates = generator.generate(model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

import java.math.BigDecimal;

import static org.springframework.http.HttpMethod.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

import java.math.BigDecimal;

import static org.springframework.http.HttpMethod.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

import java.math.BigDecimal;

import static org.springframework.http.HttpMethod.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
{{requestBodyVar}}.set{{asJavaTypeName @key}}({{{newPropertyObject property}}});
{{~#if (eq property.type 'object')}}
{{~#each property.properties as |innerProperty|}}
{{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().set{{asJavaTypeName @key}}(null);
{{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().set{{asJavaTypeName @key}}({{{populateProperty innerProperty}}});
{{~/each}}
{{~else if (eq property.type 'array')}}
{{~#each property.items.properties as |innerProperty|}}
{{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().get(0).set{{asJavaTypeName @key}}(null);
{{requestBodyVar}}.get{{asJavaTypeName parentPropertyName}}().get(0).set{{asJavaTypeName @key}}({{{populateProperty innerProperty}}});
{{~/each}}
{{~else}}
{{~/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<typeMappings>
<typeMapping>Double=java.math.BigDecimal</typeMapping>
</typeMappings>
<configOptions>
<documentationProvider>none</documentationProvider>
<openApiNullable>false</openApiNullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.*;
import java.util.stream.Collectors;

import io.zenwave360.sdk.parsers.JDLParser;
import io.zenwave360.sdk.utils.JSONPath;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -12,8 +13,46 @@

import io.zenwave360.sdk.utils.NamingUtils;

import static org.apache.commons.lang3.StringUtils.capitalize;

public class CustomHandlebarsHelpers {

public static String populateProperty(Map property, Options options) {
String type = (String) property.get("type");
String format = (String) property.get("format");
if ("date".equals(format)) {
return "new Date()";
}
if ("date-time".equals(format)) {
return "Instant.now()";
}
if ("integer".equals(type) && "int32".equals(format)) {
return "1";
}
if ("integer".equals(type) && "int64".equals(format)) {
return "1L";
}
if ("number".equals(type)) {
return "BigDecimal.valueOf(0)";
}
if ("boolean".equals(type)) {
return "true";
}
if ("array".equals(type)) {
var items = (Map<String, Object>) property.get("items");
var propertyName = (String) property.get("x--property-name");
return "null";
}
if (property.get("x--schema-name") != null) {
// root level #/component/schemas would be an entity or enum
String otherEntity = (String) property.get("x--schema-name");
String propertyName = (String) property.get("x--property-name");
return "new " + otherEntity + "()";
}
return "\"aaa\"";
}


public static Object jsonPath(Object entity, Options options) throws IOException {
String jsonPath = StringUtils.join(options.params, "");
Object defaultValue = options.hash.get("default");
Expand Down

0 comments on commit 9b5806c

Please sign in to comment.