-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jte-models: Add support to generate Kotlin code (#282)
* jte-models: Add support to generate Kotlin code * Remove unused constructor * Report configuration error when language is not supported * Rename Language enum values to follow project camel case convention * Language configuration is now case sensitive
- Loading branch information
1 parent
9230876
commit 64786c0
Showing
14 changed files
with
303 additions
and
29 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
5 changes: 5 additions & 0 deletions
5
jte-models/src/main/java/gg/jte/models/generator/Language.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,5 @@ | ||
package gg.jte.models.generator; | ||
|
||
public enum Language { | ||
Java, Kotlin | ||
} |
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
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
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
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
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,26 @@ | ||
@import gg.jte.extension.api.* | ||
@import gg.jte.models.generator.ModelConfig | ||
@import java.util.Set | ||
|
||
@param String targetClassName | ||
@param String interfaceName | ||
@param JteConfig config | ||
@param Set<TemplateDescription> templates | ||
@param Iterable<String> imports | ||
@param ModelConfig modelConfig | ||
|
||
@file:Suppress("ktlint") | ||
package ${config.packageName()} | ||
|
||
import gg.jte.TemplateEngine | ||
import gg.jte.models.runtime.* | ||
@for(String imp: imports) | ||
import ${imp} | ||
@endfor | ||
|
||
${modelConfig.implementationAnnotation()} | ||
class ${targetClassName}(private val engine: TemplateEngine) : ${interfaceName} { | ||
@for(TemplateDescription template: templates) | ||
@template.dynamictemplates.kmethod(template = template) | ||
@endfor | ||
} |
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,12 @@ | ||
@import gg.jte.extension.api.* | ||
@import gg.jte.models.generator.Util | ||
|
||
@param TemplateDescription template | ||
|
||
override fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template)}): JteModel { | ||
val paramMap = mapOf( | ||
@for(ParamDescription param: template.params()) | ||
"${param.name()}" to ${param.name()},@endfor | ||
) | ||
return DynamicJteModel(engine, "${template.name()}", paramMap); | ||
} |
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,24 @@ | ||
@import gg.jte.extension.api.* | ||
@import gg.jte.models.generator.ModelConfig | ||
@import java.util.Set | ||
|
||
@param String targetClassName | ||
@param JteConfig config | ||
@param Set<TemplateDescription> templates | ||
@param Iterable<String> imports | ||
@param ModelConfig modelConfig | ||
|
||
@file:Suppress("ktlint") | ||
package ${config.packageName()} | ||
|
||
import gg.jte.models.runtime.* | ||
@for(String imp: imports) | ||
import ${imp} | ||
@endfor | ||
|
||
${modelConfig.interfaceAnnotation()} | ||
interface ${targetClassName} { | ||
@for(TemplateDescription template: templates) | ||
@template.interfacetemplates.kmethod(template = template) | ||
@endfor | ||
} |
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,7 @@ | ||
@import gg.jte.extension.api.* | ||
@import gg.jte.models.generator.Util | ||
|
||
@param TemplateDescription template | ||
|
||
@JteView("${template.name()}") | ||
fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template)}): JteModel |
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 @@ | ||
@import gg.jte.extension.api.* | ||
@import gg.jte.models.generator.ModelConfig | ||
@import java.util.Set | ||
|
||
@param String targetClassName | ||
@param String interfaceName | ||
@param JteConfig config | ||
@param Set<TemplateDescription> templates | ||
@param Iterable<String> imports | ||
@param ModelConfig modelConfig | ||
|
||
@file:Suppress("ktlint") | ||
package ${config.packageName()} | ||
|
||
import gg.jte.models.runtime.* | ||
import gg.jte.ContentType | ||
import gg.jte.TemplateOutput | ||
import gg.jte.html.HtmlInterceptor | ||
import gg.jte.html.HtmlTemplateOutput | ||
@for(String imp: imports) | ||
import ${imp} | ||
@endfor | ||
|
||
${modelConfig.implementationAnnotation()} | ||
class ${targetClassName} : ${interfaceName} { | ||
@for(TemplateDescription template: templates) | ||
@template.statictemplates.kmethod(config = config, template = template) | ||
@endfor | ||
} |
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,17 @@ | ||
@import gg.jte.ContentType | ||
@import gg.jte.extension.api.* | ||
@import gg.jte.models.generator.Util | ||
|
||
@param JteConfig config | ||
@param TemplateDescription template | ||
|
||
!{String outputClass = config.contentType() == ContentType.Html ? "HtmlTemplateOutput" : "TemplateOutput";} | ||
override fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template)}): JteModel { | ||
return StaticJteModel<${outputClass}>( | ||
ContentType.${config.contentType()}, | ||
{ output, interceptor -> ${template.fullyQualifiedClassName()}.render(output, interceptor${Util.paramNames(template)}) }, | ||
"${template.name()}", | ||
"${template.packageName()}", | ||
${template.fullyQualifiedClassName()}.JTE_LINE_INFO | ||
); | ||
} |
72 changes: 72 additions & 0 deletions
72
jte-models/src/test/java/gg/jte/models/generator/TestModelConfig.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,72 @@ | ||
package gg.jte.models.generator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class TestModelConfig { | ||
|
||
@Test | ||
public void configureInterfaceAnnotation() { | ||
var modelConfig = new ModelConfig(Map.of("interfaceAnnotation", "@Deprecated")); | ||
assertEquals(modelConfig.interfaceAnnotation(), "@Deprecated"); | ||
} | ||
|
||
@Test | ||
public void interfaceAnnotationBlankWhenConfigurationNotPresent() { | ||
var modelConfig = new ModelConfig(Map.of()); | ||
assertEquals("", modelConfig.interfaceAnnotation()); | ||
} | ||
|
||
@Test | ||
public void configureImplementationAnnotation() { | ||
var modelConfig = new ModelConfig(Map.of("implementationAnnotation", "@Singleton")); | ||
assertEquals("@Singleton", modelConfig.implementationAnnotation()); | ||
} | ||
|
||
@Test | ||
public void implementationAnnotationNullWhenConfigurationNotPresent() { | ||
var modelConfig = new ModelConfig(Map.of()); | ||
assertEquals("", modelConfig.implementationAnnotation()); | ||
} | ||
|
||
@Test | ||
public void languageConfigurationSupportsJava() { | ||
var modelConfig = new ModelConfig(Map.of("language", "Java")); | ||
assertEquals(modelConfig.language(), Language.Java); | ||
} | ||
|
||
@Test | ||
public void languageConfigurationDefaultsToJava() { | ||
var modelConfig = new ModelConfig(Map.of()); | ||
assertEquals(modelConfig.language(), Language.Java); | ||
} | ||
|
||
@Test | ||
public void languageConfigurationSupportsKotlin() { | ||
var modelConfig = new ModelConfig(Map.of("language", "Kotlin")); | ||
assertEquals(modelConfig.language(), Language.Kotlin); | ||
} | ||
|
||
@Test | ||
public void languageConfigurationIsCaseSensitive() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
new ModelConfig(Map.of("language", "jAvA")).language(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void languageConfigurationFailsWhenLanguageIsNotSupported() { | ||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { | ||
new ModelConfig(Map.of("language", "Ooops")).language(); | ||
}); | ||
|
||
assertEquals( | ||
"JTE ModelExtension 'language' property is not configured correctly (current value is 'Ooops'). Supported values: [Java, Kotlin]", | ||
exception.getMessage() | ||
); | ||
} | ||
} |
Oops, something went wrong.