-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
ktorfit-annotations/src/commonMain/kotlin/de/jensklingenberg/ktorfit/http/Tag.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.jensklingenberg.ktorfit.http | ||
|
||
/** | ||
* Adds the argument instance as a request tag using the type as a AttributeKey. | ||
* | ||
* ``` | ||
* @GET("/") | ||
* fun foo(@Tag tag: String) | ||
* ``` | ||
* | ||
* Tag arguments may be `null` which will omit them from the request. | ||
* | ||
* @param value Will be used as the name for the attribute key. The default value will be replaced with the name of the parameter that is annotated. | ||
* | ||
*/ | ||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
annotation class Tag(val value: String = "KTORFIT_DEFAULT_VALUE") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
.../src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/AttributeCodeGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package de.jensklingenberg.ktorfit.reqBuilderExtension | ||
|
||
import de.jensklingenberg.ktorfit.model.ParameterData | ||
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation | ||
|
||
fun getAttributeCode(parameterDataList: List<ParameterData>): String { | ||
return parameterDataList.filter { it.hasAnnotation<ParameterAnnotation.Tag>() } | ||
.joinToString("\n") { | ||
val tag = it.findAnnotationOrNull<ParameterAnnotation.Tag>()!! | ||
if (it.type.isNullable) { | ||
"${it.name}?.let{ attributes.put(io.ktor.util.AttributeKey(\"${tag.value}\"), it) }" | ||
} else { | ||
"attributes.put(io.ktor.util.AttributeKey(\"${tag.value}\"), ${it.name})" | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/ReqBuilderAnnotationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/TagAnnotationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package de.jensklingenberg.ktorfit | ||
|
||
import com.tschuchort.compiletesting.KotlinCompilation | ||
import com.tschuchort.compiletesting.SourceFile | ||
import com.tschuchort.compiletesting.kspSourcesDir | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class TagAnnotationsTest { | ||
|
||
@Test | ||
fun whenTagsAnnotationFound_AddThemAsAttributeKey() { | ||
|
||
val source = SourceFile.kotlin( | ||
"Source.kt", """ | ||
package com.example.api | ||
import de.jensklingenberg.ktorfit.http.GET | ||
import de.jensklingenberg.ktorfit.http.Tag | ||
interface TestService { | ||
@GET("posts") | ||
suspend fun test(@Tag myTag1 : String, @Tag("myTag2") someParameter: Int?): String | ||
} | ||
""" | ||
) | ||
|
||
val expectedHeadersArgumentText = | ||
"""attributes.put(io.ktor.util.AttributeKey("myTag1"), myTag1) | ||
someParameter?.let{ attributes.put(io.ktor.util.AttributeKey("myTag2"), it) } """ | ||
|
||
val compilation = getCompilation(listOf(source)) | ||
val result = compilation.compile() | ||
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode) | ||
val generatedSourcesDir = compilation.kspSourcesDir | ||
val generatedFile = File( | ||
generatedSourcesDir, | ||
"/kotlin/com/example/api/_TestServiceImpl.kt" | ||
) | ||
|
||
val actualSource = generatedFile.readText() | ||
assertTrue(actualSource.contains(expectedHeadersArgumentText)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters