diff --git a/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngine.java b/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngine.java
index 2f3b817d3e0..53074de7f66 100644
--- a/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngine.java
+++ b/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngine.java
@@ -21,24 +21,9 @@
import software.amazon.smithy.utils.SmithyBuilder;
/**
- * Creates a template engine that always injects default values
- * into the data model.
- *
- *
Default values can be overridden per/template by passing in a
- * different value in the data model when rendering templates.
- *
- *
- * {@code
- * TemplateEngine myEngine = createMyTemplateEngine();
- * TemplateEngine wrappedEngine = DefaultDataTemplateEngine.builder()
- * .delegate(myEngine)
- * .put("foo", "baz")
- * .put("hello", true)
- * .build();
- * assert(wrappedEngine.renderString("{{ foo }}") == "baz");
- * }
- *
+ * 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 defaultContext;
@@ -72,6 +57,7 @@ private Map merge(Map map) {
/**
* Builds a new DefaultDataTemplateEngine.
*/
+ @Deprecated
public static final class Builder {
private TemplateEngine delegate;
private final Map defaultContext = new HashMap<>();
diff --git a/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/TemplateEngine.java b/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/TemplateEngine.java
index 8309b1dbc73..0577555635d 100644
--- a/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/TemplateEngine.java
+++ b/codegen/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/TemplateEngine.java
@@ -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 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 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());
}
diff --git a/codegen/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngineTest.java b/codegen/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngineTest.java
deleted file mode 100644
index b7e9420588b..00000000000
--- a/codegen/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/DefaultDataTemplateEngineTest.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License").
- * You may not use this file except in compliance with the License.
- * A copy of the License is located at
- *
- * http://aws.amazon.com/apache2.0
- *
- * or in the "license" file accompanying this file. This file is distributed
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
- * express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package software.amazon.smithy.codegen.core;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
-
-import java.io.Writer;
-import java.util.HashMap;
-import java.util.Map;
-import org.junit.jupiter.api.Test;
-
-public class DefaultDataTemplateEngineTest {
- @Test
- public void injectsDefaultValues() {
- DefaultDataTemplateEngine engine = DefaultDataTemplateEngine.builder()
- .put("foo", "baz")
- .delegate(new Custom())
- .build();
-
- assertThat(engine.render("foo"), equalTo("baz"));
- assertThat(engine.render("notThere"), equalTo("null"));
- }
-
- @Test
- public void canOverrideDefaults() {
- DefaultDataTemplateEngine engine = DefaultDataTemplateEngine.builder()
- .put("foo", "baz")
- .delegate(new Custom())
- .build();
- Map dataModel = new HashMap<>();
- dataModel.put("foo", "qux");
-
- assertThat(engine.render("foo", dataModel), equalTo("qux"));
- assertThat(engine.render("notThere", dataModel), equalTo("null"));
- }
-
- @Test
- public void canPassInEmptyCustomValues() {
- DefaultDataTemplateEngine engine = DefaultDataTemplateEngine.builder().delegate(new Custom()).build();
- Map dataModel = new HashMap<>();
- dataModel.put("foo", "qux");
-
- assertThat(engine.render("foo", dataModel), equalTo("qux"));
- assertThat(engine.render("notThere", dataModel), equalTo("null"));
- }
-
- private static final class Custom implements TemplateEngine {
- @Override
- public void write(String templatePath, Writer out, Map dataModel) {
- try {
- out.write(String.valueOf(dataModel.get(templatePath)));
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
-}
diff --git a/codegen/smithy-codegen-freemarker/src/main/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngine.java b/codegen/smithy-codegen-freemarker/src/main/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngine.java
index e0bfa3530f0..1afbd28c6eb 100644
--- a/codegen/smithy-codegen-freemarker/src/main/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngine.java
+++ b/codegen/smithy-codegen-freemarker/src/main/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngine.java
@@ -23,7 +23,9 @@
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;
@@ -31,7 +33,6 @@
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;
@@ -49,13 +50,24 @@
* @see FreeMarker
*/
public final class FreeMarkerEngine implements TemplateEngine {
+
private final Configuration freeMarkerConfig;
+ private final Map 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 defaultContext) {
this.freeMarkerConfig = freeMarkerConfig;
+ this.defaultContext = defaultContext;
}
/**
@@ -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 dataModel) {
try {
+ Map 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 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 {
+ public static final class Builder implements SmithyBuilder {
+
private Configuration config;
private boolean disableObjectWrapper;
private boolean disableHelpers;
@@ -95,7 +139,7 @@ public static final class Builder implements SmithyBuilder {
private final Map defaultProperties = new HashMap<>();
@Override
- public TemplateEngine build() {
+ public FreeMarkerEngine build() {
if (config == null) {
config = new Configuration(Configuration.VERSION_2_3_28);
}
@@ -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);
}
/**
diff --git a/codegen/smithy-codegen-freemarker/src/test/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngineTest.java b/codegen/smithy-codegen-freemarker/src/test/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngineTest.java
index cededfa3c16..2b106acb237 100644
--- a/codegen/smithy-codegen-freemarker/src/test/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngineTest.java
+++ b/codegen/smithy-codegen-freemarker/src/test/java/software/amazon/smithy/codegen/freemarker/FreeMarkerEngineTest.java
@@ -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 properties = new HashMap<>();
@@ -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();