Skip to content
Open
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 @@ -17,6 +17,7 @@
package org.springframework.ai.template;

import java.util.Map;
import java.util.Set;

import org.springframework.util.Assert;

Expand All @@ -36,4 +37,9 @@ public String apply(String template, Map<String, Object> variables) {
return template;
}

@Override
public Set<String> getRequiredVariables(String template) {
return Set.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@
package org.springframework.ai.template;

import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;

/**
* Renders a template using a given strategy.
*
* @author Thomas Vitale
* @author Sun Yuhan
* @since 1.0.0
*/
public interface TemplateRenderer extends BiFunction<String, Map<String, Object>, String> {

@Override
String apply(String template, Map<String, Object> variables);

Set<String> getRequiredVariables(String template);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -316,6 +317,11 @@ public String apply(String template, Map<String, Object> model) {
return template + " (Rendered by Custom)";
}

@Override
public Set<String> getRequiredVariables(String template) {
return Set.of();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -325,6 +326,11 @@ public String apply(String template, Map<String, Object> model) {
return template + " (Rendered by Custom)";
}

@Override
public Set<String> getRequiredVariables(String template) {
return Set.of();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* is shared between threads.
*
* @author Thomas Vitale
* @author Sun Yuhan
* @since 1.0.0
*/
public class StTemplateRenderer implements TemplateRenderer {
Expand Down Expand Up @@ -110,6 +111,11 @@ public String apply(String template, Map<String, Object> variables) {
return st.render();
}

@Override
public Set<String> getRequiredVariables(String template) {
return getInputVariables(createST(template));
}

private ST createST(String template) {
try {
return new ST(template, this.startDelimiterToken, this.endDelimiterToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;

Expand All @@ -31,6 +32,7 @@
* Unit tests for {@link StTemplateRenderer}.
*
* @author Thomas Vitale
* @author Sun Yuhan
*/
class StTemplateRendererTests {

Expand Down Expand Up @@ -297,4 +299,16 @@ void shouldRenderTemplateWithBuiltInFunctions() {
assertThat(result).isEqualTo("Hello!");
}

/**
* Test whether the required variables can be correctly extracted from the template.
*/
@Test
void shouldCorrectlyExtractedRequiredVariables() {
StTemplateRenderer renderer = StTemplateRenderer.builder().build();
String template = "Person: {name}, Age: {age}";
Set<String> requiredVariables = renderer.getRequiredVariables(template);

assertThat(requiredVariables).contains("name", "age");
}

}