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

Feat/kv store #4217

Merged
merged 3 commits into from
Jul 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static <T> String render(String templateName, Map<String, Object> vars) t

PebbleTemplate compiledTemplate = pebbleEngine.getLiteralTemplate(pebbleTemplate);

Writer writer = new JsonWriter(new StringWriter());
Writer writer = new JsonWriter();
compiledTemplate.evaluate(writer, vars);
String renderer = writer.toString();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.kestra.core.exceptions;

public class ResourceExpiredException extends Exception {
private static final long serialVersionUID = 1L;

public ResourceExpiredException(Throwable e) {
super(e);
}

public ResourceExpiredException(String message) {
super(message);
}

public ResourceExpiredException(String message, Throwable e) {
super(message, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ public String render(String inline) throws IllegalVariableEvaluationException {
return variableRenderer.render(inline, this.variables);
}

/**
* {@inheritDoc}
*/
@Override
public Object renderTyped(String inline) throws IllegalVariableEvaluationException {
return variableRenderer.renderTyped(inline, this.variables);
}

@Override
@SuppressWarnings("unchecked")
public String render(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/io/kestra/core/runners/RunContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public abstract class RunContext {

public abstract String render(String inline) throws IllegalVariableEvaluationException;

public abstract Object renderTyped(String inline) throws IllegalVariableEvaluationException;

public abstract String render(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException;

public abstract List<String> render(List<String> inline) throws IllegalVariableEvaluationException;
Expand Down
64 changes: 40 additions & 24 deletions core/src/main/java/io/kestra/core/runners/VariableRenderer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.kestra.core.runners;

import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.runners.pebble.ExtensionCustomizer;
import io.kestra.core.runners.pebble.JsonWriter;
import io.kestra.core.runners.pebble.PebbleLruCache;
import io.kestra.core.runners.pebble.*;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.Nullable;
Expand Down Expand Up @@ -70,37 +68,53 @@ public String render(String inline, Map<String, Object> variables) throws Illega
return this.render(inline, variables, this.variableConfiguration.getRecursiveRendering());
}

public Object renderTyped(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
return this.render(inline, variables, this.variableConfiguration.getRecursiveRendering(), false);
}

public String render(String inline, Map<String, Object> variables, boolean recursive) throws IllegalVariableEvaluationException {
return (String) this.render(inline, variables, recursive, true);
}

public Object render(Object inline, Map<String, Object> variables, boolean recursive, boolean stringify) throws IllegalVariableEvaluationException {
if (inline == null) {
return null;
}

if (inline.indexOf('{') == -1) {
if (inline instanceof String inlineStr && inlineStr.indexOf('{') == -1) {
// it's not a Pebble template so we short-circuit rendering
return inline;
}

String render = recursive
? renderRecursively(inline, variables)
: renderOnce(inline, variables);
Object render = recursive
? renderRecursively(inline, variables, stringify)
: renderOnce(inline, variables, stringify);

return RAW_PATTERN.matcher(render).replaceAll("$2");
if (render instanceof String renderStr) {
return RAW_PATTERN.matcher(renderStr).replaceAll("$2");
}

return render;
}

public String renderOnce(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
// pre-process raw tags
Matcher rawMatcher = RAW_PATTERN.matcher(inline);
Map<String, String> replacers = new HashMap<>((int) Math.ceil(rawMatcher.groupCount() / 0.75));
String result = replaceRawTags(rawMatcher, replacers);
public Object renderOnce(Object inline, Map<String, Object> variables, boolean stringify) throws IllegalVariableEvaluationException {
Object result = inline;
Map<String, String> replacers = null;
if (inline instanceof String inlineStr) {
// pre-process raw tags
Matcher rawMatcher = RAW_PATTERN.matcher(inlineStr);
replacers = new HashMap<>((int) Math.ceil(rawMatcher.groupCount() / 0.75));
result = replaceRawTags(rawMatcher, replacers);
}

try {
PebbleTemplate compiledTemplate = this.pebbleEngine.getLiteralTemplate(result);
PebbleTemplate compiledTemplate = this.pebbleEngine.getLiteralTemplate((String) result);

Writer writer = new JsonWriter(new StringWriter());
OutputWriter writer = stringify ? new JsonWriter() : new TypedObjectWriter();
compiledTemplate.evaluate(writer, variables);
result = writer.toString();
result = writer.output();
} catch (IOException | PebbleException e) {
String alternativeRender = this.alternativeRender(e, inline, variables);
String alternativeRender = this.alternativeRender(e, (String) inline, variables);
if (alternativeRender == null) {
if (e instanceof PebbleException) {
throw properPebbleException((PebbleException) e);
Expand All @@ -111,8 +125,10 @@ public String renderOnce(String inline, Map<String, Object> variables) throws Il
}
}

// post-process raw tags
result = putBackRawTags(replacers, result);
if (result instanceof String && replacers != null) {
// post-process raw tags
result = putBackRawTags(replacers, (String) result);
}

return result;
}
Expand Down Expand Up @@ -144,21 +160,21 @@ private static String replaceRawTags(Matcher rawMatcher, Map<String, String> rep
});
}

public String renderRecursively(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
return this.renderRecursively(0, inline, variables);
public Object renderRecursively(Object inline, Map<String, Object> variables, boolean stringify) throws IllegalVariableEvaluationException {
return this.renderRecursively(0, inline, variables, stringify);
}

private String renderRecursively(int renderingCount, String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
private Object renderRecursively(int renderingCount, Object inline, Map<String, Object> variables, boolean stringify) throws IllegalVariableEvaluationException {
if (renderingCount > MAX_RENDERING_AMOUNT) {
throw new IllegalVariableEvaluationException("Too many rendering attempts");
}

String result = this.renderOnce(inline, variables);
Object result = this.renderOnce(inline, variables, stringify);
if (result.equals(inline)) {
return result;
}

return renderRecursively(++renderingCount, result, variables);
return renderRecursively(++renderingCount, result, variables, stringify);
}

public Map<String, Object> render(Map<String, Object> in, Map<String, Object> variables) throws IllegalVariableEvaluationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class Extension extends AbstractExtension {
@Inject
private SecretFunction secretFunction;

@Inject
private KvFunction kvFunction;

@Inject
private ReadFileFunction readFileFunction;

Expand Down Expand Up @@ -105,6 +108,7 @@ public Map<String, Function> getFunctions() {
functions.put("json", new JsonFunction());
functions.put("currentEachOutput", new CurrentEachOutputFunction());
functions.put("secret", secretFunction);
functions.put("kv", kvFunction);
functions.put("read", readFileFunction);
if (this.renderFunction != null) {
functions.put("render", renderFunction);
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/io/kestra/core/runners/pebble/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
import java.util.Collection;
import java.util.Map;

public class JsonWriter extends Writer implements SpecializedWriter {
public class JsonWriter extends OutputWriter implements SpecializedWriter {
private static final ObjectMapper MAPPER = JacksonMapper.ofJson();

private final StringWriter stringWriter;

public JsonWriter(StringWriter stringWriter) {
this.stringWriter = stringWriter;
}
private final StringWriter stringWriter = new StringWriter();

@Override
public void writeSpecialized(int i) {
Expand Down Expand Up @@ -93,4 +89,9 @@ public void close() throws IOException {
public String toString() {
return stringWriter.toString();
}

@Override
public Object output() {
return this.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.kestra.core.runners.pebble;

import java.io.Writer;

public abstract class OutputWriter extends Writer {
public abstract Object output();
}
Loading