From 54a76c907567902a37597399822ec2dc52b6db99 Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Tue, 10 Aug 2021 13:20:13 +0200 Subject: [PATCH] BREAKING CHANGE: move modelOf to ViewUtils (#272) --- src/main/docs/guide/breaks.adoc | 4 +++ .../java/io/micronaut/views/ViewUtils.java | 23 +++++++++++++++++ .../io/micronaut/views/ViewsRenderer.java | 25 +------------------ .../freemarker/FreemarkerViewsRenderer.java | 4 +-- .../handlebars/HandlebarsViewsRenderer.java | 2 +- .../views/pebble/PebbleEngineFactory.java | 5 ++-- .../views/pebble/PebbleViewsRenderer.java | 2 +- .../views/rocker/RockerViewsRenderer.java | 3 ++- .../views/soy/SoySauceViewsRenderer.java | 3 ++- .../thymeleaf/ThymeleafViewsRenderer.java | 2 +- .../views/velocity/VelocityViewsRenderer.java | 2 +- 11 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/main/docs/guide/breaks.adoc b/src/main/docs/guide/breaks.adoc index 6535c4888..ecfc344da 100644 --- a/src/main/docs/guide/breaks.adoc +++ b/src/main/docs/guide/breaks.adoc @@ -6,6 +6,10 @@ This section outlines the breaking changes done in major versions of Micronaut V * api:views.model.ViewsRenderer[] are now typed. Moreover, provided `ViewsRenderer` don't specify `@Produces(MediaType.TEXT_HTML)` and responses content type respect the content type defined for the route. +* `ViewsRenderer::modelOf` method has been moved to `ViewUtils::modelOf` + +* Constant `EXTENSION_SEPARATOR` has been moved from `ViewsRenderer` to `ViewUtils` + == 2.0.0 * The `micronaut-views` dependency is no longer published. Replace the dependency with the one specific to the view implementation being used. diff --git a/views-core/src/main/java/io/micronaut/views/ViewUtils.java b/views-core/src/main/java/io/micronaut/views/ViewUtils.java index 002a9d1d5..9633a5da5 100644 --- a/views-core/src/main/java/io/micronaut/views/ViewUtils.java +++ b/views-core/src/main/java/io/micronaut/views/ViewUtils.java @@ -17,6 +17,10 @@ import io.micronaut.core.annotation.NonNull; import io.micronaut.core.annotation.Nullable; +import io.micronaut.core.beans.BeanMap; + +import java.util.HashMap; +import java.util.Map; /** * Utility methods for views. @@ -25,7 +29,26 @@ * @since 1.1.0 */ public class ViewUtils { + /** + * Extension separator. + */ + public static final String EXTENSION_SEPARATOR = "."; + /** + * Creates a view model for the given data. + * @param data The data + * @return The model + */ + @NonNull + public static Map modelOf(@Nullable Object data) { + if (data == null) { + return new HashMap<>(0); + } + if (data instanceof Map) { + return (Map) data; + } + return BeanMap.of(data); + } /** * Returns a path with unix style folder * separators that starts and ends with a "/". diff --git a/views-core/src/main/java/io/micronaut/views/ViewsRenderer.java b/views-core/src/main/java/io/micronaut/views/ViewsRenderer.java index ea0f59460..257c7c3a9 100644 --- a/views-core/src/main/java/io/micronaut/views/ViewsRenderer.java +++ b/views-core/src/main/java/io/micronaut/views/ViewsRenderer.java @@ -16,13 +16,10 @@ package io.micronaut.views; import io.micronaut.core.annotation.NonNull; -import io.micronaut.core.beans.BeanMap; +import io.micronaut.core.annotation.Nullable; import io.micronaut.core.io.Writable; import io.micronaut.core.order.Ordered; import io.micronaut.http.HttpRequest; -import io.micronaut.core.annotation.Nullable; -import java.util.HashMap; -import java.util.Map; /** * Interface to be implemented by View Engines implementations. @@ -32,11 +29,6 @@ */ public interface ViewsRenderer extends Ordered { - /** - * The extension separator. - */ - String EXTENSION_SEPARATOR = "."; - /** * @param viewName view name to be rendered * @param data response body to render it with a view @@ -51,19 +43,4 @@ public interface ViewsRenderer extends Ordered { */ boolean exists(@NonNull String viewName); - /** - * Creates a view model for the given data. - * @param data The data - * @return The model - */ - default @NonNull Map modelOf(@Nullable T data) { - if (data == null) { - return new HashMap<>(0); - } - if (data instanceof Map) { - return (Map) data; - } - return BeanMap.of(data); - } - } diff --git a/views-freemarker/src/main/java/io/micronaut/views/freemarker/FreemarkerViewsRenderer.java b/views-freemarker/src/main/java/io/micronaut/views/freemarker/FreemarkerViewsRenderer.java index 6afb2b311..15a47aab0 100644 --- a/views-freemarker/src/main/java/io/micronaut/views/freemarker/FreemarkerViewsRenderer.java +++ b/views-freemarker/src/main/java/io/micronaut/views/freemarker/FreemarkerViewsRenderer.java @@ -70,7 +70,7 @@ public FreemarkerViewsRenderer(ViewsConfiguration viewsConfiguration, public Writable render(@NonNull String viewName, @Nullable T data, @NonNull HttpRequest request) { ArgumentUtils.requireNonNull("viewName", viewName); return (writer) -> { - Map context = modelOf(data); + Map context = ViewUtils.modelOf(data); String location = viewLocation(viewName); Template template = freemarkerMicronautConfiguration.getTemplate(location); try { @@ -96,7 +96,7 @@ public boolean exists(@NonNull String view) { private String viewLocation(String name) { return ViewUtils.normalizeFile(name, extension) + - EXTENSION_SEPARATOR + + ViewUtils.EXTENSION_SEPARATOR + extension; } diff --git a/views-handlebars/src/main/java/io/micronaut/views/handlebars/HandlebarsViewsRenderer.java b/views-handlebars/src/main/java/io/micronaut/views/handlebars/HandlebarsViewsRenderer.java index f47a2b57f..577940173 100644 --- a/views-handlebars/src/main/java/io/micronaut/views/handlebars/HandlebarsViewsRenderer.java +++ b/views-handlebars/src/main/java/io/micronaut/views/handlebars/HandlebarsViewsRenderer.java @@ -91,7 +91,7 @@ public boolean exists(@NonNull String viewName) { if (viewName == null) { return false; } - String location = viewLocation(viewName) + EXTENSION_SEPARATOR + extension(); + String location = viewLocation(viewName) + ViewUtils.EXTENSION_SEPARATOR + extension(); return resourceLoader.getResource(location).isPresent(); } diff --git a/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleEngineFactory.java b/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleEngineFactory.java index ac74e3cef..906ca72f6 100644 --- a/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleEngineFactory.java +++ b/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleEngineFactory.java @@ -17,6 +17,8 @@ import java.util.List; import java.util.Optional; + +import io.micronaut.views.ViewUtils; import jakarta.inject.Inject; import jakarta.inject.Singleton; import com.mitchellbosecke.pebble.PebbleEngine; @@ -30,7 +32,6 @@ import io.micronaut.core.annotation.Nullable; import io.micronaut.scheduling.TaskExecutors; import io.micronaut.views.ViewsConfiguration; -import io.micronaut.views.ViewsRenderer; import java.util.concurrent.ExecutorService; import jakarta.inject.Named; @@ -110,7 +111,7 @@ public PebbleEngine create() { } else { Loader loader = new ClasspathLoader(); loader.setPrefix(viewsConfiguration.getFolder()); - loader.setSuffix(ViewsRenderer.EXTENSION_SEPARATOR + configuration.getDefaultExtension()); + loader.setSuffix(ViewUtils.EXTENSION_SEPARATOR + configuration.getDefaultExtension()); builder.loader(loader); } diff --git a/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleViewsRenderer.java b/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleViewsRenderer.java index 344c7ced0..3447082a0 100644 --- a/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleViewsRenderer.java +++ b/views-pebble/src/main/java/io/micronaut/views/pebble/PebbleViewsRenderer.java @@ -50,7 +50,7 @@ public PebbleViewsRenderer(PebbleConfiguration configuration, PebbleEngine engin @Override public Writable render(String name, T data, @NonNull HttpRequest request) { - return (writer) -> engine.getTemplate(ViewUtils.normalizeFile(name, extension)).evaluate(writer, modelOf(data)); + return (writer) -> engine.getTemplate(ViewUtils.normalizeFile(name, extension)).evaluate(writer, ViewUtils.modelOf(data)); } @Override diff --git a/views-rocker/src/main/java/io/micronaut/views/rocker/RockerViewsRenderer.java b/views-rocker/src/main/java/io/micronaut/views/rocker/RockerViewsRenderer.java index 8c4e54569..82b6ece69 100644 --- a/views-rocker/src/main/java/io/micronaut/views/rocker/RockerViewsRenderer.java +++ b/views-rocker/src/main/java/io/micronaut/views/rocker/RockerViewsRenderer.java @@ -19,6 +19,7 @@ import io.micronaut.core.io.Writable; import io.micronaut.core.util.ArgumentUtils; import io.micronaut.http.HttpRequest; +import io.micronaut.views.ViewUtils; import io.micronaut.views.ViewsConfiguration; import io.micronaut.views.ViewsRenderer; import io.micronaut.core.annotation.NonNull; @@ -62,7 +63,7 @@ public RockerViewsRenderer(ViewsConfiguration viewsConfiguration, public Writable render(@NonNull String view, @Nullable T data, @NonNull HttpRequest request) { ArgumentUtils.requireNonNull("view", view); - Map context = modelOf(data); + Map context = ViewUtils.modelOf(data); BindableRockerModel model = rockerConfiguration.isRelaxed() ? rockerEngine.template(view).relaxedBind(context) : rockerEngine.template(view).bind(context); diff --git a/views-soy/src/main/java/io/micronaut/views/soy/SoySauceViewsRenderer.java b/views-soy/src/main/java/io/micronaut/views/soy/SoySauceViewsRenderer.java index e90c1edef..03faada3d 100644 --- a/views-soy/src/main/java/io/micronaut/views/soy/SoySauceViewsRenderer.java +++ b/views-soy/src/main/java/io/micronaut/views/soy/SoySauceViewsRenderer.java @@ -26,6 +26,7 @@ import io.micronaut.core.io.Writable; import io.micronaut.core.util.ArgumentUtils; import io.micronaut.http.HttpRequest; +import io.micronaut.views.ViewUtils; import io.micronaut.views.ViewsConfiguration; import io.micronaut.views.ViewsRenderer; import io.micronaut.views.csp.CspConfiguration; @@ -107,7 +108,7 @@ public Writable render(@NonNull String viewName, @Nullable T data, @NonNull Http ArgumentUtils.requireNonNull("viewName", viewName); Map ijOverlay = new HashMap<>(1); - Map context = modelOf(data); + Map context = ViewUtils.modelOf(data); final SoySauce.Renderer renderer = soySauce.newRenderer(new SoyTemplate() { @Override public String getTemplateName() { diff --git a/views-thymeleaf/src/main/java/io/micronaut/views/thymeleaf/ThymeleafViewsRenderer.java b/views-thymeleaf/src/main/java/io/micronaut/views/thymeleaf/ThymeleafViewsRenderer.java index ff1ec0088..6c30d18b2 100644 --- a/views-thymeleaf/src/main/java/io/micronaut/views/thymeleaf/ThymeleafViewsRenderer.java +++ b/views-thymeleaf/src/main/java/io/micronaut/views/thymeleaf/ThymeleafViewsRenderer.java @@ -75,7 +75,7 @@ public Writable render(@NonNull String viewName, ArgumentUtils.requireNonNull("viewName", viewName); ArgumentUtils.requireNonNull("request", request); return (writer) -> { - IContext context = new WebContext(request, request.getLocale().orElse(Locale.US), modelOf(data)); + IContext context = new WebContext(request, request.getLocale().orElse(Locale.US), ViewUtils.modelOf(data)); render(viewName, context, writer); }; } diff --git a/views-velocity/src/main/java/io/micronaut/views/velocity/VelocityViewsRenderer.java b/views-velocity/src/main/java/io/micronaut/views/velocity/VelocityViewsRenderer.java index 6b15dbaed..817bb26c2 100644 --- a/views-velocity/src/main/java/io/micronaut/views/velocity/VelocityViewsRenderer.java +++ b/views-velocity/src/main/java/io/micronaut/views/velocity/VelocityViewsRenderer.java @@ -74,7 +74,7 @@ public VelocityViewsRenderer(ViewsConfiguration viewsConfiguration, public Writable render(@NonNull String view, @Nullable T data, @NonNull HttpRequest request) { ArgumentUtils.requireNonNull("view", view); return (writer) -> { - Map context = modelOf(data); + Map context = ViewUtils.modelOf(data); final VelocityContext velocityContext = new VelocityContext(context); render(view, velocityContext, StandardCharsets.UTF_8.name(), writer); };