Skip to content

Commit

Permalink
CamelDefinitionApiGenerator and CamelDefinitionYamlStepGenerator for …
Browse files Browse the repository at this point in the history
…Camel YAML DSL 4.1.0
  • Loading branch information
mgubaidullin committed Oct 19, 2023
1 parent c9fa438 commit ba9d49d
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 617 deletions.
275 changes: 102 additions & 173 deletions karavan-core/src/core/api/CamelDefinitionApi.ts

Large diffs are not rendered by default.

360 changes: 93 additions & 267 deletions karavan-core/src/core/api/CamelDefinitionYamlStep.ts

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions karavan-core/src/core/model/CamelDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,18 +1516,18 @@ export class ToDefinition extends CamelElement {
}

export class ToDynamicDefinition extends CamelElement {
stepName?: string = 'toDynamic';
stepName?: string = 'toD';
uri: string = '';
pattern?: string;
cacheSize?: number;
ignoreInvalidEndpoint?: boolean;
allowOptimisedComponents?: boolean;
autoStartComponents?: boolean;
cacheSize?: number;
description?: string;
disabled?: boolean;
id?: string = 'toDynamic-' + uuidv4().substring(0,4);
ignoreInvalidEndpoint?: boolean;
id?: string = 'toD-' + uuidv4().substring(0,4);
description?: string;
inheritErrorHandler?: boolean;
parameters?: any = {};
pattern?: string;
uri: string = '';
public constructor(init?: Partial<ToDynamicDefinition>) {
super('ToDynamicDefinition');
Object.assign(this, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ protected String getStepNameForClass (String className) {
className = "convertBodyTo";
} else if (className.equals("FinallyDefinition")) {
className = "doFinally";
} else if (className.equals("ToDynamicDefinition")) {
className = "toD";
} else if (className.equals("SamplingDefinition")) {
className = "sample";
} else if (className.endsWith("Definition")) {
Expand Down Expand Up @@ -94,11 +96,60 @@ protected Map<String, String> getExpressionStepNameForClass(){
String fullClassName = prop.getJsonObject("properties").getJsonObject(key).getString("$ref");
String className = classSimple(fullClassName);
stepNames.put(className, key);
System.out.println(className + " : " + key);
}
return stepNames;
}

private Map<String, JsonObject> getJsonObjectProperties (JsonObject val) {
Map<String, JsonObject> properties = new LinkedHashMap<>();
val.getMap().keySet().forEach(s -> {
JsonObject value = val.getJsonObject(s);
if (!value.getMap().isEmpty()) {
properties.put(s, val.getJsonObject(s));
} else if (s.equals("expression")){
properties.put(s, JsonObject.of("$ref", "#/items/definitions/org.apache.camel.model.language.ExpressionDefinition"));
}
});
return properties;
}

protected Map<String, JsonObject> getClassProperties (JsonObject obj) {
Map<String, JsonObject> properties = new LinkedHashMap<>();

obj.getMap().keySet().forEach(key -> {
if (key.equals("oneOf")) {
JsonObject val = obj.getJsonArray("oneOf").getJsonObject(1).getJsonObject("properties");
properties.putAll(getJsonObjectProperties(val));
} else if (key.equals("properties")) {
JsonObject val = obj.getJsonObject("properties");
properties.putAll(getJsonObjectProperties(val));
} else if (key.equals("anyOf")) {
JsonArray vals = obj.getJsonArray("anyOf").getJsonObject(0).getJsonArray("oneOf");
for (int i = 0; i < vals.size(); i++){
JsonObject data = vals.getJsonObject(i);
if (!data.containsKey("not") && data.containsKey("type")) {
JsonObject val = data.getJsonObject("properties");
properties.putAll(getJsonObjectProperties(val));
}
}
}
});
return properties;
}

protected Comparator<String> getComparator(String stepName) {
String json = getMetaModel(stepName);
if (json != null) {
JsonObject props = new JsonObject(json).getJsonObject("properties");
List propsLowerCase = props.getMap().keySet().stream().map(String::toLowerCase).collect(Collectors.toList());
return Comparator.comparing(e -> {
if (propsLowerCase.contains(e.toLowerCase())) return propsLowerCase.indexOf(e.toLowerCase());
else return propsLowerCase.size() + 1;
});
}
return Comparator.comparing(s -> 0);
}

// protected Map<String, String> getStepNames(){
// // Prepare stepNames map
// JsonObject definitions = getDefinitions();
Expand All @@ -118,8 +169,8 @@ protected Map<String, JsonObject> getDslMetadata(){
// Prepare stepNames map
Map<String, String> stepNames = getProcessorStepNameMap();

Map<String, JsonObject> classProps = new HashMap<>();
Map<String, Object> defsMap = new HashMap<>();
Map<String, JsonObject> classProps = new LinkedHashMap<>();
Map<String, Object> defsMap = new LinkedHashMap<>();
defsMap.putAll(definitions.getJsonObject("org.apache.camel.model.ProcessorDefinition").getJsonObject("properties").getMap());
defsMap.putAll(new JsonObject(camelYamlDSL).getJsonObject("items").getJsonObject("properties").getMap());

Expand Down Expand Up @@ -284,26 +335,8 @@ protected List<String> getClasses(JsonObject definitions, String filter) {
return result;
}

protected Map<String, String> getProcessorStepName(JsonObject properties) {
Map<String, String> result = new HashMap<>();
properties.getMap().forEach((name, o) -> {
String ref = properties.getJsonObject(name).getString("$ref");
ref = ref.equals("#/items/definitions/org.apache.camel.dsl.yaml.deserializers.RouteFromDefinitionDeserializer")
? "#/items/definitions/org.apache.camel.model.FromDefinition"
: ref;
ref = ref.equals("#/items/definitions/org.apache.camel.dsl.yaml.deserializers.ErrorHandlerBuilderDeserializer")
? "#/items/definitions/org.apache.camel.model.ErrorHandlerDefinition"
: ref;
String className = classSimple(ref);
result.put(className, className.equals("ToDynamicDefinition") ? "toD" : name);
});
return result;
}



protected Map<String, String> getProcessorStepNameMapForObject(String key, JsonObject jsonObject) {
Map<String, String> result = new HashMap<>();
Map<String, String> result = new LinkedHashMap<>();

jsonObject.fieldNames().forEach(k -> {
Object object = jsonObject.getValue(k);
Expand All @@ -323,7 +356,7 @@ protected Map<String, String> getProcessorStepNameMapForObject(String key, JsonO
}

protected Map<String, String> getProcessorStepNameMapForArray(JsonArray jsonArray) {
Map<String, String> result = new HashMap<>();
Map<String, String> result = new LinkedHashMap<>();

jsonArray.forEach(object -> {
if (object instanceof JsonObject) {
Expand All @@ -339,8 +372,28 @@ protected Map<String, String> getProcessorStepNameMap() {
String camelYamlDSL = getCamelYamlDSL();
JsonObject definitions = new JsonObject(camelYamlDSL);

Map<String, String> result = new HashMap<>(getProcessorStepNameMapForObject(null, definitions));
result.put("ToDynamicDefinition", "toD");
Map<String, String> result = new LinkedHashMap<>(getProcessorStepNameMapForObject(null, definitions));
return result;
}

protected Map<String, String> getProcessorDefinitionStepNameMap() {
Map<String, String> result = new LinkedHashMap<>();
String camelYamlDSL = getCamelYamlDSL();
JsonObject definitions = new JsonObject(camelYamlDSL);

JsonObject properties = definitions
.getJsonObject("items")
.getJsonObject("definitions")
.getJsonObject("org.apache.camel.model.ProcessorDefinition")
.getJsonObject("properties");

properties.getMap().forEach((key, o) -> {
String ref = ((Map)o).get("$ref").toString();
System.out.println(ref);
System.out.println(key);
String className = classSimple(ref);
result.put(className, key);
});
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ private void createCamelDefinitions() throws Exception {
});

// generate createStep function
Map<String, String> stepNames = getProcessorStepName(new JsonObject(camelYamlDSL).getJsonObject("items").getJsonObject("properties"));
stepNames.putAll(getProcessorStepName(definitions.getJsonObject("org.apache.camel.model.ProcessorDefinition").getJsonObject("properties")));
Map<String, String> stepNames = getProcessorStepNameMap();
// Map<String, String> stepNames = getProcessorStepName(new JsonObject(camelYamlDSL).getJsonObject("items").getJsonObject("properties"));
// stepNames.putAll(getProcessorStepName(definitions.getJsonObject("org.apache.camel.model.ProcessorDefinition").getJsonObject("properties")));
StringBuilder cs = new StringBuilder(
" static createStep = (name: string, body: any, clone: boolean = false): CamelElement => {\n" +
" const newBody = CamelUtil.camelizeBody(name, body, clone);\n" +
Expand All @@ -85,16 +86,18 @@ private void createCamelDefinitions() throws Exception {
camelModel.append(cs);

// generate createExpression function
stepNames.clear();
stepNames.putAll(getProcessorStepName(definitions.getJsonObject("org.apache.camel.model.language.ExpressionDefinition").getJsonObject("properties")));
// stepNames.clear();
// stepNames.putAll(getProcessorStepName(definitions.getJsonObject("org.apache.camel.model.language.ExpressionDefinition").getJsonObject("properties")));
StringBuilder ce = new StringBuilder(
" static createExpression = (name: string, body: any): CamelElement => {\n" +
" const newBody = CamelUtil.camelizeBody(name, body, false);\n" +
" delete newBody.expressionName;\n" +
" delete newBody.dslName;\n" +
" switch (name) { \n"
);
stepNames.forEach((className, stepName) -> {
stepNames.entrySet().stream().filter(e-> e.getKey().endsWith("Expression")).forEach(e -> {
String className = e.getKey();
String stepName = e.getValue();
String code = String.format(" case '%1$s': return CamelDefinitionApi.create%1$s(newBody);\n", className);
ce.append(code);
});
Expand All @@ -105,16 +108,19 @@ private void createCamelDefinitions() throws Exception {
camelModel.append(ce);

// generate createDataFormat function
stepNames.clear();
stepNames.putAll(getProcessorStepName(definitions.getJsonObject("org.apache.camel.model.dataformat.DataFormatsDefinition").getJsonObject("properties")));
// stepNames.clear();
// stepNames.putAll(getProcessorStepName(definitions.getJsonObject("org.apache.camel.model.dataformat.DataFormatsDefinition").getJsonObject("properties")));
StringBuilder df = new StringBuilder(
" static createDataFormat = (name: string, body: any): CamelElement => {\n" +
" const newBody = CamelUtil.camelizeBody(name, body, false);\n" +
" delete newBody.dataFormatName;\n" +
" delete newBody.dslName;\n" +
" switch (name) { \n"
);
stepNames.forEach((className, stepName) -> {
stepNames.entrySet().stream().filter(e-> e.getKey().endsWith("DataFormat")).forEach(e -> {
String className = e.getKey();
String stepName = e.getValue();
// stepNames.forEach((className, stepName) -> {
String code = String.format(" case '%1$s': return CamelDefinitionApi.create%1$s(newBody);\n", className);
df.append(code);
});
Expand All @@ -132,44 +138,41 @@ private void createCamelDefinitions() throws Exception {

private String generateModelApi(String classFullName, JsonObject obj) {
String className = classSimple(classFullName);
String stepName = getStepNameForClass(className);

JsonObject properties = obj.containsKey("oneOf")
? obj.getJsonArray("oneOf").getJsonObject(1).getJsonObject("properties")
: obj.getJsonObject("properties");
Map<String, JsonObject> properties = getClassProperties(obj);

List<String> attrs = new ArrayList<>();
AtomicBoolean hasId = new AtomicBoolean(false);
if (properties != null) {
properties.getMap().keySet().forEach(name -> {
JsonObject aValue = properties.getJsonObject(name);
if ("id".equals(name)) {
hasId.set(true);
}
if (isAttributeRefArray(aValue) && name.equals("steps") && ! className.equals("ChoiceDefinition") && ! className.equals("SwitchDefinition") && ! className.equals("KameletDefinition")) {
attrs.add(" def.steps = CamelDefinitionApi.createSteps(element?.steps);");
} else if (isAttributeRefArray(aValue) && !name.equals("steps")) {
String code = String.format(
" def.%1$s = element && element?.%1$s ? element?.%1$s.map((x:any) => CamelDefinitionApi.create%2$s(x)) :[];"
, name, getAttributeArrayClass(aValue));
attrs.add(code);
} else if (isAttributeRef(aValue)
&& !getAttributeClass(aValue).equals("SagaActionUriDefinition") // SagaActionUriDefinition is exception
&& !getAttributeClass(aValue).equals("ToDefinition") // exception for ToDefinition (in REST Methods)
&& !getAttributeClass(aValue).equals("ToDynamicDefinition") // exception for ToDynamicDefinition (in REST Methods)
) {
String attributeClass = getAttributeClass(aValue);
String template = attributeClass.equals("ExpressionDefinition")
? " def.%1$s = CamelDefinitionApi.create%2$s(element.%1$s); \n"
: " if (element?.%1$s !== undefined) { \n" +
" def.%1$s = CamelDefinitionApi.create%2$s(element.%1$s); \n" +
" }";
String code = String.format(template, name, getAttributeClass(aValue));
attrs.add(code);
} else {

}
});
}
properties.keySet().stream().sorted(getComparator(stepName)).forEach(name -> {
JsonObject aValue = properties.get(name);
if ("id".equals(name)) {
hasId.set(true);
}
if (isAttributeRefArray(aValue) && name.equals("steps") && ! className.equals("ChoiceDefinition") && ! className.equals("SwitchDefinition") && ! className.equals("KameletDefinition")) {
attrs.add(" def.steps = CamelDefinitionApi.createSteps(element?.steps);");
} else if (isAttributeRefArray(aValue) && !name.equals("steps")) {
String code = String.format(
" def.%1$s = element && element?.%1$s ? element?.%1$s.map((x:any) => CamelDefinitionApi.create%2$s(x)) :[];"
, name, getAttributeArrayClass(aValue));
attrs.add(code);
} else if (isAttributeRef(aValue)
&& !getAttributeClass(aValue).equals("SagaActionUriDefinition") // SagaActionUriDefinition is exception
&& !getAttributeClass(aValue).equals("ToDefinition") // exception for ToDefinition (in REST Methods)
&& !getAttributeClass(aValue).equals("ToDynamicDefinition") // exception for ToDynamicDefinition (in REST Methods)
) {
String attributeClass = getAttributeClass(aValue);
String template = attributeClass.equals("ExpressionDefinition")
? " def.%1$s = CamelDefinitionApi.create%2$s(element.%1$s); \n"
: " if (element?.%1$s !== undefined) { \n" +
" def.%1$s = CamelDefinitionApi.create%2$s(element.%1$s); \n" +
" }";
String code = String.format(template, name, getAttributeClass(aValue));
attrs.add(code);
} else {

}
});
String stringToRequired = getStringToRequired(obj, className);
String s2 = stringToRequired.isEmpty() ? "" : "\n" + stringToRequired;
String s3 = attrs.size() > 0 ? "\n" + attrs.stream().collect(Collectors.joining("\n")) : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,11 @@ private void createCamelDefinitions() throws Exception {
writeFileText(targetModel, camelModel.toString());
}

private Map<String, JsonObject> getJsonObjectProperties (JsonObject val) {
Map<String, JsonObject> properties = new LinkedHashMap<>();
val.getMap().keySet().forEach(s -> {
JsonObject value = val.getJsonObject(s);
if (!value.getMap().isEmpty()) {
properties.put(s, val.getJsonObject(s));
} else if (s.equals("expression")){
properties.put(s, JsonObject.of("$ref", "#/items/definitions/org.apache.camel.model.language.ExpressionDefinition"));
}
});
return properties;
}


private String generateModel(String classFullName, JsonObject obj, JsonObject definitions, Map<String, JsonObject> dslMetadata) {
String className = classSimple(classFullName);
Map<String, JsonObject> properties = new LinkedHashMap<>();

obj.getMap().keySet().forEach(key -> {
if (key.equals("oneOf")) {
JsonObject val = obj.getJsonArray("oneOf").getJsonObject(1).getJsonObject("properties");
properties.putAll(getJsonObjectProperties(val));
} else if (key.equals("properties")) {
JsonObject val = obj.getJsonObject("properties");
properties.putAll(getJsonObjectProperties(val));
} else if (key.equals("anyOf")) {
JsonArray vals = obj.getJsonArray("anyOf").getJsonObject(0).getJsonArray("oneOf");
for (int i = 0; i < vals.size(); i++){
JsonObject data = vals.getJsonObject(i);
if (!data.containsKey("not") && data.containsKey("type")) {
JsonObject val = data.getJsonObject("properties");
properties.putAll(getJsonObjectProperties(val));
}
}
}
});
Map<String, JsonObject> properties = getClassProperties(obj);

List<String> required = obj.containsKey("required") ? obj.getJsonArray("required").getList() : List.of();
List<String> attrs = new ArrayList<>();
Expand Down Expand Up @@ -124,19 +94,6 @@ private String generateModel(String classFullName, JsonObject obj, JsonObject de
return String.format(readFileText(modelTemplate), className, s2);
}

private Comparator<String> getComparator(String stepName) {
String json = getMetaModel(stepName);
if (json != null) {
JsonObject props = new JsonObject(json).getJsonObject("properties");
List propsLowerCase = props.getMap().keySet().stream().map(String::toLowerCase).collect(Collectors.toList());
return Comparator.comparing(e -> {
if (propsLowerCase.contains(e.toLowerCase())) return propsLowerCase.indexOf(e.toLowerCase());
else return propsLowerCase.size() + 1;
});
}
return Comparator.comparing(s -> 0);
}

private String getAttributeType(String stepName, JsonObject attribute, boolean required, JsonObject definitions, String generatedValue) {
if (attribute.containsKey("$ref")) {
String classFullName = attribute.getString("$ref");
Expand Down
Loading

0 comments on commit ba9d49d

Please sign in to comment.