Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to dynamic properties #43

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/io/kestra/plugin/couchbase/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.couchbase.client.java.query.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 @@ -50,7 +51,7 @@ public class Query extends CouchbaseConnection implements RunnableTask<Query.Out

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

@NotNull
Expand All @@ -69,7 +70,7 @@ public Output run(RunContext runContext) throws Exception {
List<Map<String, Object>> rowsAsMap = result.rowsAs(MAP_TYPE_REF);

Output.OutputBuilder outputBuilder = Output.builder().size((long) rowsAsMap.size());
return (switch (fetchType) {
return (switch (runContext.render(fetchType).as(FetchType.class).orElseThrow()) {
case FETCH -> outputBuilder
.rows(rowsAsMap);
case FETCH_ONE -> outputBuilder
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/kestra/plugin/couchbase/QueryInterface.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.plugin.couchbase;

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 Down Expand Up @@ -40,6 +41,5 @@ public interface QueryInterface {
+ "STORE - store all the rows in a file.\n"
+ "NONE - do nothing."
)
@PluginProperty
FetchType getFetchType();
Property<FetchType> getFetchType();
}
7 changes: 4 additions & 3 deletions src/main/java/io/kestra/plugin/couchbase/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.tasks.common.FetchType;
import io.kestra.core.models.triggers.*;
import io.kestra.core.runners.RunContext;
Expand Down Expand Up @@ -33,7 +34,7 @@
code = """
id: couchbase_trigger
namespace: company.team

tasks:
- id: each
type: io.kestra.plugin.core.flow.ForEach
Expand All @@ -42,7 +43,7 @@
- id: return
type: io.kestra.plugin.core.debug.Return
format: "{{ json(taskrun.value) }}"

triggers:
- id: watch
type: io.kestra.plugin.couchbase.Trigger
Expand Down Expand Up @@ -77,7 +78,7 @@ public class Trigger extends AbstractTrigger implements PollingTriggerInterface,

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

@NotNull
@Builder.Default
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/io/kestra/plugin/couchbase/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.codec.RawBinaryTranscoder;
import com.couchbase.client.java.kv.UpsertOptions;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.common.FetchType;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
Expand Down Expand Up @@ -40,7 +41,7 @@ void simpleQuery_AllTypesParsed() throws Exception {

Query query = authentifiedQueryBuilder()
.query("SELECT * FROM " + BUCKET + " USE KEYS 'a-doc'")
.fetchType(FetchType.FETCH_ONE)
.fetchType(Property.of(FetchType.FETCH_ONE))
.build();

Query.Output queryResult = query.run(runContext);
Expand Down Expand Up @@ -73,7 +74,7 @@ void simpleQuery_WorksWithScopeAndCollection() throws Exception {

Query query = authentifiedQueryBuilder()
.query("SELECT * FROM " + BUCKET + ".`" + SCOPE + "`.`" + COLLECTION + "` WHERE c_string='A collection doc'")
.fetchType(FetchType.FETCH_ONE)
.fetchType(Property.of(FetchType.FETCH_ONE))
.build();

Query.Output queryResult = query.run(runContext);
Expand All @@ -95,7 +96,7 @@ void preparedStatement(String firstArg, String secondArg, String parametersJson)

Query query = authentifiedQueryBuilder()
.query("SELECT c_string, c_int FROM " + BUCKET + " WHERE c_string=" + firstArg + " AND c_int=" + secondArg)
.fetchType(FetchType.FETCH_ONE)
.fetchType(Property.of(FetchType.FETCH_ONE))
.parameters(JacksonMapper.toObject(parametersJson))
.build();

Expand All @@ -118,15 +119,15 @@ void simpleQuery_FetchAll() throws Exception {
" \"c_string\" : \"Another Kestra Doc\"" +
"})" +
"RETURNING *")
.fetchType(FetchType.NONE)
.fetchType(Property.of(FetchType.NONE))
.build().run(runContext);

// Only available if adding 'RETURNING *' to insert
assertThat(insertQuery.getSize(), is(1L));

Query query = authentifiedQueryBuilder()
.query("SELECT * FROM " + BUCKET)
.fetchType(FetchType.FETCH)
.fetchType(Property.of(FetchType.FETCH))
.build();

Query.Output queryResult = query.run(runContext);
Expand All @@ -143,7 +144,7 @@ void simpleQuery_FetchAll() throws Exception {
// If we precise field, we get rid of bucket layer in output
query = authentifiedQueryBuilder()
.query("SELECT c_string FROM " + BUCKET)
.fetchType(FetchType.FETCH)
.fetchType(Property.of(FetchType.FETCH))
.build();

queryResult = query.run(runContext);
Expand Down Expand Up @@ -174,7 +175,7 @@ public void binaryData() throws Exception {

Query.Output queryResult = authentifiedQueryBuilder()
.query("SELECT * FROM " + BUCKET + ".`" + SCOPE + "`.`" + COLLECTION + "` USE KEYS 'xml-doc'")
.fetchType(FetchType.FETCH_ONE)
.fetchType(Property.of(FetchType.FETCH_ONE))
.build().run(runContextFactory.of());

// We should query bucket through API to be able to decode binary. Not implemented for now.
Expand All @@ -187,7 +188,7 @@ void simpleQuery_ToInternalStorage() throws Exception {

Query query = authentifiedQueryBuilder()
.query("SELECT * FROM " + BUCKET + ".`" + SCOPE + "`.`" + COLLECTION + "` WHERE c_string='A collection doc'")
.fetchType(FetchType.STORE)
.fetchType(Property.of(FetchType.STORE))
.build();

Query.Output queryResult = query.run(runContext);
Expand Down
Loading