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

kte: Fix parameter declaration for generated templates #319

Merged
merged 2 commits into from
Jan 19, 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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@casid @edward3h, I'm unsure if this is the best way/place to test it, but let me know what you think.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I'd say!

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