Skip to content

Commit

Permalink
Release 2.0.0-beta2
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed May 16, 2024
1 parent 8e51a1d commit 96db58e
Show file tree
Hide file tree
Showing 31 changed files with 181 additions and 89 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project orients towards [Semantic Versioning](http://semver.org/spec/v2
Note: This project needs KSP to work and every new Ktorfit with an update of the KSP version is technically a breaking change.
But there is no intent to bump the Ktorfit major version for every KSP update.

2.0.0-beta2 - 2024-05-16
========================================
- Build with KSP 1.0.20, Kotlin 2.0.0-RC3, Ktor 2.3.11
- Optimize code generation

2.0.0-beta1 - 2024-04-28
========================================
### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import de.jensklingenberg.ktorfit.converter.ResponseConverterFactory
import de.jensklingenberg.ktorfit.ktorfit
import io.ktor.client.HttpClient
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.prepareRequest
import io.ktor.client.request.takeFrom
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -58,7 +60,12 @@ class MainActivity : ComponentActivity() {
}
}



lifecycleScope.launch {
ktorfit.httpClient.prepareRequest {
this.method = io.ktor.http.HttpMethod.Post
}
peopleState.value = api.getPerson(1)
}
}
Expand Down
15 changes: 9 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
autoService = "1.1.1"
autoServiceKsp = "1.10"
binaryCompatibilityValidator = "0.13.2"
coroutines = "1.7.3"
detekt = "1.23.1"
coroutines = "1.8.0"
detekt = "1.23.6"
junit = "4.13.2"
kctfork = "0.4.1"
kotlin = "2.0.0-RC1"
kotlin = "2.0.0-RC3"
kotlinPoet = "1.16.0"
kspVersion = "2.0.0-RC1-1.0.20"
ktorfit = "2.0.0-beta1"
kspVersion = "2.0.0-RC3-1.0.20"
ktorfit = "2.0.0-beta2-SNAPSHOT"
ktorfitKsp = "2.0.0-beta2-SNAPSHOT"
ktorfitCompiler = "2.0.0-beta2-SNAPSHOT"

ktorfitGradlePlugin = "1.14.0"
ktorVersion = "2.3.10"
ktorVersion = "2.3.11"
mockk = "1.13.8"
mockito-kotlin = "4.1.0"
gradleMavenPublishPlugin = "0.25.3"
Expand Down
2 changes: 1 addition & 1 deletion ktorfit-compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ licensee {
}

mavenPublishing {
coordinates("de.jensklingenberg.ktorfit", "compiler-plugin", libs.versions.ktorfit.get())
coordinates("de.jensklingenberg.ktorfit", "compiler-plugin", libs.versions.ktorfitCompiler.get())
publishToMavenCentral()
// publishToMavenCentral(SonatypeHost.S01) for publishing through s01.oss.sonatype.org
if (enableSigning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.types.classFqName
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.companionObject
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.name.ClassId
Expand Down Expand Up @@ -73,13 +74,15 @@ class CreateFuncTransformer(
)
) ?: throw IllegalStateException(ERROR_IMPL_NOT_FOUND(argumentType.originalKotlinType.toString()))

val newConstructor = implClassSymbol.constructors.first()
val compo = implClassSymbol.owner.companionObject()!!.symbol

val newConstructor = compo!!.constructors.first()

//Create the constructor call for _ExampleApiImpl()
val newCall = IrConstructorCallImpl(
0,
0,
type = implClassSymbol.defaultType,
type = compo.defaultType,
symbol = newConstructor,
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression
import org.jetbrains.kotlin.ir.expressions.*

class ElementTransformer(
private val pluginContext: IrPluginContext,
Expand All @@ -33,6 +31,33 @@ class ElementTransformer(
return super.visitVariable(declaration)
}

override fun visitBlock(expression: IrBlock): IrExpression {
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
return super.visitBlock(expression)
}

override fun visitFunctionNew(declaration: IrFunction): IrStatement {
declaration.transform(CreateFuncTransformer(pluginContext, debugLogger), null)

return super.visitFunctionNew(declaration)
}

override fun visitExpression(expression: IrExpression): IrExpression {
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null)

return super.visitExpression(expression)
}

override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null)

return super.visitFunctionReference(expression)
}

override fun visitBlockBody(body: IrBlockBody): IrBody {
body.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
return super.visitBlockBody(body)
}

override fun visitFunctionExpression(expression: IrFunctionExpression): IrExpression {
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ class FunctionTransformerTest {
package de.jensklingenberg.ktorfit
class Ktorfit(){
fun <T> create(ktorfitService: KtorfitService? = null): T {
fun <T> create(ktorfitService: ClassProvider<T>? = null): T {
return ktorfitService as T
}
}
interface KtorfitService
class Default : KtorfitService
interface ClassProvider<T>
"""
)
Expand All @@ -41,10 +39,12 @@ class Default : KtorfitService
package com.example.api
import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.KtorfitService
import de.jensklingenberg.ktorfit.ClassProvider
interface TestService
class _TestServiceImpl : TestService, KtorfitService
class _TestServiceImpl : TestService{
companion object : ClassProvider<TestService>
}
class TestClass{
val api = Ktorfit().create<TestService>()
Expand All @@ -69,11 +69,9 @@ package de.jensklingenberg.ktorfit
class Ktorfit()
interface KtorfitService
interface ClassProvider
class Default : KtorfitService
fun <T> Ktorfit.create(ktorfitService: KtorfitService = Default()): T {
fun <T> Ktorfit.create(ktorfitService: ClassProvider? = null): T {
return this.create(ktorfitService)
}
Expand All @@ -84,10 +82,12 @@ fun <T> Ktorfit.create(ktorfitService: KtorfitService = Default()): T {
package com.example.api
import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.create
import de.jensklingenberg.ktorfit.KtorfitService
import de.jensklingenberg.ktorfit.ClassProvider
interface TestService
class _TestServiceImpl : TestService, KtorfitService
class _TestServiceImpl : TestService{
companion object : ClassProvider
}
class TestClass{
Expand Down
2 changes: 1 addition & 1 deletion ktorfit-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mavenPublishing {
coordinates(
"de.jensklingenberg.ktorfit",
"ktorfit-gradle-plugin",
libs.versions.ktorfit.get()
libs.versions.ktorfitCompiler.get()
)
publishToMavenCentral()
// publishToMavenCentral(SonatypeHost.S01) for publishing through s01.oss.sonatype.org
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class KtorfitCompilerSubPlugin : KotlinCompilerPluginSupportPlugin {
return SubpluginArtifact(
groupId = SERIALIZATION_GROUP_NAME,
artifactId = ARTIFACT_NAME,
version = "2.0.0-beta1" // remember to bump this version before any release!
version = "2.0.0-beta2-2.0.0-RC3" // remember to bump this version before any release!
)
}
}
6 changes: 5 additions & 1 deletion ktorfit-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ tasks.withType<KotlinCompile> {
}

mavenPublishing {
coordinates("de.jensklingenberg.ktorfit", "ktorfit-ksp", libs.versions.ktorfit.get())
coordinates(
"de.jensklingenberg.ktorfit",
"ktorfit-ksp",
(libs.versions.ktorfitKsp.get())
)
publishToMavenCentral()
// publishToMavenCentral(SonatypeHost.S01) for publishing through s01.oss.sonatype.org
if (enableSigning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ package de.jensklingenberg.ktorfit.model
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.ksp.toKModifier
import com.squareup.kotlinpoet.ksp.toTypeName
import de.jensklingenberg.ktorfit.KtorfitOptions
import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.PROPERTIES_NOT_SUPPORTED
import de.jensklingenberg.ktorfit.model.annotations.FormUrlEncoded
import de.jensklingenberg.ktorfit.model.annotations.Multipart
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Field
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Part
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.RequestType
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.*
import de.jensklingenberg.ktorfit.utils.addImports

/**
Expand Down Expand Up @@ -81,19 +76,40 @@ fun ClassData.getImplClassFileSource(resolver: Resolver, ktorfitOptions: Ktorfit
val implClassName = "_${classData.name}Impl"

val converterProperty =
PropertySpec.builder(converterHelper.objectName, converterHelper.toClassName())
.addModifiers(KModifier.LATEINIT, KModifier.OVERRIDE)
.mutable(true)
PropertySpec.builder(converterHelper.objectName, converterHelper.toClassName()).initializer("KtorfitConverterHelper(_ktorfit)")
.addModifiers(KModifier.PRIVATE)
.build()

val companion = TypeSpec.companionObjectBuilder()
.addSuperinterface(exampleInterface.toClassName().parameterizedBy(ClassName(classData.packageName, classData.name)))
.addFunction(
FunSpec.builder("create")
.returns(ClassName(classData.packageName, classData.name))
.addStatement("return _${classData.name}Impl(_ktorfit)")
.addModifiers( KModifier.OVERRIDE)
.addParameter(ktorfitClass.objectName, ktorfitClass.toClassName())
.build()
)
.build()
val implClassSpec = TypeSpec.classBuilder(implClassName)

.primaryConstructor(
FunSpec.constructorBuilder()
.addParameter(ktorfitClass.objectName, ktorfitClass.toClassName())
.build()
)
.addProperty(
PropertySpec.builder("_ktorfit", ktorfitClass.toClassName())
.initializer(ktorfitClass.objectName)
.addModifiers(KModifier.PRIVATE)
.build()
)
.addAnnotation(
optinAnnotation
)
.addType(companion)
.addModifiers(classData.modifiers)
.addSuperinterface(ClassName(classData.packageName, classData.name))
.addSuperinterface(ktorfitInterface.toClassName())
//.addSuperinterface(ktorfitInterface.toClassName())
.addKtorfitSuperInterface(classData.superClasses)
.addProperties(listOf(converterProperty) + properties)
.addFunctions(classData.functions.map { it.toFunSpec(resolver, ktorfitOptions.setQualifiedType) })
Expand All @@ -118,7 +134,7 @@ private fun getCreateExtensionFunctionSpec(
val functionName = "create${classData.name}"
return FunSpec.builder(functionName)
.addModifiers(classData.modifiers)
.addStatement("return this.create(_${classData.name}Impl().apply { ${converterHelper.objectName}= ${converterHelper.name}(this@$functionName) })")
.addStatement("return _${classData.name}Impl(this)")
.receiver(ktorfitClass.toClassName())
.returns(ClassName(classData.packageName, classData.name))
.build()
Expand Down Expand Up @@ -233,7 +249,7 @@ private fun TypeSpec.Builder.addKtorfitSuperInterface(superClasses: List<KSTypeR
this.addSuperinterface(
ClassName(superTypePackage, superTypeClassName),
CodeBlock.of(
"%L._%LImpl()",
"%L._%LImpl(_ktorfit)",
superTypePackage,
superTypeClassName
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ data class KtorfitClass(val name: String, val packageName: String, val objectNam

val ktorfitClass = KtorfitClass("Ktorfit", "de.jensklingenberg.ktorfit", "_ktorfit")
val ktorfitInterface = KtorfitClass("KtorfitInterface", "de.jensklingenberg.ktorfit.internal", "EMPTY")
val exampleInterface = KtorfitClass("ClassProvider", "de.jensklingenberg.ktorfit.internal", "EMPTY")

val typeDataClass = KtorfitClass("TypeData", "de.jensklingenberg.ktorfit.converter", "_typeData")
val extDataClass = KtorfitClass("HttpRequestBuilder.() -> Unit", "", "_ext")
val formParameters = KtorfitClass("", "", "__formParameters")
val converterHelper = KtorfitClass("KtorfitConverterHelper","de.jensklingenberg.ktorfit.internal", "_converter")
val converterHelper = KtorfitClass("KtorfitConverterHelper","de.jensklingenberg.ktorfit.internal", "_helper")
val internalApi = ClassName("de.jensklingenberg.ktorfit.internal", "InternalKtorfitApi")

fun KtorfitClass.toClassName() = ClassName(packageName, name)
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fun getUrlCode(params: List<ParameterData>, methodAnnotation: HttpMethodAnnotati
""
} else {
params.firstOrNull { it.hasAnnotation<Url>() }?.let { parameterData ->
"(${converterHelper.objectName}.baseUrl.takeIf{ !${parameterData.name}.startsWith(\"http\")} ?: \"\") + "
} ?: "${converterHelper.objectName}.baseUrl + "
"(${ktorfitClass.objectName}.baseUrl.takeIf{ !${parameterData.name}.startsWith(\"http\")} ?: \"\") + "
} ?: "${ktorfitClass.objectName}.baseUrl + "
}

params.filter { it.hasAnnotation<Path>() }.forEach { parameterData ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ interface TestService {
val expectedFunctionText = """val _ext: HttpRequestBuilder.() -> Unit = {
method = HttpMethod.parse("GET2")
url{
takeFrom(_converter.baseUrl + "user")
takeFrom(_ktorfit.baseUrl + "user")
}
setBody(body)
}"""
Expand Down
Loading

0 comments on commit 96db58e

Please sign in to comment.