-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from papsign/reworked-model
Reworked model
- Loading branch information
Showing
145 changed files
with
2,735 additions
and
1,510 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
kotlin.code.style=official | ||
ktor_version=1.3.2 | ||
logback_version=1.2.1 | ||
kotlin_version=1.3.61 | ||
kotlin_version=1.3.70 |
127 changes: 0 additions & 127 deletions
127
src/main/kotlin/com/papsign/kotlin/reflection/Reflection.kt
This file was deleted.
Oops, something went wrong.
39 changes: 23 additions & 16 deletions
39
src/main/kotlin/com/papsign/ktor/openapigen/APIException.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 |
---|---|---|
@@ -1,35 +1,42 @@ | ||
package com.papsign.ktor.openapigen | ||
|
||
import com.papsign.kotlin.reflection.getKType | ||
import com.papsign.kotlin.reflection.unitKType | ||
import io.ktor.http.HttpStatusCode | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KType | ||
|
||
interface APIException<EX: Throwable, B> { | ||
interface APIException<EX : Throwable, B> { | ||
val status: HttpStatusCode | ||
val exceptionClass: KClass<EX> | ||
val contentType: KType | ||
get() = unitKType | ||
val contentGen: ((EX)->B)? | ||
val contentGen: ((EX) -> B)? | ||
get() = null | ||
val example: B? | ||
get() = null | ||
|
||
companion object { | ||
class APIExceptionProxy<EX: Throwable, B>(override val status: HttpStatusCode, | ||
override val exceptionClass: KClass<EX>, | ||
override val contentType: KType = unitKType, | ||
override val contentGen: ((EX)->B)? = null): APIException<EX, B> | ||
|
||
class EmptyAPIExceptionProxy<EX: Throwable>(override val status: HttpStatusCode, | ||
override val exceptionClass: KClass<EX>): APIException<EX, Unit> | ||
|
||
class APIExceptionProxy<EX : Throwable, B>( | ||
override val status: HttpStatusCode, | ||
override val exceptionClass: KClass<EX>, | ||
override val example: B? = null, | ||
override val contentType: KType = unitKType, | ||
override val contentGen: ((EX) -> B)? = null | ||
) : APIException<EX, B> | ||
|
||
inline fun <reified EX: Throwable> apiException(status: HttpStatusCode): APIException<EX, Unit> { | ||
return EmptyAPIExceptionProxy(status, EX::class) | ||
inline fun <reified EX : Throwable> apiException(status: HttpStatusCode): APIException<EX, Unit> { | ||
return apiException(status, null as Unit?) | ||
} | ||
|
||
inline fun <reified EX: Throwable, reified B> apiException(status: HttpStatusCode, noinline gen: (EX)->B): APIException<EX, B> { | ||
return APIExceptionProxy(status, EX::class, getKType<B>(), gen) | ||
inline fun <reified EX : Throwable, reified B> apiException( | ||
status: HttpStatusCode, | ||
example: B? = null, | ||
noinline gen: ((EX) -> B)? = null | ||
): APIException<EX, B> { | ||
return APIExceptionProxy( | ||
status, EX::class, | ||
example, | ||
getKType<B>(), gen | ||
) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package com.papsign.ktor.openapigen | ||
|
||
import com.papsign.ktor.openapigen.openapi.ExternalDocumentation | ||
import com.papsign.ktor.openapigen.openapi.Tag | ||
import com.papsign.ktor.openapigen.model.info.ExternalDocumentationModel | ||
import com.papsign.ktor.openapigen.model.info.TagModel | ||
|
||
|
||
interface APITag { | ||
val name: String | ||
val description: String | ||
val externalDocs: ExternalDocumentation? | ||
val externalDocs: ExternalDocumentationModel? | ||
get() = null | ||
|
||
fun toTag(): Tag { | ||
return Tag(name, description, externalDocs) | ||
fun toTag(): TagModel { | ||
return TagModel(name, description, externalDocs) | ||
} | ||
} | ||
} |
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,46 @@ | ||
package com.papsign.ktor.openapigen | ||
|
||
import java.lang.reflect.Field | ||
import kotlin.reflect.* | ||
import kotlin.reflect.full.createType | ||
import kotlin.reflect.full.declaredMemberProperties | ||
import kotlin.reflect.full.memberProperties | ||
import kotlin.reflect.jvm.javaField | ||
import kotlin.reflect.jvm.jvmErasure | ||
|
||
val unitKType = getKType<Unit>() | ||
|
||
inline fun <reified T> isNullable(): Boolean { | ||
return null is T | ||
} | ||
|
||
inline fun <reified T> getKType() = typeOf<T>() | ||
|
||
fun KType.strip(nullable: Boolean = isMarkedNullable): KType { | ||
return jvmErasure.createType(arguments, nullable) | ||
} | ||
|
||
fun KType.deepStrip(nullable: Boolean = isMarkedNullable): KType { | ||
return jvmErasure.createType(arguments.map { it.copy(type = it.type?.deepStrip()) }, nullable) | ||
} | ||
|
||
data class KTypeProperty( | ||
val name: String, | ||
val type: KType, | ||
val source: KProperty1<*, *> | ||
) | ||
|
||
val KType.memberProperties: List<KTypeProperty> | ||
get() { | ||
val typeParameters = jvmErasure.typeParameters.zip(arguments).associate { Pair(it.first.name, it.second.type) } | ||
return jvmErasure.memberProperties.map { | ||
val retType = it.returnType | ||
val properType = when (val classifier = retType.classifier) { | ||
is KTypeParameter -> typeParameters[classifier.name] ?: it.returnType | ||
else -> it.returnType | ||
} | ||
KTypeProperty(it.name, properType, it) | ||
} | ||
} | ||
|
||
val KClass<*>.isInterface get() = java.isInterface |
Oops, something went wrong.