Skip to content

Commit

Permalink
Implement AutoCloseable to close wrapped client
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Jul 3, 2024
1 parent 436adf3 commit dcef1d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
kotlin("multiplatform") version "2.0.0"
kotlin("plugin.serialization") version "2.0.0"
id("io.github.nomisrev.openapi-kt-plugin") version "0.0.4"
id("io.github.nomisrev.openapi-kt-plugin") version "0.0.6"
}

openApiConfig { spec("OpenAI", file("openai.yaml")) {
Expand Down
19 changes: 15 additions & 4 deletions generation/src/main/kotlin/io/github/nomisrev/openapi/APIs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.kotlinpoet.MemberSpecHolder
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.TypeSpecHolder
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.withIndent
import io.github.nomisrev.openapi.NamingContext.Named
Expand Down Expand Up @@ -55,12 +56,16 @@ context(OpenAPIContext)
private fun Root.className(): ClassName = toClassName(Named(name))

context(OpenAPIContext, FileSpec.Builder)
private fun Root.addPaths() {
private fun Root.addInterface() {
val properties =
endpoints.map { api ->
PropertySpec(toParamName(Named(api.name)), toClassName(Named(api.name)))
}
val type = TypeSpec.interfaceBuilder(className()).addProperties(properties).build()
val type =
TypeSpec.interfaceBuilder(className())
.addSuperinterface(AutoCloseable::class)
.addProperties(properties)
.build()
addType(type)
}

Expand Down Expand Up @@ -91,9 +96,15 @@ private fun Root.implementation() {
addType(
TypeSpec.classBuilder(className.postfix("Ktor"))
.addModifiers(KModifier.PRIVATE)
.addSuperinterface(className)
.addSuperinterfaces(listOf(className, AutoCloseable::class.asClassName()))
.apiConstructor()
.addProperties(properties)
.addFunction(
FunSpec.builder("close")
.addModifiers(KModifier.OVERRIDE)
.addStatement("client.close()")
.build()
)
.build()
)
}
Expand All @@ -102,7 +113,7 @@ context(OpenAPIContext)
private fun Root.root() =
FileSpec.builder(`package`, className().simpleName)
.apply {
addPaths()
addInterface()
operations.forEach { it.addFunction(implemented = false) }
smartConstructor()
implementation()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.nomisrev.openapi.plugin

import java.io.File
import java.io.Serializable
import javax.inject.Inject
import org.gradle.api.Project
import org.gradle.api.provider.ListProperty
Expand All @@ -18,4 +19,9 @@ abstract class OpenApiConfig @Inject constructor(private val project: Project) {

data class OpenApiBuilder(var packageName: String? = null)

data class SpecDefinition(val name: String, val file: File, val packageName: String?)
data class SpecDefinition(val name: String, val file: File, val packageName: String?) :
Serializable {
companion object {
@JvmStatic val serialVersionUID = 1L
}
}

0 comments on commit dcef1d0

Please sign in to comment.