Skip to content

Commit

Permalink
Move RewriteMapProvider example to docs examples and add test. (#565)
Browse files Browse the repository at this point in the history
* Move `RewriteMapProvider` example to docs examples and add test.

closes #202

* fix code smell
  • Loading branch information
wetted committed Jul 12, 2023
1 parent 510646b commit 71083f7
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 51 deletions.
52 changes: 1 addition & 51 deletions src/main/docs/guide/views/templates/soy/soyfeaturesrenaming.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,58 +53,8 @@ must be a string literal (variables cannot be used):
--srcs [...templates...];
The last step is to provide the renaming map to Micronaut Views Soy:
```java
@Singleton
public class RewriteMapProvider implements SoyNamingMapProvider {
/** Filename for the JSON class renaming map. */
private static final String RENAMING_MAP_NAME = "renaming-map.json";

/** Naming map to use for classes. */
private static SoyCssRenamingMap CSS_RENAMING_MAP = null;
/**
* Load JSON embedded in a JAR resource into a naming map for Soy rendering.
*
* @param mapPath URL to the JAR resource we should load.
*/
@Nullable
private static Map<String, String> loadMapFromJSON(URL mapPath) {
try {
return new ObjectMapper().readValue(mapPath, new TypeReference<Map<String, String>>() { });
} catch (Throwable thr) {
//handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson
throw new RuntimeException(thr);
}
}
static {
final URL mapPath =
SoyRenderConfigProvider.class.getClassLoader().getResource(RENAMING_MAP_NAME);
if (mapPath != null) {
// load the renaming map
final Map<String, String> cssRenamingMap = loadMapFromJSON(mapPath);
if (renamingMapRaw != null) {
CSS_RENAMING_MAP = new SoyCssRenamingMap() {
@Nullable @Override
public String get(@NotNull String className) {
// (or whatever logic you need to rewrite the class)
return cssRenamingMap.get(className);
}
};
}
}
}
/**
* Provide a CSS renaming map to Soy/Micronaut.
*
* @return Inflated Soy CSS renaming map.
*/
@Nullable @Override public SoyCssRenamingMap cssRenamingMap() {
return CSS_RENAMING_MAP;
}
}
```
snippet::io.micronaut.docs.soy.RewriteMapProvider[tags="clazz"]

Then, your output will be renamed. Referencing the Soy template sample above, output would look something like this:
```html
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.micronaut.docs.soy

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.template.soy.shared.SoyCssRenamingMap
import io.micronaut.context.annotation.Requires
import io.micronaut.core.annotation.Nullable
import io.micronaut.views.soy.SoyNamingMapProvider
import jakarta.inject.Singleton

import javax.validation.constraints.NotNull

@Requires(property = "spec.name", value = "RewriteMapProviderSpec")
//tag::clazz[]
@Singleton
class RewriteMapProvider implements SoyNamingMapProvider {
/** Filename for the JSON class renaming map. */
private static final String RENAMING_MAP_NAME = "styles/renaming-map.json"

/** Naming map to use for classes. */
private static SoyCssRenamingMap CSS_RENAMING_MAP = null

/**
* Load JSON embedded in a JAR resource into a naming map for Soy rendering.
*
* @param mapPath URL to the JAR resource we should load.
*/
@Nullable
private static Map<String, String> loadMapFromJSON(URL mapPath) {
try {
return new ObjectMapper().readValue(mapPath, new TypeReference<Map<String, String>>() { })
} catch (Throwable thr) {
//handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson
throw new RuntimeException(thr)
}
}

static {
final URL mapPath =
RewriteMapProvider.class.getClassLoader().getResource(RENAMING_MAP_NAME)
if (mapPath != null) {
// load the renaming map
final Map<String, String> cssRenamingMap = loadMapFromJSON(mapPath)
if (cssRenamingMap != null) {
CSS_RENAMING_MAP = new SoyCssRenamingMap() {
@Nullable @Override
public String get(@NotNull String className) {
// (or whatever logic you need to rewrite the class)
return cssRenamingMap.get(className)
}
}
}
}
}

/**
* Provide a CSS renaming map to Soy/Micronaut.
*
* @return Inflated Soy CSS renaming map.
*/
@Nullable @Override SoyCssRenamingMap cssRenamingMap() {
return CSS_RENAMING_MAP
}
}
//end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.micronaut.docs.soy

import com.google.template.soy.shared.SoyCssRenamingMap
import io.micronaut.context.BeanContext
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.micronaut.views.soy.SoyNamingMapProvider
import jakarta.inject.Inject
import spock.lang.Specification

@Property(name = "spec.name", value = "RewriteMapProviderSpec")
@MicronautTest
class RewriteMapProviderSpec extends Specification {

@Inject
BeanContext beanContext;

def "test read renaming map file"() {
when:
SoyNamingMapProvider rewriteMapProvider = beanContext.getBean(SoyNamingMapProvider.class);

then:
rewriteMapProvider
SoyCssRenamingMap soyCssRenamingMap = rewriteMapProvider.cssRenamingMap();
soyCssRenamingMap.get('dialog') == 'a'
soyCssRenamingMap.get('content') == 'b'
soyCssRenamingMap.get('title') == 'c'
}
}
5 changes: 5 additions & 0 deletions test-suite-groovy/src/test/resources/styles/renaming-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dialog": "a",
"content": "b",
"title": "c"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.micronaut.docs.soy

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.template.soy.shared.SoyCssRenamingMap
import io.micronaut.context.annotation.Requires
import io.micronaut.core.annotation.Nullable
import io.micronaut.views.soy.SoyNamingMapProvider
import jakarta.inject.Singleton
import java.net.URL

@Requires(property = "spec.name", value = "RewriteMapProviderTest")
//tag::clazz[]
@Singleton
class RewriteMapProvider : SoyNamingMapProvider {

/**
* Provide a CSS renaming map to Soy/Micronaut.
*
* @return Inflated Soy CSS renaming map.
*/
@Nullable
override fun cssRenamingMap(): SoyCssRenamingMap {
return CSS_RENAMING_MAP!!
}

companion object {
/** Filename for the JSON class renaming map. */
private const val RENAMING_MAP_NAME = "styles/renaming-map.json"

/** Naming map to use for classes. */
private var CSS_RENAMING_MAP: SoyCssRenamingMap? = null

/**
* Load JSON embedded in a JAR resource into a naming map for Soy rendering.
*
* @param mapPath URL to the JAR resource we should load.
*/
@Nullable
private fun loadMapFromJSON(mapPath: URL): Map<String?, String?>? {
return try {
ObjectMapper().readValue<Map<String?, String?>?>(
mapPath,
object : TypeReference<Map<String?, String?>?>() {})
} catch (thr: Throwable) {
//handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson
throw RuntimeException(thr)
}
}

init {
val mapPath = RewriteMapProvider::class.java.classLoader.getResource(RENAMING_MAP_NAME)
if (mapPath != null) {
// load the renaming map
val cssRenamingMap = loadMapFromJSON(mapPath)
if (cssRenamingMap != null) {
CSS_RENAMING_MAP =
SoyCssRenamingMap { className -> // (or whatever logic you need to rewrite the class)
cssRenamingMap[className]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.micronaut.docs.soy

import io.micronaut.context.BeanContext
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import io.micronaut.views.soy.SoyNamingMapProvider
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test


@Property(name = "spec.name", value = "RewriteMapProviderTest")
@MicronautTest
internal class RewriteMapProviderTest {

@Inject
lateinit var beanContext: BeanContext

@Test
fun testReadRenamingMapFile() {
val rewriteMapProvider = beanContext.getBean(SoyNamingMapProvider::class.java)

Assertions.assertNotNull(rewriteMapProvider)
val soyCssRenamingMap = rewriteMapProvider.cssRenamingMap()
Assertions.assertEquals("a", soyCssRenamingMap["dialog"])
Assertions.assertEquals("b", soyCssRenamingMap["content"])
Assertions.assertEquals("c", soyCssRenamingMap["title"] )
}
}

5 changes: 5 additions & 0 deletions test-suite-kotlin/src/test/resources/styles/renaming-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dialog": "a",
"content": "b",
"title": "c"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.micronaut.docs.soy;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.template.soy.shared.SoyCssRenamingMap;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.views.soy.SoyNamingMapProvider;
import jakarta.inject.Singleton;

import javax.validation.constraints.NotNull;
import java.net.URL;
import java.util.Map;

@Requires(property = "spec.name", value = "RewriteMapProviderTest")
//tag::clazz[]
@Singleton
public class RewriteMapProvider implements SoyNamingMapProvider {
/** Filename for the JSON class renaming map. */
private static final String RENAMING_MAP_NAME = "styles/renaming-map.json";

/** Naming map to use for classes. */
private static SoyCssRenamingMap CSS_RENAMING_MAP = null;

/**
* Load JSON embedded in a JAR resource into a naming map for Soy rendering.
*
* @param mapPath URL to the JAR resource we should load.
*/
@Nullable
private static Map<String, String> loadMapFromJSON(URL mapPath) {
try {
return new ObjectMapper().readValue(mapPath, new TypeReference<Map<String, String>>() { });
} catch (Throwable thr) {
//handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson
throw new RuntimeException(thr);
}
}

static {
final URL mapPath = RewriteMapProvider.class.getClassLoader().getResource(RENAMING_MAP_NAME);
if (mapPath != null) {
// load the renaming map
final Map<String, String> cssRenamingMap = loadMapFromJSON(mapPath);
if (cssRenamingMap != null) {
CSS_RENAMING_MAP = new SoyCssRenamingMap() {
@Nullable @Override
public String get(@NotNull String className) {
// (or whatever logic you need to rewrite the class)
return cssRenamingMap.get(className);
}
};
}
}
}

/**
* Provide a CSS renaming map to Soy/Micronaut.
*
* @return Inflated Soy CSS renaming map.
*/
@Nullable @Override public SoyCssRenamingMap cssRenamingMap() {
return CSS_RENAMING_MAP;
}
}
//end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.micronaut.docs.soy;

import com.google.template.soy.shared.SoyCssRenamingMap;
import io.micronaut.context.BeanContext;
import io.micronaut.context.annotation.Property;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.micronaut.views.soy.SoyNamingMapProvider;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@Property(name = "spec.name", value = "RewriteMapProviderTest")
@MicronautTest
class RewriteMapProviderTest {

@Inject
BeanContext beanContext;

@Test
void testReadRenamingMapFile() {
SoyNamingMapProvider rewriteMapProvider = beanContext.getBean(SoyNamingMapProvider.class);
assertNotNull(rewriteMapProvider);
SoyCssRenamingMap soyCssRenamingMap = rewriteMapProvider.cssRenamingMap();
assertEquals("a", soyCssRenamingMap.get("dialog"));
assertEquals("b", soyCssRenamingMap.get("content"));
assertEquals("c", soyCssRenamingMap.get("title"));
}
}
5 changes: 5 additions & 0 deletions test-suite/src/test/resources/styles/renaming-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dialog": "a",
"content": "b",
"title": "c"
}

0 comments on commit 71083f7

Please sign in to comment.