Skip to content

Commit

Permalink
fix: boolean conditions in template generator annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
chillleader committed Nov 1, 2024
1 parent 74bd0b9 commit 9990b11
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,37 @@ enum DefaultValueType {
String name();
}

enum EqualsBoolean {
TRUE,
FALSE,
NULL;

public static EqualsBoolean fromBoolean(Boolean value) {
if (value == null) {
return NULL;
} else if (value) {
return TRUE;
} else {
return FALSE;
}
}

public Boolean toBoolean() {
if (this == NULL) {
return null;
} else {
return this == TRUE;
}
}
}

@interface PropertyCondition {
String property();

String equals() default "";

EqualsBoolean equalsBoolean() default EqualsBoolean.NULL;

String[] oneOf() default {};

NestedPropertyCondition[] allMatch() default {};
Expand All @@ -153,6 +179,8 @@ enum DefaultValueType {
/** For string properties */
String equals() default "";

EqualsBoolean equalsBoolean() default EqualsBoolean.NULL;

String[] oneOf() default {};

boolean isActive() default false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.camunda.connector.generator.dsl.PropertyCondition;
import io.camunda.connector.generator.dsl.PropertyConstraints;
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.annotation.TemplateProperty.EqualsBoolean;
import io.camunda.connector.generator.java.annotation.TemplateProperty.NestedPropertyCondition;
import io.camunda.connector.generator.java.util.TemplateGenerationContext;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -88,6 +89,7 @@ private PropertyCondition buildCondition(TemplateProperty propertyAnnotation) {
validateCondition(
conditionAnnotation.property(),
conditionAnnotation.equals(),
conditionAnnotation.equalsBoolean(),
conditionAnnotation.oneOf(),
conditionAnnotation.allMatch());
return transformToCondition(conditionAnnotation);
Expand All @@ -99,6 +101,9 @@ public static PropertyCondition transformToCondition(
if (!conditionAnnotation.equals().isBlank()) {
return new PropertyCondition.Equals(
conditionAnnotation.property(), conditionAnnotation.equals());
} else if (conditionAnnotation.equalsBoolean() != TemplateProperty.EqualsBoolean.NULL) {
return new PropertyCondition.Equals(
conditionAnnotation.property(), conditionAnnotation.equalsBoolean().toBoolean());
} else if (conditionAnnotation.oneOf().length > 0) {
return new PropertyCondition.OneOf(
conditionAnnotation.property(), Arrays.asList(conditionAnnotation.oneOf()));
Expand All @@ -119,11 +124,15 @@ public static PropertyCondition transformToNestedCondition(
validateCondition(
conditionAnnotation.property(),
conditionAnnotation.equals(),
conditionAnnotation.equalsBoolean(),
conditionAnnotation.oneOf(),
new NestedPropertyCondition[] {});
if (!conditionAnnotation.equals().isBlank()) {
return new PropertyCondition.Equals(
conditionAnnotation.property(), conditionAnnotation.equals());
} else if (conditionAnnotation.equalsBoolean() != TemplateProperty.EqualsBoolean.NULL) {
return new PropertyCondition.Equals(
conditionAnnotation.property(), conditionAnnotation.equalsBoolean().toBoolean());
} else if (conditionAnnotation.oneOf().length > 0) {
return new PropertyCondition.OneOf(
conditionAnnotation.property(), Arrays.asList(conditionAnnotation.oneOf()));
Expand All @@ -135,16 +144,26 @@ public static PropertyCondition transformToNestedCondition(
}

private static void validateCondition(
String property, String equals, String[] oneOf, NestedPropertyCondition[] allMatch) {
String property,
String equals,
EqualsBoolean equalsBoolean,
String[] oneOf,
NestedPropertyCondition[] allMatch) {
var equalsSet = !equals.isBlank();
var equalsBooleanSet = !equalsBoolean.equals(TemplateProperty.EqualsBoolean.NULL);
var oneOfSet = oneOf != null && oneOf.length > 0;
var allMatchSet = allMatch != null && allMatch.length > 0;
// equalsBoolean always has a value, so it's not included in the check
// if everything else is not set, we consider it an equalsBoolean condition

if (equalsSet && oneOfSet || equalsSet && allMatchSet || oneOfSet && allMatchSet) {
if (equalsSet && equalsBooleanSet
|| equalsSet && oneOfSet
|| equalsSet && allMatchSet
|| oneOfSet && allMatchSet
|| oneOfSet && equalsBooleanSet
|| allMatchSet && equalsBooleanSet) {
throw new IllegalStateException(
"Condition must have only one of 'equals', 'oneOf', 'isActive', or 'allMatch' set");
"Condition must have only one of 'equals', 'equalsBoolean', 'oneOf', 'isActive', or 'allMatch' set");
}
if (equalsSet && property.isBlank()) {
throw new IllegalStateException("Condition 'equals' must have 'property' set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ void booleanProperty() {
void booleanProperty_dependants() {
var template = generator.generate(MyConnectorFunction.MinimallyAnnotated.class).getFirst();
var dependsOnTrue = getPropertyById("dependsOnBooleanPropertyTrue", template);
assertThat(dependsOnTrue.getCondition()).isEqualTo(new Equals("booleanProperty", "true"));
assertThat(dependsOnTrue.getCondition()).isEqualTo(new Equals("booleanProperty", true));
var dependsOnFalse = getPropertyById("dependsOnBooleanPropertyFalse", template);
assertThat(dependsOnFalse.getCondition()).isEqualTo(new Equals("booleanProperty", "false"));
assertThat(dependsOnFalse.getCondition()).isEqualTo(new Equals("booleanProperty", false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.camunda.connector.generator.java.annotation.TemplateDiscriminatorProperty;
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.annotation.TemplateProperty.DefaultValueType;
import io.camunda.connector.generator.java.annotation.TemplateProperty.EqualsBoolean;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyCondition;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyConstraints;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyType;
Expand Down Expand Up @@ -96,11 +97,17 @@ public record MyConnectorInput(
Boolean booleanProperty,
@TemplateProperty(
id = "dependsOnBooleanPropertyFalse",
condition = @PropertyCondition(property = "booleanProperty", equals = "false"))
condition =
@PropertyCondition(
property = "booleanProperty",
equalsBoolean = EqualsBoolean.FALSE))
String dependsOnBooleanPropertyFalse,
@TemplateProperty(
id = "dependsOnBooleanPropertyTrue",
condition = @PropertyCondition(property = "booleanProperty", equals = "true"))
condition =
@PropertyCondition(
property = "booleanProperty",
equalsBoolean = EqualsBoolean.TRUE))
String dependsOnBooleanPropertyTrue,
@TemplateProperty(
id = "mayBeEmptyOrRegexValidated",
Expand Down

0 comments on commit 9990b11

Please sign in to comment.