Skip to content

Commit

Permalink
[kotlin-server][ktor2] fix isKtor, update samples (#20327)
Browse files Browse the repository at this point in the history
* fix isKtor, update samples

* fi dockerfile

* fix dataclass

* add new files

* fix template

* regenerate samples
  • Loading branch information
wing328 authored Dec 14, 2024
1 parent f766f44 commit b161440
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,21 @@ public void processOpts() {

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

if (isKtor()) {
if (isKtor2Or3()) {
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
}

String gradleBuildFile = "build.gradle";

if (isJavalin() || isKtor()) {
if (isJavalin() || isKtor2Or3()) {
gradleBuildFile = "build.gradle.kts";
}

supportingFiles.add(new SupportingFile(gradleBuildFile + ".mustache", "", gradleBuildFile));
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
supportingFiles.add(new SupportingFile("gradle.properties", "", "gradle.properties"));

if (isKtor()) {
if (isKtor2Or3()) {
additionalProperties.put(Constants.IS_KTOR, true);

supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
Expand Down Expand Up @@ -445,7 +445,20 @@ private boolean isJavalin() {
return Constants.JAVALIN5.equals(library) || Constants.JAVALIN6.equals(library);
}

private boolean isKtor() {
private boolean isKtor2Or3() {
return Constants.KTOR.equals(library) || Constants.KTOR2.equals(library);
}

/**
* Returns true if latest version of ktor is used.
*
* @return true if latest veresion of ktor is used.
*/
private boolean isKtor() {
return Constants.KTOR.equals(library);
}

private boolean isKtor2() {
return Constants.KTOR2.equals(library);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#hasAuthMethods}}
{{>libraries/ktor/_principal}}
{{>libraries/ktor2/_principal}}
{{#examples}}
{{#-first}}
{{#lambda.indented}}{{>_response}}{{/lambda.indented}}
Expand All @@ -12,10 +12,10 @@ call.respond(HttpStatusCode.NotImplemented)
{{^hasAuthMethods}}
{{#examples}}
{{#-first}}
{{>libraries/ktor/_response}}
{{>libraries/ktor2/_response}}
{{/-first}}
{{/examples}}
{{^examples}}
call.respond(HttpStatusCode.NotImplemented)
{{/examples}}
{{/hasAuthMethods}}
{{/hasAuthMethods}}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ fun Route.{{classname}}() {
{{^featureResources}}
route("{{path}}") {
{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}} {
{{#lambda.indented_12}}{{>libraries/ktor/_api_body}}{{/lambda.indented_12}}
{{#lambda.indented_12}}{{>libraries/ktor2/_api_body}}{{/lambda.indented_12}}
}
}
{{/featureResources}}
{{#featureResources}}
{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}<Paths.{{operationId}}> {
{{#lambda.indented_8}}{{>libraries/ktor/_api_body}}{{/lambda.indented_8}}
{{#lambda.indented_8}}{{>libraries/ktor2/_api_body}}{{/lambda.indented_8}}
}
{{/featureResources}}
{{#hasAuthMethods}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{{#parcelizeModels}}
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

{{/parcelizeModels}}
{{#serializableModel}}
import java.io.Serializable
{{/serializableModel}}
/**
* {{{description}}}
{{#vars}}
* @param {{{name}}} {{{description}}}
{{/vars}}
*/
{{#parcelizeModels}}
@Parcelize
{{/parcelizeModels}}
{{#hasVars}}data {{/hasVars}}class {{classname}}(
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>data_class_opt_var}}{{^-last}},
{{/-last}}{{/optionalVars}}
) {{^serializableModel}}{{#parcelizeModels}} : Parcelable{{/parcelizeModels}}{{/serializableModel}}{{^parcelizeModels}}{{#serializableModel}}: Serializable {{/serializableModel}}{{/parcelizeModels}}{{#parcelizeModels}}{{#serializableModel}} : Parcelable, Serializable {{/serializableModel}}{{/parcelizeModels}}
{{#vendorExtensions.x-has-data-class-body}}
{
{{/vendorExtensions.x-has-data-class-body}}
{{#serializableModel}}
companion object {
private const val serialVersionUID: Long = 123
}
{{/serializableModel}}
{{#hasEnums}}
{{#vars}}
{{#isEnum}}
/**
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{{nameInPascalCase}}}(val value: {{{dataType}}}){
{{#allowableValues}}
{{#enumVars}}
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/vars}}
{{/hasEnums}}
{{#vendorExtensions.x-has-data-class-body}}
}
{{/vendorExtensions.x-has-data-class-body}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{#description}}
/* {{{.}}} */
{{/description}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#description}}
/* {{{.}}} */
{{/description}}
{{! Note that required properties may be nullable according to the OpenAPI specification. }}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}

0 comments on commit b161440

Please sign in to comment.