Skip to content

Commit

Permalink
fix: when an id was null, there was still cases in which we lost
Browse files Browse the repository at this point in the history
branches

Fixes KaotoIO#513
  • Loading branch information
Delawen committed Mar 20, 2023
1 parent cb1c5d3 commit f4532fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,31 @@ public ChoiceFlowStep(final Step step, final KamelPopulator kameletPopulator) {
List<Choice> choices = new LinkedList<>();

if (step.getBranches() != null) {
Branch tentativeOtherwise = null;
for (Branch b : step.getBranches()) {
if (b.getCondition() != null || String.valueOf(b.getIdentifier()).startsWith("when-")
|| choice.getOtherwise() != null) {
|| tentativeOtherwise != null) {
if ("otherwise".equalsIgnoreCase(b.getIdentifier())) {
Branch tmp = tentativeOtherwise;
tentativeOtherwise = setOtherwiseBranch(kameletPopulator, choice, b);
b = tmp;
}
choices.add(processChoice(b, kameletPopulator));
} else {
var otherwise = new Otherwise();
otherwise.setSteps(kameletPopulator.processSteps(b));
choice.setOtherwise(otherwise);
tentativeOtherwise = setOtherwiseBranch(kameletPopulator, choice, b);
}
}
}
choice.setChoice(choices);
}

private static Branch setOtherwiseBranch(KamelPopulator kameletPopulator, SuperChoice choice, Branch b) {
var otherwise = new Otherwise();
otherwise.setSteps(kameletPopulator.processSteps(b));
choice.setOtherwise(otherwise);
return b;
}

private Choice processChoice(final Branch b, final KamelPopulator kameletPopulator) {
Choice choice = new Choice();

Expand All @@ -83,7 +94,7 @@ private Choice processChoice(final Branch b, final KamelPopulator kameletPopulat
@Override
public Map<String, Object> getRepresenterProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put(CHOICE_LABEL, this.getChoice());
properties.put(CHOICE_LABEL, this.getChoice().getRepresenterProperties());
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.kaoto.backend.model.step.Branch;
import io.kaoto.backend.model.step.Step;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -31,7 +31,7 @@ public SuperChoice(Step step, final KamelPopulator kameletPopulator) {

if (step.getBranches() != null) {
for (Branch b : step.getBranches()) {
if (b.getCondition() != null) {
if (b.getCondition() != null || String.valueOf(b.getIdentifier()).startsWith("when-")) {
Choice choice = new Choice();
choice.setSteps(kameletPopulator.processSteps(b));
switch (b.getConditionSyntax()) {
Expand Down Expand Up @@ -66,8 +66,9 @@ protected void processBranches(final Step step, final StepCatalog catalog,
step.setBranches(new LinkedList<>());

if (getChoice() != null) {
int i = 1;
for (var flow : getChoice()) {
Branch branch = createBranch(getChoiceIdentifier(flow), flow.getSteps(), kameletStepParserService);
Branch branch = createBranch(getChoiceIdentifier(flow, i++), flow.getSteps(), kameletStepParserService);
branch.setConditionSyntax(getChoiceConditionSyntax(flow));
branch.setCondition(getChoiceCondition(flow));
kameletStepParserService.setValueOnStepProperty(step,
Expand All @@ -83,13 +84,17 @@ protected void processBranches(final Step step, final StepCatalog catalog,
}
}

private String getChoiceIdentifier(final Choice flow) {
return getChoiceCondition(flow);
private String getChoiceIdentifier(final Choice flow, Integer i) {
String id = getChoiceCondition(flow);
if (id == null || id.isBlank()) {
id = "when-" + i;
}
return id;
}

private String getChoiceCondition(final Choice flow) {
var res = "";
switch (getChoiceConditionSyntax(flow)){
switch (getChoiceConditionSyntax(flow)) {
case JQ:
res = flow.getJq();
break;
Expand Down Expand Up @@ -117,7 +122,7 @@ private Branch.ConditionSyntax getChoiceConditionSyntax(final Choice flow) {

@Override
public Map<String, Object> getRepresenterProperties() {
Map<String, Object> properties = new HashMap<>();
Map<String, Object> properties = new LinkedHashMap<>();
if (this.getChoice() != null) {
properties.put(WHEN_LABEL, this.getChoice());
}
Expand All @@ -129,7 +134,6 @@ public Map<String, Object> getRepresenterProperties() {
}



@Override
protected void assignProperty(final Parameter parameter) {
//We don't have properties
Expand Down

0 comments on commit f4532fb

Please sign in to comment.