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

Move TemplateEngine into FreeMarker implementation #230

Merged
merged 1 commit into from
Dec 13, 2019
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 @@ -21,24 +21,9 @@
import software.amazon.smithy.utils.SmithyBuilder;

/**
* Creates a template engine that always injects default values
* into the data model.
*
* <p>Default values can be overridden per/template by passing in a
* different value in the data model when rendering templates.
*
* <pre>
* {@code
* TemplateEngine myEngine = createMyTemplateEngine();
* TemplateEngine wrappedEngine = DefaultDataTemplateEngine.builder()
* .delegate(myEngine)
* .put("foo", "baz")
* .put("hello", true)
* .build();
* assert(wrappedEngine.renderString("{{ foo }}") == "baz");
* }
* </pre>
* This is deprecated and will be removed in 0.10.0.
*/
@Deprecated
public final class DefaultDataTemplateEngine implements TemplateEngine {
private final TemplateEngine delegate;
private final Map<String, Object> defaultContext;
Expand Down Expand Up @@ -72,6 +57,7 @@ private Map<String, Object> merge(Map<String, Object> map) {
/**
* Builds a new DefaultDataTemplateEngine.
*/
@Deprecated
public static final class Builder {
private TemplateEngine delegate;
private final Map<String, Object> defaultContext = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,19 @@
import java.util.Map;

/**
* Abstraction to renderFile templates using a data model.
* This is deprecated and will be removed in 0.10.0.
*/
@FunctionalInterface
@Deprecated
public interface TemplateEngine {
/**
* Writes a template to the given writer.
*
* @param templatePath Loaded template to render.
* @param out Writer to write to.
* @param dataModel Data model to apply to the template.
*/
void write(String templatePath, Writer out, Map<String, Object> dataModel);

/**
* Renders a template loaded from the given path and returns the result.
*
* @param templatePath Path to a template to load.
* @param dataModel Data model to apply to the template.
* @return Returns the rendered text of the template.
*/
default String render(String templatePath, Map<String, Object> dataModel) {
StringWriter writer = new StringWriter();
write(templatePath, writer, dataModel);
return writer.toString();
}

/**
* Renders a template loaded from the given path and returns the result.
*
* @param templatePath Path to a template to load.
* @return Returns the rendered text of the template.
*/
default String render(String templatePath) {
return render(templatePath, Collections.emptyMap());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.DefaultDataTemplateEngine;
import software.amazon.smithy.codegen.core.TemplateEngine;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.BooleanNode;
Expand All @@ -49,13 +50,24 @@
* @see <a href="https://freemarker.apache.org/">FreeMarker</a>
*/
public final class FreeMarkerEngine implements TemplateEngine {

private final Configuration freeMarkerConfig;
private final Map<String, Object> defaultContext;

/**
* @param freeMarkerConfig FreeMarker configuration.
*/
public FreeMarkerEngine(Configuration freeMarkerConfig) {
this(freeMarkerConfig, Collections.emptyMap());
}

/**
* @param freeMarkerConfig FreeMarker configuration.
* @param defaultContext Default context to pass to each template.
*/
public FreeMarkerEngine(Configuration freeMarkerConfig, Map<String, Object> defaultContext) {
this.freeMarkerConfig = freeMarkerConfig;
this.defaultContext = defaultContext;
}

/**
Expand All @@ -71,21 +83,53 @@ public static Builder builder() {
return new Builder();
}

@Override
/**
* Writes a template to the given writer.
*
* @param templatePath Loaded template to render.
* @param out Writer to write to.
* @param dataModel Data model to apply to the template.
*/
public void write(String templatePath, Writer out, Map<String, Object> dataModel) {
try {
Map<String, Object> merged = new HashMap<>(defaultContext);
merged.putAll(dataModel);
Template template = freeMarkerConfig.getTemplate(templatePath);
template.process(dataModel, out);
template.process(merged, out);
} catch (IOException | TemplateException e) {
throw new CodegenException(String.format(
"Error evaluating FreeMarker template [%s]: %s", templatePath, e.getMessage()), e);
}
}

/**
* Renders a template loaded from the given path and returns the result.
*
* @param templatePath Path to a template to load.
* @param dataModel Data model to apply to the template.
* @return Returns the rendered text of the template.
*/
public String render(String templatePath, Map<String, Object> dataModel) {
StringWriter writer = new StringWriter();
write(templatePath, writer, dataModel);
return writer.toString();
}

/**
* Renders a template loaded from the given path and returns the result.
*
* @param templatePath Path to a template to load.
* @return Returns the rendered text of the template.
*/
public String render(String templatePath) {
return render(templatePath, Collections.emptyMap());
}

/**
* Builds a new FreeMarker template engine.
*/
public static final class Builder implements SmithyBuilder<TemplateEngine> {
public static final class Builder implements SmithyBuilder<FreeMarkerEngine> {

private Configuration config;
private boolean disableObjectWrapper;
private boolean disableHelpers;
Expand All @@ -95,7 +139,7 @@ public static final class Builder implements SmithyBuilder<TemplateEngine> {
private final Map<String, Object> defaultProperties = new HashMap<>();

@Override
public TemplateEngine build() {
public FreeMarkerEngine build() {
if (config == null) {
config = new Configuration(Configuration.VERSION_2_3_28);
}
Expand Down Expand Up @@ -123,10 +167,7 @@ public TemplateEngine build() {
putDefaultProperty("CaseUtils", loadUtilsIntoTemplateModel(CaseUtils.class));
}

return DefaultDataTemplateEngine.builder()
.putAll(defaultProperties)
.delegate(new FreeMarkerEngine(config))
.build();
return new FreeMarkerEngine(config, defaultProperties);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.codegen.core.TemplateEngine;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.utils.MapUtils;

public class FreeMarkerEngineTest {
@Test
public void rendersTemplatesWithObjectWrapper() {
TemplateEngine engine = FreeMarkerEngine.builder()
FreeMarkerEngine engine = FreeMarkerEngine.builder()
.classLoader(getClass())
.build();
Map<String, Object> properties = new HashMap<>();
Expand All @@ -34,7 +33,7 @@ public void rendersTemplatesWithObjectWrapper() {

@Test
public void rendersTemplatesWithUtils() {
TemplateEngine engine = FreeMarkerEngine.builder()
FreeMarkerEngine engine = FreeMarkerEngine.builder()
.classLoader(getClass().getClassLoader())
.putDefaultProperty("snake", "snake_man")
.build();
Expand Down