From 2b734eebbc800c7cedc2155456278b02a8166bb5 Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Sun, 7 Jan 2024 11:57:11 -0500 Subject: [PATCH 1/6] 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. --- .../src/main/jte/dynamictemplates/kmain.jte | 2 +- .../src/main/jte/dynamictemplates/kmethod.jte | 12 ++++++++---- .../src/main/jte/statictemplates/kmain.jte | 4 +--- .../src/main/jte/statictemplates/kmethod.jte | 16 +++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/jte-models/src/main/jte/dynamictemplates/kmain.jte b/jte-models/src/main/jte/dynamictemplates/kmain.jte index d73ddd44..19d3fe09 100644 --- a/jte-models/src/main/jte/dynamictemplates/kmain.jte +++ b/jte-models/src/main/jte/dynamictemplates/kmain.jte @@ -15,7 +15,7 @@ package ${config.packageName()} import gg.jte.TemplateEngine import gg.jte.models.runtime.* @for(String imp: imports) - import ${imp} +import ${imp} @endfor ${modelConfig.implementationAnnotation()} diff --git a/jte-models/src/main/jte/dynamictemplates/kmethod.jte b/jte-models/src/main/jte/dynamictemplates/kmethod.jte index 6ce52984..fd817ad6 100644 --- a/jte-models/src/main/jte/dynamictemplates/kmethod.jte +++ b/jte-models/src/main/jte/dynamictemplates/kmethod.jte @@ -4,9 +4,13 @@ @param TemplateDescription template override fun ${Util.methodName(template)}(${Util.kotlinTypedParams(template, false)}): JteModel { - val paramMap = mapOf( + @if(template.params().isEmpty()) + val paramMap = emptyMap() + @else + val paramMap = buildMap { @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) } diff --git a/jte-models/src/main/jte/statictemplates/kmain.jte b/jte-models/src/main/jte/statictemplates/kmain.jte index 2f11c7fc..973fd6ed 100644 --- a/jte-models/src/main/jte/statictemplates/kmain.jte +++ b/jte-models/src/main/jte/statictemplates/kmain.jte @@ -14,11 +14,9 @@ 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()} diff --git a/jte-models/src/main/jte/statictemplates/kmethod.jte b/jte-models/src/main/jte/statictemplates/kmethod.jte index e3af8376..329805c7 100644 --- a/jte-models/src/main/jte/statictemplates/kmethod.jte +++ b/jte-models/src/main/jte/statictemplates/kmethod.jte @@ -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 + ) From 95cf06779b014d1ea3bc7bd99c389452b8962995 Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Tue, 9 Jan 2024 23:12:36 -0500 Subject: [PATCH 2/6] fixup! Reformat the templates for Kotlin model code --- .../src/main/jte/dynamictemplates/kmain.jte | 1 - .../src/main/jte/interfacetemplates/kmain.jte | 1 - .../src/main/jte/statictemplates/kmain.jte | 1 - .../models/generator/TestModelExtension.java | 145 +++++++++++++++--- 4 files changed, 123 insertions(+), 25 deletions(-) diff --git a/jte-models/src/main/jte/dynamictemplates/kmain.jte b/jte-models/src/main/jte/dynamictemplates/kmain.jte index 19d3fe09..f49a521a 100644 --- a/jte-models/src/main/jte/dynamictemplates/kmain.jte +++ b/jte-models/src/main/jte/dynamictemplates/kmain.jte @@ -8,7 +8,6 @@ @param Set templates @param Iterable imports @param ModelConfig modelConfig - @file:Suppress("ktlint") package ${config.packageName()} diff --git a/jte-models/src/main/jte/interfacetemplates/kmain.jte b/jte-models/src/main/jte/interfacetemplates/kmain.jte index 89aa1f87..6ae9799c 100644 --- a/jte-models/src/main/jte/interfacetemplates/kmain.jte +++ b/jte-models/src/main/jte/interfacetemplates/kmain.jte @@ -7,7 +7,6 @@ @param Set templates @param Iterable imports @param ModelConfig modelConfig - @file:Suppress("ktlint") package ${config.packageName()} diff --git a/jte-models/src/main/jte/statictemplates/kmain.jte b/jte-models/src/main/jte/statictemplates/kmain.jte index 973fd6ed..aa5aeb9d 100644 --- a/jte-models/src/main/jte/statictemplates/kmain.jte +++ b/jte-models/src/main/jte/statictemplates/kmain.jte @@ -8,7 +8,6 @@ @param Set templates @param Iterable imports @param ModelConfig modelConfig - @file:Suppress("ktlint") package ${config.packageName()} diff --git a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java index 425884ce..db8a3d2d 100644 --- a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java +++ b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java @@ -219,13 +219,70 @@ 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", """ + @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", """ + @file:Suppress("ktlint") + package test.myktemplates + + import gg.jte.models.runtime.* + import gg.jte.ContentType + import gg.jte.html.HtmlTemplateOutput + + class StaticTemplates : Templates { + \s + override fun hello(content: gg.jte.Content): JteModel = StaticJteModel( + ContentType.Plain, + { output, interceptor -> JtehelloGenerated.render(output, interceptor, content) }, + "hello.kte", + "test.myktemplates", + JtehelloGenerated.JTE_LINE_INFO + ) + + }""", + "DynamicTemplates.kt", """ + @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 { + \s + put("content", content) + } + \s + return DynamicJteModel(engine, "hello.kte", paramMap) + } + + }""" + ); + assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected); } @Test @@ -244,23 +301,67 @@ 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"); - } catch (IOException ex) { - fail("Could not read file " + path, ex); - } - }); + )); + var expected = Map.of( + "Templates.kt", """ + @file:Suppress("ktlint") + package test.myktemplates + + import gg.jte.models.runtime.* + + interface Templates { + \s + @JteView("hello.kte") + fun hello(): JteModel + + }""", + "StaticTemplates.kt", """ + @file:Suppress("ktlint") + package test.myktemplates + + import gg.jte.models.runtime.* + import gg.jte.ContentType + import gg.jte.html.HtmlTemplateOutput + + class StaticTemplates : Templates { + \s + override fun hello(): JteModel = StaticJteModel( + ContentType.Plain, + { output, interceptor -> JtehelloGenerated.render(output, interceptor) }, + "hello.kte", + "test.myktemplates", + JtehelloGenerated.JTE_LINE_INFO + ) + + }""", + "DynamicTemplates.kt", """ + @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() + \s + return DynamicJteModel(engine, "hello.kte", paramMap) + } + + }""" + ); + assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected); } } } From 8782843cc9f5562228138ad3d86deb03082721a5 Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Wed, 24 Jan 2024 15:31:04 -0500 Subject: [PATCH 3/6] fixup! Reformat the templates for Kotlin model code Add import for Collectors --- .../test/java/gg/jte/models/generator/TestModelExtension.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java index db8a3d2d..5840dc64 100644 --- a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java +++ b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java @@ -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; From 5a7144a354ab2172526c2109939a2770d581b128 Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Wed, 24 Jan 2024 15:41:29 -0500 Subject: [PATCH 4/6] fixup! Reformat the templates for Kotlin model code Restore the TemplateOutput import --- jte-models/src/main/jte/statictemplates/kmain.jte | 1 + 1 file changed, 1 insertion(+) diff --git a/jte-models/src/main/jte/statictemplates/kmain.jte b/jte-models/src/main/jte/statictemplates/kmain.jte index aa5aeb9d..29c896bb 100644 --- a/jte-models/src/main/jte/statictemplates/kmain.jte +++ b/jte-models/src/main/jte/statictemplates/kmain.jte @@ -13,6 +13,7 @@ package ${config.packageName()} import gg.jte.models.runtime.* import gg.jte.ContentType +import gg.jte.TemplateOutput import gg.jte.html.HtmlTemplateOutput @for(String imp: imports) import ${imp} From e33fca2ac992fb3b682f984a086ec34aeaa44495 Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Wed, 24 Jan 2024 16:27:00 -0500 Subject: [PATCH 5/6] fixup! Reformat the templates for Kotlin model code --- .../test/java/gg/jte/models/generator/TestModelExtension.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java index 5840dc64..cc00b400 100644 --- a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java +++ b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java @@ -249,6 +249,7 @@ fun hello(content: gg.jte.Content): JteModel import gg.jte.models.runtime.* import gg.jte.ContentType + import gg.jte.TemplateOutput import gg.jte.html.HtmlTemplateOutput class StaticTemplates : Templates { @@ -331,6 +332,7 @@ fun hello(): JteModel import gg.jte.models.runtime.* import gg.jte.ContentType + import gg.jte.TemplateOutput import gg.jte.html.HtmlTemplateOutput class StaticTemplates : Templates { From 3aa393ed05a626b9b4d0886c9cf15c7b661e17fd Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Wed, 24 Jan 2024 18:47:05 -0500 Subject: [PATCH 6/6] fixup! Reformat the templates for Kotlin model code Adjust the tests for windows line endings --- .../models/generator/TestModelExtension.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java index cc00b400..09668992 100644 --- a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java +++ b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java @@ -231,7 +231,7 @@ public void generatesKotlinWithoutDefaultValueForContentParams() { } )); var expected = Map.of( - "Templates.kt", """ + "Templates.kt", withSystemLineEndings(""" @file:Suppress("ktlint") package test.myktemplates @@ -242,8 +242,8 @@ interface Templates { @JteView("hello.kte") fun hello(content: gg.jte.Content): JteModel - }""", - "StaticTemplates.kt", """ + }"""), + "StaticTemplates.kt", withSystemLineEndings(""" @file:Suppress("ktlint") package test.myktemplates @@ -262,8 +262,8 @@ override fun hello(content: gg.jte.Content): JteModel = StaticJteModel( JtehelloGenerated.JTE_LINE_INFO ) - }""", - "DynamicTemplates.kt", """ + }"""), + "DynamicTemplates.kt", withSystemLineEndings(""" @file:Suppress("ktlint") package test.myktemplates @@ -362,9 +362,13 @@ override fun hello(): JteModel { return DynamicJteModel(engine, "hello.kte", paramMap) } - }""" + }""") ); assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected); } } + + private static String withSystemLineEndings(String content) { + return content.replaceAll("\n", System.lineSeparator()); + } }