Skip to content

Commit

Permalink
refactor(properties): migrate to v2 properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle committed Nov 7, 2024
1 parent 94c0558 commit f3b2c06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/main/java/io/kestra/plugin/templates/Example.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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;
import org.apache.commons.lang3.StringUtils;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.runners.RunContext;
Expand Down Expand Up @@ -33,14 +33,13 @@ public class Example extends Task implements RunnableTask<Example.Output> {
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<String> 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()
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/kestra/plugin/templates/Trigger.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Double> min = Property.of(0.5);

@Override
public Optional<Execution> evaluate(ConditionContext conditionContext, TriggerContext context) {
public Optional<Execution> 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,
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/io/kestra/plugin/templates/ExampleTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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;
import io.kestra.core.runners.RunContextFactory;

import jakarta.inject.Inject;

import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

Expand All @@ -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(new Property<>("Hello {{ variable }}"))
.build();

Example.Output runOutput = task.run(runContext);
Expand Down

0 comments on commit f3b2c06

Please sign in to comment.