Skip to content

Commit

Permalink
feat(core): add URI as input type
Browse files Browse the repository at this point in the history
close #758
  • Loading branch information
tchiotludo committed Sep 30, 2022
1 parent 6649a42 commit 9df2050
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/io/kestra/core/models/flows/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ public enum Type {
DURATION,
FILE,
JSON,
URI,
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/io/kestra/core/runners/RunnerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Singleton
public class RunnerUtils {
public static final Pattern URI_PATTERN = Pattern.compile("^[a-z]+:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$");

@Inject
@Named(QueueFactoryInterface.EXECUTION_NAMED)
protected QueueInterface<Execution> executionQueue;
Expand Down Expand Up @@ -211,6 +215,17 @@ public Map<String, Object> typedInputs(Flow flow, Execution execution, Map<Strin
throw new MissingRequiredInput("Invalid JSON format for '" + input.getName() + "' for '" + current + "' with error " + e.getMessage(), e);
}

case URI:
Matcher matcher = URI_PATTERN.matcher(current);
if (matcher.matches()) {
return Optional.of(new AbstractMap.SimpleEntry<>(
input.getName(),
current
));
} else {
throw new MissingRequiredInput("Invalid URI format for '" + input.getName() + "' for '" + current + "'");
}

default:
throw new MissingRequiredInput("Invalid input type '" + input.getType() + "' for '" + input.getName() + "'");
}
Expand Down
44 changes: 32 additions & 12 deletions core/src/test/java/io/kestra/core/runners/InputsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import io.kestra.core.exceptions.MissingRequiredInput;
import org.junit.jupiter.api.Test;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.flows.State;
Expand Down Expand Up @@ -30,18 +31,19 @@

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class InputsTest extends AbstractMemoryRunnerTest {
public static Map<String, String> inputs = ImmutableMap.of(
"string", "myString",
"int", "42",
"float", "42.42",
"bool", "false",
"instant", "2019-10-06T18:27:49Z",
"date", "2019-10-06",
"time", "18:27:49",
"duration", "PT5M6S",
"file", Objects.requireNonNull(InputsTest.class.getClassLoader().getResource("application.yml")).getPath(),
"json", "{\"a\": \"b\"}"
);
public static Map<String, String> inputs = ImmutableMap.<String, String>builder()
.put("string", "myString")
.put("int", "42")
.put("float", "42.42")
.put("bool", "false")
.put("instant", "2019-10-06T18:27:49Z")
.put("date", "2019-10-06")
.put("time", "18:27:49")
.put("duration", "PT5M6S")
.put("file", Objects.requireNonNull(InputsTest.class.getClassLoader().getResource("application.yml")).getPath())
.put("json", "{\"a\": \"b\"}")
.put("uri", "https://www.google.com")
.build();

@Inject
private FlowRepositoryInterface flowRepository;
Expand Down Expand Up @@ -158,4 +160,22 @@ void inputJson() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("json"), is(Map.of("a", "b")));
}

@Test
void inputUri() {
Map<String, Object> typeds = typedInputs(inputs);
assertThat(typeds.get("uri"), is("https://www.google.com"));
}

@Test
void inputFailed() {
HashMap<String, String> map = new HashMap<>(inputs);
map.put("uri", "http:/bla");

MissingRequiredInput e = assertThrows(MissingRequiredInput.class, () -> {
Map<String, Object> typeds = typedInputs(map);
});

assertThat(e.getMessage(), containsString("Invalid URI format"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ void inputsFailed() {
void inputs() {
Flow flow = this.parse("flows/valids/inputs.yaml");

assertThat(flow.getInputs().size(), is(13));
assertThat(flow.getInputs().size(), is(14));
assertThat(flow.getInputs().stream().filter(Input::getRequired).count(), is(6L));
assertThat(flow.getInputs().stream().filter(r -> !r.getRequired()).count(), is(7L));
assertThat(flow.getInputs().stream().filter(r -> !r.getRequired()).count(), is(8L));
assertThat(flow.getInputs().stream().filter(r -> r.getDefaults() != null).count(), is(1L));
}

Expand Down
3 changes: 3 additions & 0 deletions core/src/test/resources/flows/valids/inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ inputs:
- name: json
type: JSON
required: false
- name: uri
type: URI
required: false

tasks:
- id: string
Expand Down

0 comments on commit 9df2050

Please sign in to comment.