Skip to content

Commit

Permalink
kte: Fix parameter declaration for generated templates (#319)
Browse files Browse the repository at this point in the history
* kte: Fix parameter declaration for generated templates

* Some minor clean ups
  • Loading branch information
marcospereira authored Jan 19, 2024
1 parent de6880b commit d4f82bf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,7 @@ public void onComplete() {
continue;
}

kotlinCode.setCurrentTemplateLine(parameter.templateLine);
kotlinCode.append("\t\tval ").append(parameter.name).append(" = params[\"").append(parameter.name).append("\"] as ").append(parameter.type);
if (parameter.defaultValue != null) {
kotlinCode.append("? ?: ");
writeCodeWithContentSupport(0, parameter.defaultValue);
}
kotlinCode.append('\n');
writeParameterDeclaration(parameter);
}
kotlinCode.append("\t\trender(jteOutput, jteHtmlInterceptor");

Expand Down Expand Up @@ -184,6 +178,29 @@ public void onComplete() {
this.classInfo.lineInfo = kotlinCode.getLineInfo();
}

private void writeParameterDeclaration(ParamInfo parameter) {
var nonNullDefaultValue = parameter.defaultValue != null && !parameter.defaultValue.equals("null");

kotlinCode.setCurrentTemplateLine(parameter.templateLine);
kotlinCode.append("\t\tval ").append(parameter.name).append(" = params[\"").append(parameter.name).append("\"] as ");
if (nonNullDefaultValue) {
kotlinCode.append(asNullableType(parameter.type));
kotlinCode.append(" ?: ");
writeCodeWithContentSupport(0, parameter.defaultValue);
} else {
kotlinCode.append(parameter.type);
}
kotlinCode.append('\n');
}

private String asNullableType(String type) {
if (type.endsWith("?")) {
return type;
} else {
return type + "?";
}
}

private void addNameField(StringBuilder fields, String name) {
fields.append("\t@JvmField val ").append(Constants.NAME_FIELD).append(" = \"");
fields.append(name);
Expand Down
39 changes: 39 additions & 0 deletions jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.*;

Expand Down Expand Up @@ -764,6 +768,41 @@ void paramWithDefaultValue() {
assertThat(output.toString()).isEqualTo("Your age is 10");
}

@Test
void nullableParamsShouldNotHaveDoubleQuestionMarks() throws IOException {
givenRawTemplate("@param age: Int? = 10\nYour age is ${age}");

List<String> compiledPaths = templateEngine.precompileAll();
Path classDirectory = Paths.get("jte-classes");
var templateContent = Files.readString(classDirectory.resolve(compiledPaths.get(0)));

assertThat(templateContent).contains("val age = params[\"age\"] as Int?");
assertThat(templateContent).doesNotContain("val age = params[\"age\"] as Int??");
}

@Test
void nullableParamsWithNonNullDefaultValuesShouldFallbackToTheDefault() throws IOException {
givenRawTemplate("@param age: Int? = 10\nYour age is ${age}");

List<String> compiledPaths = templateEngine.precompileAll();
Path classDirectory = Paths.get("jte-classes");
var templateContent = Files.readString(classDirectory.resolve(compiledPaths.get(0)));

assertThat(templateContent).contains("val age = params[\"age\"] as Int? ?: 10");
}

@Test
void nullableParamsWithNullDefaultValuesShouldNotUseElvisOperator() throws IOException {
givenRawTemplate("@param age: Int? = null\nYour age is ${age}");

List<String> compiledPaths = templateEngine.precompileAll();
Path classDirectory = Paths.get("jte-classes");
var templateContent = Files.readString(classDirectory.resolve(compiledPaths.get(0)));

assertThat(templateContent).contains("val age = params[\"age\"] as Int?");
assertThat(templateContent).doesNotContain("val age = params[\"age\"] as Int? ?: null");
}

@Test
void contentParamWithDefaultValue() {
givenRawTemplate("@param content:gg.jte.Content = @`Some Content`\nThe default content is ${content}");
Expand Down

0 comments on commit d4f82bf

Please sign in to comment.