Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
fix: expression object support
Browse files Browse the repository at this point in the history
Fixes: #547
Fixes: #548
Fixes: #554
  • Loading branch information
igarashitm committed Mar 22, 2023
1 parent 0b80f6f commit 0dfef25
Show file tree
Hide file tree
Showing 20 changed files with 842 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import io.kaoto.backend.model.deployment.kamelet.expression.Expression;
import io.kaoto.backend.model.step.Step;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -306,4 +308,78 @@ void scriptStep() throws Exception {
assertTrue(groovy.isPresent());
assertEquals("some groovy script", groovy.get().getValue());
}

@Test
void expressionObject() throws Exception {
String yaml = Files.readString(Path.of(
IntegrationsResourceTest.class.getResource(
"../expression-object.yaml")
.toURI()));
var res = given()
.when()
.contentType("text/yaml")
.body(yaml)
.post("?dsl=Camel Route")
.then()
.statusCode(Response.Status.OK.getStatusCode());
String json = res.extract().body().asString();

res = given()
.when()
.contentType("application/json")
.body(json)
.post("?dsl=Camel Route")
.then()
.statusCode(Response.Status.OK.getStatusCode());

var yaml2 = res.extract().body().asString();
System.out.println(yaml2);
List<Object> parsed = new Yaml().load(yaml2);
List<Object> steps = (List<Object>) ((Map)((Map)parsed.get(0)).get("from")).get("steps");
assertEquals(19, steps.size());
var choice = (Map<String, Object>) ((Map<String, Object>) steps.get(0)).get("choice");
var when0 = (Map<String, Object>) ((List<Object>)choice.get("when")).get(0);
assertExpression("choice", "simple", when0);
var delay = (Map<String, Object>) ((Map<String, Object>) steps.get(1)).get("delay");
assertExpression("delay", "simple", delay);
var drouter = (Map<String, Object>) ((Map<String, Object>) steps.get(2)).get("dynamic-router");
assertExpression("dynamic-router", "simple", drouter);
var enrich = (Map<String, Object>) ((Map<String, Object>) steps.get(3)).get("enrich");
assertExpression("enrich", "simple", enrich);
var filter = (Map<String, Object>) ((Map<String, Object>) steps.get(4)).get("filter");
assertExpression("filter", "simple", filter);
var penrich = (Map<String, Object>) ((Map<String, Object>) steps.get(5)).get("poll-enrich");
assertExpression("poll-enrich", "simple", penrich);
var rlist = (Map<String, Object>) ((Map<String, Object>) steps.get(6)).get("recipient-list");
assertExpression("recipient-list", "simple", rlist);
var resequence = (Map<String, Object>) ((Map<String, Object>) steps.get(7)).get("resequence");
assertExpression("resequence", "simple", resequence);
var rslip = (Map<String, Object>) ((Map<String, Object>) steps.get(8)).get("routing-slip");
assertExpression("routing-slip", "simple", rslip);
var script = (Map<String, Object>) ((Map<String, Object>) steps.get(9)).get("script");
assertExpression("script", "groovy", script);
var scall = (Map<String, Object>) ((Map<String, Object>) steps.get(10)).get("service-call");
assertExpression("service-call", "jsonpath", scall);
var sbody = (Map<String, Object>) ((Map<String, Object>) steps.get(11)).get("set-body");
assertExpression("set-body", "constant", sbody);
var sheader = (Map<String, Object>) ((Map<String, Object>) steps.get(12)).get("set-header");
assertExpression("set-header", "jq", sheader);
var sprop = (Map<String, Object>) ((Map<String, Object>) steps.get(13)).get("set-property");
assertExpression("set-property", "jq", sprop);
var sort = (Map<String, Object>) ((Map<String, Object>) steps.get(14)).get("sort");
assertExpression("sort", "simple", sort);
var split = (Map<String, Object>) ((Map<String, Object>) steps.get(15)).get("split");
assertExpression("split", "simple", split);
var throttle = (Map<String, Object>) ((Map<String, Object>) steps.get(16)).get("throttle");
assertExpression("throttle", "simple", throttle);
var transform = (Map<String, Object>) ((Map<String, Object>) steps.get(17)).get("transform");
assertExpression("transform", "jq", transform);
var validate = (Map<String, Object>) ((Map<String, Object>) steps.get(18)).get("validate");
assertExpression("validate", "simple", validate);
}

private void assertExpression(String name, String syntax, Map<String, Object> step) {
var nested = (Map<String, Object>) ((Map<String, Object>)step.get("expression")).get(syntax);
assertEquals(name, nested.get("expression"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
- from:
uri: timer:null
steps:
- choice:
when:
- expression:
simple:
expression: choice
- delay:
expression:
simple:
expression: delay
- dynamic-router:
expression:
simple:
expression: dynamic-router
- enrich:
expression:
simple:
expression: enrich
- filter:
expression:
simple:
expression: filter
- poll-enrich:
expression:
simple:
expression: poll-enrich
- recipient-list:
expression:
simple:
expression: recipient-list
- resequence:
expression:
simple:
expression: resequence
- routing-slip:
expression:
simple:
expression: routing-slip
- script:
expression:
groovy:
expression: script
- service-call:
expression:
jsonpath:
expression: service-call
- set-body:
expression:
constant:
expression: set-body
- set-header:
name: header1
expression:
jq:
expression: set-header
- set-property:
name: prop1
expression:
jq:
expression: set-property
- sort:
expression:
simple:
expression: sort
- split:
expression:
simple:
expression: split
- throttle:
expression:
simple:
expression: throttle
- transform:
expression:
jq:
expression: transform
- validate:
expression:
simple:
expression: validate
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.kaoto.backend.model.deployment.kamelet.Template;
import io.kaoto.backend.model.deployment.kamelet.expression.Expression;
import io.kaoto.backend.model.deployment.kamelet.expression.Script;
import io.kaoto.backend.model.deployment.kamelet.expression.ScriptExpression;
import io.kaoto.backend.model.deployment.kamelet.step.AggregateFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.ChoiceFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.CircuitBreakerFlowStep;
Expand Down Expand Up @@ -507,7 +508,8 @@ public static Expression getExpression(final Step step) {
} else if (CONSTANT.equalsIgnoreCase(p.getId())) {
expression.setConstant(String.valueOf(p.getValue()));
} else if (EXPRESSION.equalsIgnoreCase(p.getId())) {
expression.setExpression((Expression) p.getValue());
Expression nestedExpression = new Expression(p.getValue());
expression.setExpression(nestedExpression);
}
}
expression.setId(step.getStepId());
Expand All @@ -526,8 +528,12 @@ public static Script getScript(final Step step) {
script.setGroovy(String.valueOf(p.getValue()));
} else if (JAVASCRIPT.equalsIgnoreCase(p.getId())) {
script.setJavascript(String.valueOf(p.getValue()));
} else if (EXPRESSION.equalsIgnoreCase(p.getId())) {
ScriptExpression nestedExpression = new ScriptExpression(p.getValue());
script.setExpression(nestedExpression);
}
}
script.setId(step.getStepId());
return script;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.kaoto.backend.model.deployment.kamelet.KameletSpec;
import io.kaoto.backend.model.deployment.kamelet.Template;
import io.kaoto.backend.model.deployment.kamelet.expression.Script;
import io.kaoto.backend.model.deployment.kamelet.expression.ScriptExpression;
import io.kaoto.backend.model.deployment.kamelet.step.AggregateFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.ChoiceFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.CircuitBreakerFlowStep;
Expand Down Expand Up @@ -86,6 +87,7 @@ public class KameletRepresenter extends Representer {
public static final String CONSTANT = "constant";
public static final String GROOVY = "groovy";
public static final String JAVASCRIPT = "javascript";
public static final String EXPRESSION = "expression";
public static final String STEPS = "steps";
public static final String PARAMETERS = "parameters";
public static final String URI = "uri";
Expand Down Expand Up @@ -350,6 +352,14 @@ public Node representData(final Object data) {
}
});

this.multiRepresenters.put(ScriptExpression.class, new RepresentMap() {
@Override
public Node representData(final Object data) {
return representMapping(getTag(data.getClass(), Tag.MAP),
((ScriptExpression) data).getRepresenterProperties(), DumperOptions.FlowStyle.AUTO);
}
});

choice();
filter();
}
Expand Down Expand Up @@ -398,6 +408,8 @@ private Node representConditionBlock(final Object data) {
properties.put(JQ, step.getJq());
} else if (step.getJsonpath() != null && !step.getJsonpath().isEmpty()) {
properties.put(JSONPATH, step.getJsonpath());
} else if (step.getExpression() != null) {
properties.put(EXPRESSION, step.getExpression());
}
return representMapping(getTag(data.getClass(), Tag.MAP), properties,
DumperOptions.FlowStyle.AUTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class KameletStepParserService
public static final Pattern PATTERN = Pattern.compile("[\n|\r]kind:(.+)[\n|\r]", Pattern.CASE_INSENSITIVE);
public static final String GROOVY = "groovy";
public static final String JAVASCRIPT = "javascript";
public static final String EXPRESSION = "expression";
private final Logger log =
Logger.getLogger(KameletStepParserService.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.util.Map;

@JsonPropertyOrder({"name", "constant", "simple", "jq", "jsonpath"})
@JsonPropertyOrder({"name", "constant", "simple", "jq", "jsonpath", "expression"})
public class Expression extends EIPStep {
public static final String CONSTANT_LABEL = KameletRepresenter.CONSTANT;
public static final String SIMPLE_LABEL = KameletRepresenter.SIMPLE;
Expand All @@ -30,7 +30,7 @@ public class Expression extends EIPStep {
private Object simple;

private Object jsonpath;
private String jq;
private Object jq;

private String name;

Expand All @@ -45,7 +45,7 @@ public Expression(
final @JsonProperty(EXPRESSION_LABEL) Expression expression,
final @JsonProperty(CONSTANT_LABEL) Object constant,
final @JsonProperty(SIMPLE_LABEL) Object simple,
final @JsonProperty(JQ_LABEL) String jq,
final @JsonProperty(JQ_LABEL) Object jq,
final @JsonProperty(NAME_LABEL) String name,
final @JsonProperty(RESULT_TYPE) String resultType,
final @JsonProperty(RESULT_TYPE2) String resultType2,
Expand Down Expand Up @@ -85,16 +85,16 @@ protected void setProperties(final Object obj) {

protected void setPropertiesFromMap(final Map map, final Expression expression) {
if (map.containsKey(CONSTANT_LABEL) && map.get(CONSTANT_LABEL) != null) {
expression.setConstant(String.valueOf(map.get(CONSTANT_LABEL)));
expression.setConstant(parseLang(map.get(CONSTANT_LABEL)));
}
if (map.containsKey(SIMPLE_LABEL) && map.get(SIMPLE_LABEL) != null) {
expression.setSimple(String.valueOf(map.get(SIMPLE_LABEL)));
expression.setSimple(parseLang(map.get(SIMPLE_LABEL)));
}
if (map.containsKey(JQ_LABEL) && map.get(JQ_LABEL) != null) {
expression.setJq(String.valueOf(map.get(JQ_LABEL)));
expression.setJq(parseLang(map.get(JQ_LABEL)));
}
if (map.containsKey(JSON_PATH_LABEL) && map.get(JSON_PATH_LABEL) != null) {
expression.setJsonpath(map.get(JSON_PATH_LABEL));
expression.setJsonpath(parseLang(map.get(JSON_PATH_LABEL)));
}
if (map.containsKey(NAME_LABEL) && map.get(NAME_LABEL) != null) {
expression.setName(String.valueOf(map.get(NAME_LABEL)));
Expand All @@ -112,6 +112,13 @@ protected void setPropertiesFromMap(final Map map, final Expression expression)
}
}

private Object parseLang(Object lang) {
if (lang instanceof Map) {
return lang;
}
return String.valueOf(lang);
}

@Override
protected void assignAttribute(final Parameter parameter) {
switch (parameter.getId()) {
Expand All @@ -122,7 +129,7 @@ protected void assignAttribute(final Parameter parameter) {
this.setSimple(parameter.getValue());
break;
case JQ_LABEL:
this.setJq(String.valueOf(parameter.getValue()));
this.setJq(parameter.getValue());
break;
case JSON_PATH_LABEL:
this.setJsonpath(parameter.getValue());
Expand All @@ -131,7 +138,8 @@ protected void assignAttribute(final Parameter parameter) {
this.setName(String.valueOf(parameter.getValue()));
break;
case EXPRESSION_LABEL:
this.setExpression((Expression) parameter.getValue());
Expression nestedExpression = new Expression(parameter.getValue());
this.setExpression(nestedExpression);
break;
case RESULT_TYPE:
case RESULT_TYPE2:
Expand Down Expand Up @@ -193,7 +201,11 @@ public Map<String, Object> getRepresenterProperties() {
}

if (this.getJq() != null) {
properties.put(JQ_LABEL, this.getJq());
if (this.getJq() instanceof Jq jq) {
properties.put(JQ_LABEL, jq.getRepresenterProperties());
} else {
properties.put(JQ_LABEL, this.getJq());
}
}

if (this.getJsonpath() != null) {
Expand Down Expand Up @@ -278,11 +290,15 @@ public void setJsonpath(Object jsonpath) {
}
}

public String getJq() {
public Object getJq() {
return jq;
}

public void setJq(final String jq) {
this.jq = jq;
public void setJq(final Object jq) {
if (jq instanceof Map map) {
this.jq = new Jq(map);
} else {
this.jq = jq;
}
}
}
Loading

0 comments on commit 0dfef25

Please sign in to comment.