-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
RewriteMapProvider
example to docs examples and add test. (#565)
* Move `RewriteMapProvider` example to docs examples and add test. closes #202 * fix code smell
- Loading branch information
Showing
10 changed files
with
301 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProvider.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
29 changes: 29 additions & 0 deletions
29
test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProviderSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
test-suite-groovy/src/test/resources/styles/renaming-map.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dialog": "a", | ||
"content": "b", | ||
"title": "c" | ||
} |
65 changes: 65 additions & 0 deletions
65
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
test-suite-kotlin/src/test/resources/styles/renaming-map.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dialog": "a", | ||
"content": "b", | ||
"title": "c" | ||
} |
66 changes: 66 additions & 0 deletions
66
test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
30 changes: 30 additions & 0 deletions
30
test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dialog": "a", | ||
"content": "b", | ||
"title": "c" | ||
} |