Skip to content

Commit

Permalink
refactor: dynamic properties (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle authored Dec 10, 2024
1 parent 5f1d025 commit ccd1fdd
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 65 deletions.
12 changes: 7 additions & 5 deletions src/main/java/io/kestra/plugin/surrealdb/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.surrealdb.driver.model.QueryResult;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.common.FetchType;
import io.kestra.core.runners.RunContext;
Expand Down Expand Up @@ -37,7 +38,7 @@
code = """
id: surrealdb_query
namespace: company.team
tasks:
- id: select
type: io.kestra.plugin.surrealdb.Query
Expand All @@ -58,10 +59,10 @@ public class Query extends SurrealDBConnection implements RunnableTask<Query.Out

@NotNull
@Builder.Default
protected FetchType fetchType = FetchType.STORE;
protected Property<FetchType> fetchType = Property.of(FetchType.STORE);

@Builder.Default
protected Map<String, String> parameters = new HashMap<>();
protected Property<Map<String, String>> parameters = Property.of(new HashMap<>());

@NotBlank
protected String query;
Expand All @@ -72,15 +73,16 @@ public Query.Output run(RunContext runContext) throws Exception {

String renderedQuery = runContext.render(query);

List<QueryResult<Object>> results = driver.query(renderedQuery, parameters, Object.class);
Map<String, String> parametersValue = runContext.render(parameters).asMap(String.class, String.class).isEmpty() ? new HashMap<>() : runContext.render(parameters).asMap(String.class, String.class);
List<QueryResult<Object>> results = driver.query(renderedQuery, parametersValue, Object.class);

Query.Output.OutputBuilder outputBuilder = Output.builder().size(results.stream()
.mapToLong(result -> (long) result.getResult().size())
.sum());

super.disconnect();

return (switch (fetchType) {
return (switch (runContext.render(fetchType).as(FetchType.class).orElseThrow()) {
case FETCH -> outputBuilder.rows(getResultStream(results).toList());
case FETCH_ONE -> outputBuilder.row(getResultStream(results).findFirst().orElse(null));
case STORE -> outputBuilder.uri(getTempFile(runContext, getResultStream(results).toList()));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/kestra/plugin/surrealdb/QueryInterface.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.plugin.surrealdb;

import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.common.FetchType;
import io.swagger.v3.oas.annotations.media.Schema;

Expand All @@ -17,8 +18,8 @@ public interface QueryInterface {
+ "STORE - store all rows to a file.\n"
+ "NONE - do nothing."
)
@PluginProperty
@NotNull FetchType getFetchType();
@NotNull
Property<FetchType> getFetchType();

@Schema(
title = "Query parameters, can be named parameters.",
Expand All @@ -30,8 +31,7 @@ public interface QueryInterface {
Map.class
}
)
@PluginProperty(dynamic = true)
Map<String, String> getParameters();
Property<Map<String, String>> getParameters();

@Schema(
title = "SurrealQL query to execute."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.surrealdb.connection.SurrealWebSocketConnection;
import com.surrealdb.driver.SyncSurrealDriver;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.runners.RunContext;
import lombok.*;
Expand All @@ -20,7 +21,7 @@
public abstract class SurrealDBConnection extends Task implements SurrealDBConnectionInterface {

@Builder.Default
private boolean useTls = false;
private Property<Boolean> useTls = Property.of(false);

@Positive
@Builder.Default
Expand All @@ -29,9 +30,9 @@ public abstract class SurrealDBConnection extends Task implements SurrealDBConne
@NotBlank
private String host;

private String username;
private Property<String> username;

private String password;
private Property<String> password;

@NotBlank
private String namespace;
Expand All @@ -45,14 +46,14 @@ public abstract class SurrealDBConnection extends Task implements SurrealDBConne

private SurrealConnection connection;

protected SyncSurrealDriver connect(RunContext context) throws IllegalVariableEvaluationException {
SurrealWebSocketConnection connection = new SurrealWebSocketConnection(context.render(host), port, useTls);
protected SyncSurrealDriver connect(RunContext runContext) throws IllegalVariableEvaluationException {
SurrealWebSocketConnection connection = new SurrealWebSocketConnection(runContext.render(host), port, runContext.render(useTls).as(Boolean.class).orElseThrow());
connection.connect(connectionTimeout);

SyncSurrealDriver driver = new SyncSurrealDriver(connection);

signIn(driver, context);
useDatabase(driver, context);
signIn(driver, runContext);
useDatabase(driver, runContext);

return driver;
}
Expand All @@ -69,7 +70,7 @@ private void useDatabase(SyncSurrealDriver driver, RunContext context) throws Il

private void signIn(SyncSurrealDriver driver, RunContext context) throws IllegalVariableEvaluationException {
if (username != null && password != null) {
driver.signIn(context.render(username), context.render(password));
driver.signIn(context.render(username).as(String.class).orElseThrow(), context.render(password).as(String.class).orElseThrow());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.plugin.surrealdb;

import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.validation.constraints.NotBlank;
Expand All @@ -12,8 +13,7 @@ public interface SurrealDBConnectionInterface {
@Schema(
title = "Specify whether to use TLS for connection. Default is `false`."
)
@PluginProperty
boolean isUseTls();
Property<Boolean> getUseTls();

@Schema(
title = "Connection timeout. Default is `60` seconds."
Expand All @@ -39,14 +39,12 @@ public interface SurrealDBConnectionInterface {
@Schema(
title = "Plaintext authentication username."
)
@PluginProperty(dynamic = true)
String getUsername();
Property<String> getUsername();

@Schema(
title = "Plaintext authentication password."
)
@PluginProperty(dynamic = true)
String getPassword();
Property<String> getPassword();

@Schema(
title = "Connection namespace."
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/io/kestra/plugin/surrealdb/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.executions.ExecutionTrigger;
import io.kestra.core.models.flows.State;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.common.FetchType;
import io.kestra.core.models.triggers.AbstractTrigger;
import io.kestra.core.models.triggers.PollingTriggerInterface;
Expand Down Expand Up @@ -40,7 +41,7 @@
code = """
id: surrealdb_trigger
namespace: company.team
tasks:
- id: each
type: io.kestra.plugin.core.flow.ForEach
Expand All @@ -49,7 +50,7 @@
- id: return
type: io.kestra.plugin.core.debug.Return
format: "{{ json(taskrun.value) }}"
triggers:
- id: watch
type: io.kestra.plugin.surrealdb.Trigger
Expand All @@ -69,7 +70,7 @@
public class Trigger extends AbstractTrigger implements PollingTriggerInterface, SurrealDBConnectionInterface, QueryInterface {

@Builder.Default
private boolean useTls = false;
private Property<Boolean> useTls = Property.of(false);

@Positive
@Builder.Default
Expand All @@ -78,9 +79,9 @@ public class Trigger extends AbstractTrigger implements PollingTriggerInterface,
@NotBlank
private String host;

private String username;
private Property<String> username;

private String password;
private Property<String> password;

@NotBlank
private String namespace;
Expand All @@ -94,10 +95,10 @@ public class Trigger extends AbstractTrigger implements PollingTriggerInterface,

@NotNull
@Builder.Default
protected FetchType fetchType = FetchType.STORE;
protected Property<FetchType> fetchType = Property.of(FetchType.STORE);

@Builder.Default
protected Map<String, String> parameters = new HashMap<>();
protected Property<Map<String, String>> parameters = Property.of(new HashMap<>());

@NotBlank
protected String query;
Expand Down
Loading

0 comments on commit ccd1fdd

Please sign in to comment.