Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat the templates for Kotlin model code #310

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading