Skip to content

Commit

Permalink
Merge pull request #41 from papsign/reworked-model
Browse files Browse the repository at this point in the history
Reworked model
  • Loading branch information
Wicpar committed May 4, 2020
2 parents 0acb0b9 + 1575e7a commit e3a6dad
Show file tree
Hide file tree
Showing 145 changed files with 2,735 additions and 1,510 deletions.
467 changes: 184 additions & 283 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.61"
id "org.jetbrains.kotlin.jvm" version "$kotlin_version"
}

group 'com.papsign.ktor'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
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 src/main/kotlin/com/papsign/kotlin/reflection/Reflection.kt

This file was deleted.

39 changes: 23 additions & 16 deletions src/main/kotlin/com/papsign/ktor/openapigen/APIException.kt
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
)
}
}
}
13 changes: 7 additions & 6 deletions src/main/kotlin/com/papsign/ktor/openapigen/APITag.kt
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)
}
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/com/papsign/ktor/openapigen/KTypeUtil.kt
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
Loading

0 comments on commit e3a6dad

Please sign in to comment.