From dcc0b26bd5d543a5861e112f8c96fa71fc5869c6 Mon Sep 17 00:00:00 2001 From: Mathieu Gabelle Date: Thu, 7 Nov 2024 12:17:41 +0100 Subject: [PATCH] refactor(properties): migrate to v2 properties --- src/main/java/io/kestra/plugin/templates/Example.java | 6 +++--- src/main/java/io/kestra/plugin/templates/Trigger.java | 11 +++++++---- .../java/io/kestra/plugin/templates/ExampleTest.java | 8 +++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/kestra/plugin/templates/Example.java b/src/main/java/io/kestra/plugin/templates/Example.java index 82daa3c..b4d354a 100644 --- a/src/main/java/io/kestra/plugin/templates/Example.java +++ b/src/main/java/io/kestra/plugin/templates/Example.java @@ -1,6 +1,7 @@ package io.kestra.plugin.templates; import io.kestra.core.models.annotations.Plugin; +import io.kestra.core.models.property.Property; import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; import lombok.experimental.SuperBuilder; @@ -33,14 +34,13 @@ public class Example extends Task implements RunnableTask { title = "Short description for this input", description = "Full description of this input" ) - @PluginProperty(dynamic = true) // If the variables will be rendered with template {{ }} - private String format; + private Property format; @Override public Example.Output run(RunContext runContext) throws Exception { Logger logger = runContext.logger(); - String render = runContext.render(format); + String render = runContext.render(format).as(String.class).orElse(""); logger.debug(render); return Output.builder() diff --git a/src/main/java/io/kestra/plugin/templates/Trigger.java b/src/main/java/io/kestra/plugin/templates/Trigger.java index 9355c63..7971aa1 100644 --- a/src/main/java/io/kestra/plugin/templates/Trigger.java +++ b/src/main/java/io/kestra/plugin/templates/Trigger.java @@ -1,8 +1,10 @@ package io.kestra.plugin.templates; +import io.kestra.core.exceptions.IllegalVariableEvaluationException; import io.kestra.core.models.annotations.Plugin; import io.kestra.core.models.conditions.ConditionContext; import io.kestra.core.models.executions.Execution; +import io.kestra.core.models.property.Property; import io.kestra.core.models.triggers.*; import io.kestra.core.runners.RunContext; import io.swagger.v3.oas.annotations.media.Schema; @@ -26,16 +28,17 @@ public class Trigger extends AbstractTrigger implements PollingTriggerInterface, @Builder.Default private final Duration interval = Duration.ofSeconds(60); - protected Double min = 0.5; + protected Property min = Property.of(0.5); @Override - public Optional evaluate(ConditionContext conditionContext, TriggerContext context) { + public Optional evaluate(ConditionContext conditionContext, TriggerContext context) throws IllegalVariableEvaluationException { + RunContext runContext = conditionContext.getRunContext(); + double random = Math.random(); - if (random < this.min) { + if (random < runContext.render(this.min).as(Double.class).orElseThrow()) { return Optional.empty(); } - RunContext runContext = conditionContext.getRunContext(); runContext.logger().info("Will create an execution"); Execution execution = TriggerService.generateExecution( this, diff --git a/src/test/java/io/kestra/plugin/templates/ExampleTest.java b/src/test/java/io/kestra/plugin/templates/ExampleTest.java index 76274cb..fe8ae79 100644 --- a/src/test/java/io/kestra/plugin/templates/ExampleTest.java +++ b/src/test/java/io/kestra/plugin/templates/ExampleTest.java @@ -1,7 +1,7 @@ package io.kestra.plugin.templates; -import com.google.common.collect.ImmutableMap; import io.kestra.core.junit.annotations.KestraTest; +import io.kestra.core.models.property.Property; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; import io.kestra.core.runners.RunContext; @@ -9,6 +9,8 @@ import jakarta.inject.Inject; +import java.util.Map; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -23,10 +25,10 @@ class ExampleTest { @Test void run() throws Exception { - RunContext runContext = runContextFactory.of(ImmutableMap.of("variable", "John Doe")); + RunContext runContext = runContextFactory.of(Map.of("variable", "John Doe")); Example task = Example.builder() - .format("Hello {{ variable }}") + .format(Property.of("Hello {{ variable }}")) .build(); Example.Output runOutput = task.run(runContext);