Skip to content

Commit

Permalink
BREAKING CHANGE: move modelOf to ViewUtils (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo authored Aug 10, 2021
1 parent 23ced61 commit 54a76c9
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 34 deletions.
4 changes: 4 additions & 0 deletions src/main/docs/guide/breaks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions views-core/src/main/java/io/micronaut/views/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String, Object> modelOf(@Nullable Object data) {
if (data == null) {
return new HashMap<>(0);
}
if (data instanceof Map) {
return (Map<String, Object>) data;
}
return BeanMap.of(data);
}
/**
* Returns a path with unix style folder
* separators that starts and ends with a "/".
Expand Down
25 changes: 1 addition & 24 deletions views-core/src/main/java/io/micronaut/views/ViewsRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,11 +29,6 @@
*/
public interface ViewsRenderer<T> 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
Expand All @@ -51,19 +43,4 @@ public interface ViewsRenderer<T> 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<String, Object> modelOf(@Nullable T data) {
if (data == null) {
return new HashMap<>(0);
}
if (data instanceof Map) {
return (Map<String, Object>) data;
}
return BeanMap.of(data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> context = modelOf(data);
Map<String, Object> context = ViewUtils.modelOf(data);
String location = viewLocation(viewName);
Template template = freemarkerMicronautConfiguration.getTemplate(location);
try {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> context = modelOf(data);
Map<String, Object> context = ViewUtils.modelOf(data);
BindableRockerModel model = rockerConfiguration.isRelaxed()
? rockerEngine.template(view).relaxedBind(context)
: rockerEngine.template(view).bind(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,7 +108,7 @@ public Writable render(@NonNull String viewName, @Nullable T data, @NonNull Http
ArgumentUtils.requireNonNull("viewName", viewName);

Map<String, Object> ijOverlay = new HashMap<>(1);
Map<String, Object> context = modelOf(data);
Map<String, Object> context = ViewUtils.modelOf(data);
final SoySauce.Renderer renderer = soySauce.newRenderer(new SoyTemplate() {
@Override
public String getTemplateName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> context = modelOf(data);
Map<String, Object> context = ViewUtils.modelOf(data);
final VelocityContext velocityContext = new VelocityContext(context);
render(view, velocityContext, StandardCharsets.UTF_8.name(), writer);
};
Expand Down

0 comments on commit 54a76c9

Please sign in to comment.