Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@
import java.net.URI;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CallFunctionExecutorBuilder implements CallableTaskBuilder<CallFunction> {

private static final String FUNCTION_FILE = "/function.yaml";
private static final String CATALOG_PREFIX = "main/functions/";
private static final Pattern CAPTURE_FUNCTION_NAME =
Pattern.compile("/main/functions/([^/]+)(?:/[^/]+)*/function\\.yaml");

@Override
public CallableTaskFactory init(
CallFunction task, WorkflowDefinition definition, WorkflowMutablePosition position) {
String functionPosition;
String functionName = task.getCall();
Use use = definition.workflow().getUse();
int indexOf = functionName.indexOf('@');
Expand All @@ -61,26 +69,49 @@ public CallableTaskFactory init(
}
catalogEndpoint = loader.uri(catalog.getEndpoint());
}

functionPosition = functionName.substring(0, indexOf);
int sep = functionPosition.indexOf(":");
if (sep < 0) {
throw new IllegalArgumentException(
"Invalid function name "
+ functionName
+ ". It has to be of the format <function name>:<function version>");
}
function =
definition
.resourceLoader()
.loadURI(
WorkflowUtils.concatURI(
catalogEndpoint, pathFromFunctionName(functionName.substring(0, indexOf))),
catalogEndpoint, pathFromFunctionName(functionPosition, sep)),
(n, b) -> from(definition, n, b));
} else if (use != null && use.getFunctions() != null) {
// search for inline function definition
function = use.getFunctions().getAdditionalProperties().get(functionName);
}
if (function == null) {
functionPosition = functionPosition.substring(0, sep);
} else if (use != null
&& use.getFunctions() != null
&& use.getFunctions().getAdditionalProperties().get(functionName) instanceof Task found) {
function = found;
functionPosition = functionName;
} else {
// try to load function if function name is an uri
function =
definition
.resourceLoader()
.loadURI(URI.create(functionName), (n, b) -> from(definition, n, b));
URI functionUri = URI.create(functionName);
function = definition.resourceLoader().loadURI(functionUri, (n, b) -> from(definition, n, b));
String functionPath = functionUri.getPath();
Matcher functionMatcher = CAPTURE_FUNCTION_NAME.matcher(functionPath);
functionPosition =
functionMatcher.find()
? functionMatcher.group(1)
: functionPath.substring(
functionPath.lastIndexOf('/') + 1,
functionPath.endsWith(".yaml") || functionPath.endsWith(".json")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
functionPath.endsWith(".yaml") || functionPath.endsWith(".json")
functionPath.endsWith(".yaml") || functionPath.endsWith(".yml") || functionPath.endsWith(".json")

? functionPath.length() - 5
: functionPath.length());
}

TaskExecutorBuilder<? extends TaskBase> executorBuilder =
definition.application().taskFactory().getTaskExecutor(position, function, definition);
definition
.application()
.taskFactory()
.getTaskExecutor(position.copy().addProperty(functionPosition), function, definition);
FunctionArguments functionArgs = task.getWith();
WorkflowValueResolver<?> args =
functionArgs != null
Expand All @@ -90,18 +121,11 @@ catalogEndpoint, pathFromFunctionName(functionName.substring(0, indexOf))),
return () -> this.build(executorBuilder, args);
}

private String pathFromFunctionName(String functionName) {
int sep = functionName.indexOf(":");
if (sep < 0) {
throw new IllegalArgumentException(
"Invalid function name "
+ functionName
+ ". It has to be of the format <function name>:<function version>");
}
private String pathFromFunctionName(String functionName, int sep) {
StringBuilder sb = new StringBuilder(functionName);
sb.setCharAt(sep, '/');
sb.insert(0, "main/functions/");
sb.append("/function.yaml");
sb.insert(0, CATALOG_PREFIX);
sb.append(FUNCTION_FILE);
return sb.toString();
}

Expand Down
Loading