Skip to content

Commit

Permalink
Reformat the templates for Kotlin model code
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
MariusVolkhart authored and casid committed Jan 16, 2024
1 parent de6880b commit eec51dc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion jte-models/src/main/jte/dynamictemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
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)
}
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 @@ -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()}
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
)

0 comments on commit eec51dc

Please sign in to comment.