Skip to content

Commit

Permalink
feat(core): add some missing input type
Browse files Browse the repository at this point in the history
close #610
  • Loading branch information
tchiotludo committed Jul 3, 2022
1 parent 5064ebe commit 40bd88d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 9 deletions.
7 changes: 6 additions & 1 deletion core/src/main/java/io/kestra/core/models/flows/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public enum Type {
STRING,
INT,
FLOAT,
BOOLEAN,
DATETIME,
FILE
DATE,
TIME,
DURATION,
FILE,
JSON,
}
}
64 changes: 57 additions & 7 deletions core/src/main/java/io/kestra/core/runners/RunnerUtils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.kestra.core.runners;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.ImmutableMap;
import io.micronaut.http.multipart.StreamingFileUpload;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers;
import io.kestra.core.exceptions.MissingRequiredInput;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.flows.Flow;
Expand All @@ -13,16 +10,26 @@
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.repositories.FlowRepositoryInterface;
import io.kestra.core.serializers.JacksonMapper;
import io.kestra.core.services.ConditionService;
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.Await;
import io.kestra.core.utils.IdUtils;
import io.micronaut.http.multipart.StreamingFileUpload;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;

import java.io.File;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.AbstractMap;
import java.util.HashMap;
Expand All @@ -34,9 +41,6 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

@Singleton
public class RunnerUtils {
Expand Down Expand Up @@ -132,6 +136,12 @@ public Map<String, Object> typedInputs(Flow flow, Execution execution, Map<Strin
Float.valueOf(current)
));

case BOOLEAN:
return Optional.of(new AbstractMap.SimpleEntry<String, Object>(
input.getName(),
Boolean.valueOf(current)
));

case DATETIME:
try {
return Optional.of(new AbstractMap.SimpleEntry<String, Object>(
Expand All @@ -142,6 +152,36 @@ public Map<String, Object> typedInputs(Flow flow, Execution execution, Map<Strin
throw new MissingRequiredInput("Invalid DATETIME format for '" + input.getName() + "' for '" + current + "' with error " + e.getMessage(), e);
}

case DATE:
try {
return Optional.of(new AbstractMap.SimpleEntry<String, Object>(
input.getName(),
LocalDate.parse(current)
));
} catch (DateTimeParseException e) {
throw new MissingRequiredInput("Invalid DATE format for '" + input.getName() + "' for '" + current + "' with error " + e.getMessage(), e);
}

case TIME:
try {
return Optional.of(new AbstractMap.SimpleEntry<String, Object>(
input.getName(),
LocalTime.parse(current)
));
} catch (DateTimeParseException e) {
throw new MissingRequiredInput("Invalid TIME format for '" + input.getName() + "' for '" + current + "' with error " + e.getMessage(), e);
}

case DURATION:
try {
return Optional.of(new AbstractMap.SimpleEntry<String, Object>(
input.getName(),
Duration.parse(current)
));
} catch (DateTimeParseException e) {
throw new MissingRequiredInput("Invalid DURATION format for '" + input.getName() + "' for '" + current + "' with error " + e.getMessage(), e);
}

case FILE:
try {
URI uri = URI.create(current.replace(File.separator, "/"));
Expand All @@ -161,6 +201,16 @@ public Map<String, Object> typedInputs(Flow flow, Execution execution, Map<Strin
throw new MissingRequiredInput("Invalid input arguments for file on input '" + input.getName() + "'", e);
}

case JSON:
try {
return Optional.of(new AbstractMap.SimpleEntry<>(
input.getName(),
JacksonMapper.toObject(current)
));
} catch (JsonProcessingException e) {
throw new MissingRequiredInput("Invalid JSON format for '" + input.getName() + "' for '" + current + "' with error " + e.getMessage(), e);
}

default:
throw new MissingRequiredInput("Invalid input type '" + input.getType() + "' for '" + input.getName() + "'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public static Map<String, Object> toMap(String json) throws JsonProcessingExcept
return MAPPER.readValue(json, TYPE_REFERENCE);
}

private static final TypeReference<Object> TYPE_REFERENCE_OBJECT = new TypeReference<>() {};

public static Object toObject(String json) throws JsonProcessingException {
return MAPPER.readValue(json, TYPE_REFERENCE_OBJECT);
}

public static <T> String log(T Object) {
try {
return YAML_MAPPER.writeValueAsString(Object);
Expand Down
40 changes: 39 additions & 1 deletion core/src/test/java/io/kestra/core/runners/InputsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -31,8 +34,13 @@ public class InputsTest extends AbstractMemoryRunnerTest {
"string", "myString",
"int", "42",
"float", "42.42",
"bool", "false",
"instant", "2019-10-06T18:27:49Z",
"file", Objects.requireNonNull(InputsTest.class.getClassLoader().getResource("application.yml")).getPath()
"date", "2019-10-06",
"time", "18:27:49",
"duration", "PT5M6S",
"file", Objects.requireNonNull(InputsTest.class.getClassLoader().getResource("application.yml")).getPath(),
"json", "{\"a\": \"b\"}"
);

@Inject
Expand Down Expand Up @@ -78,6 +86,12 @@ void inputFloat() {
assertThat(typeds.get("float"), is(42.42F));
}

@Test
void inputBool() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("bool"), is(false));
}

@Test
void inputInstant() {
Map<String, Object> typeds = typedInputs(inputs);
Expand All @@ -90,6 +104,24 @@ void inputInstantDefaults() {
assertThat(typeds.get("instantDefaults"), is(Instant.parse("2013-08-09T14:19:00Z")));
}

@Test
void inputDate() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("date"), is(LocalDate.parse("2019-10-06")));
}

@Test
void inputTime() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("time"), is(LocalTime.parse("18:27:49")));
}

@Test
void inputDuration() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("duration"), is(Duration.parse("PT5M6S")));
}

@Test
void inputFile() throws URISyntaxException, IOException {
Map<String, Object> typeds = typedInputs(inputs);
Expand Down Expand Up @@ -120,4 +152,10 @@ void inputFlow() throws TimeoutException {
matchesRegex("kestra:///io/kestra/tests/inputs/executions/.*/inputs/file/application.yml")
);
}

@Test
void inputJson() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("json"), is(Map.of("a", "b")));
}
}
10 changes: 10 additions & 0 deletions core/src/test/resources/flows/valids/inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ inputs:
required: false
- name: int
type: INT
- name: bool
type: BOOLEAN
- name: float
type: FLOAT
- name: instant
type: DATETIME
- name: date
type: DATE
- name: time
type: TIME
- name: duration
type: DURATION
- name: file
type: FILE
- name: optionalFile
Expand All @@ -21,6 +29,8 @@ inputs:
- name: instantDefaults
type: DATETIME
defaults: "2013-08-09T14:19:00Z"
- name: json
type: JSON

tasks:
- id: string
Expand Down

0 comments on commit 40bd88d

Please sign in to comment.