Skip to content

Commit

Permalink
Reformat the templates for Kotlin model code (#310)
Browse files Browse the repository at this point in the history
* Reformat the templates for Kotlin model code

- Change import statements to not have an indent. No functional change.
- Remove unused imports. These were likely copied over from the Java template, but are not needed in Kotlin.
- Dynamic methods, which generate a map, now use Kotlin's buildMap() instead of mapOf(). mapOf() produces extra garbage, as it creates a Pair for each entry, whereas buildMap() modifies the underlying map directly.
- Static methods are now defined as an expression. No functional change.
- In static methods, the template class name is no longer fully qualified, as it's provided as an import. No functional change.
- Remove semicolons - idiomatic Kotlin doesn't use them. No functional change.

* fixup! Reformat the templates for Kotlin model code

* fixup! Reformat the templates for Kotlin model code

Add import for Collectors

* fixup! Reformat the templates for Kotlin model code

Restore the TemplateOutput import

* fixup! Reformat the templates for Kotlin model code

* fixup! Reformat the templates for Kotlin model code

Adjust the tests for windows line endings
  • Loading branch information
MariusVolkhart committed Jan 25, 2024
1 parent 0081c0c commit 1a09cd6
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 41 deletions.
3 changes: 1 addition & 2 deletions jte-models/src/main/jte/dynamictemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
@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}
import ${imp}
@endfor

${modelConfig.implementationAnnotation()}
Expand Down
12 changes: 8 additions & 4 deletions jte-models/src/main/jte/dynamictemplates/kmethod.jte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
@param TemplateDescription template

override fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template, false)}): JteModel {
val paramMap = mapOf<String, Any?>(
@if(template.params().isEmpty())
val paramMap = emptyMap<String, Any?>()
@else
val paramMap = buildMap<String, Any?> {
@for(ParamDescription param: template.params())
"${param.name()}" to ${param.name()},@endfor
)
return DynamicJteModel(engine, "${template.name()}", paramMap);
put("${param.name()}", ${param.name()})@endfor
}
@endif
return DynamicJteModel(engine, "${template.name()}", paramMap)
}
1 change: 0 additions & 1 deletion jte-models/src/main/jte/interfacetemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@param Set<TemplateDescription> templates
@param Iterable<String> imports
@param ModelConfig modelConfig

@file:Suppress("ktlint")
package ${config.packageName()}

Expand Down
4 changes: 1 addition & 3 deletions jte-models/src/main/jte/statictemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
@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}
import ${imp}
@endfor

${modelConfig.implementationAnnotation()}
Expand Down
16 changes: 7 additions & 9 deletions jte-models/src/main/jte/statictemplates/kmethod.jte
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
@param TemplateDescription template

!{String outputClass = config.contentType() == ContentType.Html ? "HtmlTemplateOutput" : "TemplateOutput";}
override fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template, false)}): 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
);
}
override fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template, false)}): JteModel = StaticJteModel<${outputClass}>(
ContentType.${config.contentType()},
{ output, interceptor -> ${template.className()}.render(output, interceptor${Util.paramNames(template)}) },
"${template.name()}",
"${template.packageName()}",
${template.className()}.JTE_LINE_INFO
)
152 changes: 130 additions & 22 deletions jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static gg.jte.extension.api.mocks.MockConfig.mockConfig;
import static gg.jte.extension.api.mocks.MockParamDescription.mockParamDescription;
Expand Down Expand Up @@ -219,13 +220,71 @@ public void generatesKotlinWithoutDefaultValueForContentParams() {
);

// Then
generatedPaths.forEach(path -> {
try {
assertThat(Files.readString(path)).contains("fun hello(content: gg.jte.Content): JteModel");
} catch (IOException ex) {
fail("Could not read file " + path, ex);
var actual = generatedPaths.stream().collect(Collectors.toMap(
path -> path.getFileName().toString(),
path -> {
try {
return Files.readString(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
));
var expected = Map.of(
"Templates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
interface Templates {
\s
@JteView("hello.kte")
fun hello(content: gg.jte.Content): JteModel
}"""),
"StaticTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
import gg.jte.ContentType
import gg.jte.TemplateOutput
import gg.jte.html.HtmlTemplateOutput
class StaticTemplates : Templates {
\s
override fun hello(content: gg.jte.Content): JteModel = StaticJteModel<TemplateOutput>(
ContentType.Plain,
{ output, interceptor -> JtehelloGenerated.render(output, interceptor, content) },
"hello.kte",
"test.myktemplates",
JtehelloGenerated.JTE_LINE_INFO
)
}"""),
"DynamicTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.TemplateEngine
import gg.jte.models.runtime.*
class DynamicTemplates(private val engine: TemplateEngine) : Templates {
\s
override fun hello(content: gg.jte.Content): JteModel {
\s
val paramMap = buildMap<String, Any?> {
\s
put("content", content)
}
\s
return DynamicJteModel(engine, "hello.kte", paramMap)
}
}""")
);
assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected);
}

@Test
Expand All @@ -244,23 +303,72 @@ public void generatesKotlinFacadesWithNoParameters() {
);

// Then
generatedPaths.forEach(path -> {
try {
assertThat(Files.readString(path)).contains("fun hello(): JteModel");
} catch (IOException ex) {
fail("Could not read file " + path, ex);
var actual = generatedPaths.stream().collect(Collectors.toMap(
path -> path.getFileName().toString(),
path -> {
try {
return Files.readString(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});

// Dynamic has a map with explicit generics types declared.
// This is relevant for empty maps (templates with no params).
assertThat(generatedPaths).anySatisfy(path -> {
try {
assertThat(Files.readString(path)).contains("val paramMap = mapOf<String, Any?>");
} catch (IOException ex) {
fail("Could not read file " + path, ex);
}
});
));
var expected = Map.of(
"Templates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
interface Templates {
\s
@JteView("hello.kte")
fun hello(): JteModel
}"""),
"StaticTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
import gg.jte.ContentType
import gg.jte.TemplateOutput
import gg.jte.html.HtmlTemplateOutput
class StaticTemplates : Templates {
\s
override fun hello(): JteModel = StaticJteModel<TemplateOutput>(
ContentType.Plain,
{ output, interceptor -> JtehelloGenerated.render(output, interceptor) },
"hello.kte",
"test.myktemplates",
JtehelloGenerated.JTE_LINE_INFO
)
}"""),
"DynamicTemplates.kt", withSystemLineEndings("""
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.TemplateEngine
import gg.jte.models.runtime.*
class DynamicTemplates(private val engine: TemplateEngine) : Templates {
\s
override fun hello(): JteModel {
\s
val paramMap = emptyMap<String, Any?>()
\s
return DynamicJteModel(engine, "hello.kte", paramMap)
}
}""")
);
assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected);
}
}

private static String withSystemLineEndings(String content) {
return content.replaceAll("\n", System.lineSeparator());
}
}

0 comments on commit 1a09cd6

Please sign in to comment.