Skip to content

Commit e3a6dad

Browse files
authored
Merge pull request #41 from papsign/reworked-model
Reworked model
2 parents 0acb0b9 + 1575e7a commit e3a6dad

File tree

145 files changed

+2735
-1510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+2735
-1510
lines changed

README.md

Lines changed: 184 additions & 283 deletions
Large diffs are not rendered by default.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id "org.jetbrains.kotlin.jvm" version "1.3.61"
2+
id "org.jetbrains.kotlin.jvm" version "$kotlin_version"
33
}
44

55
group 'com.papsign.ktor'

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
kotlin.code.style=official
22
ktor_version=1.3.2
33
logback_version=1.2.1
4-
kotlin_version=1.3.61
4+
kotlin_version=1.3.70

src/main/kotlin/com/papsign/kotlin/reflection/Reflection.kt

Lines changed: 0 additions & 127 deletions
This file was deleted.
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
package com.papsign.ktor.openapigen
22

3-
import com.papsign.kotlin.reflection.getKType
4-
import com.papsign.kotlin.reflection.unitKType
53
import io.ktor.http.HttpStatusCode
64
import kotlin.reflect.KClass
75
import kotlin.reflect.KType
86

9-
interface APIException<EX: Throwable, B> {
7+
interface APIException<EX : Throwable, B> {
108
val status: HttpStatusCode
119
val exceptionClass: KClass<EX>
1210
val contentType: KType
1311
get() = unitKType
14-
val contentGen: ((EX)->B)?
12+
val contentGen: ((EX) -> B)?
13+
get() = null
14+
val example: B?
1515
get() = null
1616

1717
companion object {
18-
class APIExceptionProxy<EX: Throwable, B>(override val status: HttpStatusCode,
19-
override val exceptionClass: KClass<EX>,
20-
override val contentType: KType = unitKType,
21-
override val contentGen: ((EX)->B)? = null): APIException<EX, B>
22-
23-
class EmptyAPIExceptionProxy<EX: Throwable>(override val status: HttpStatusCode,
24-
override val exceptionClass: KClass<EX>): APIException<EX, Unit>
25-
18+
class APIExceptionProxy<EX : Throwable, B>(
19+
override val status: HttpStatusCode,
20+
override val exceptionClass: KClass<EX>,
21+
override val example: B? = null,
22+
override val contentType: KType = unitKType,
23+
override val contentGen: ((EX) -> B)? = null
24+
) : APIException<EX, B>
2625

27-
inline fun <reified EX: Throwable> apiException(status: HttpStatusCode): APIException<EX, Unit> {
28-
return EmptyAPIExceptionProxy(status, EX::class)
26+
inline fun <reified EX : Throwable> apiException(status: HttpStatusCode): APIException<EX, Unit> {
27+
return apiException(status, null as Unit?)
2928
}
3029

31-
inline fun <reified EX: Throwable, reified B> apiException(status: HttpStatusCode, noinline gen: (EX)->B): APIException<EX, B> {
32-
return APIExceptionProxy(status, EX::class, getKType<B>(), gen)
30+
inline fun <reified EX : Throwable, reified B> apiException(
31+
status: HttpStatusCode,
32+
example: B? = null,
33+
noinline gen: ((EX) -> B)? = null
34+
): APIException<EX, B> {
35+
return APIExceptionProxy(
36+
status, EX::class,
37+
example,
38+
getKType<B>(), gen
39+
)
3340
}
3441
}
3542
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.papsign.ktor.openapigen
22

3-
import com.papsign.ktor.openapigen.openapi.ExternalDocumentation
4-
import com.papsign.ktor.openapigen.openapi.Tag
3+
import com.papsign.ktor.openapigen.model.info.ExternalDocumentationModel
4+
import com.papsign.ktor.openapigen.model.info.TagModel
5+
56

67
interface APITag {
78
val name: String
89
val description: String
9-
val externalDocs: ExternalDocumentation?
10+
val externalDocs: ExternalDocumentationModel?
1011
get() = null
1112

12-
fun toTag(): Tag {
13-
return Tag(name, description, externalDocs)
13+
fun toTag(): TagModel {
14+
return TagModel(name, description, externalDocs)
1415
}
15-
}
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.papsign.ktor.openapigen
2+
3+
import java.lang.reflect.Field
4+
import kotlin.reflect.*
5+
import kotlin.reflect.full.createType
6+
import kotlin.reflect.full.declaredMemberProperties
7+
import kotlin.reflect.full.memberProperties
8+
import kotlin.reflect.jvm.javaField
9+
import kotlin.reflect.jvm.jvmErasure
10+
11+
val unitKType = getKType<Unit>()
12+
13+
inline fun <reified T> isNullable(): Boolean {
14+
return null is T
15+
}
16+
17+
inline fun <reified T> getKType() = typeOf<T>()
18+
19+
fun KType.strip(nullable: Boolean = isMarkedNullable): KType {
20+
return jvmErasure.createType(arguments, nullable)
21+
}
22+
23+
fun KType.deepStrip(nullable: Boolean = isMarkedNullable): KType {
24+
return jvmErasure.createType(arguments.map { it.copy(type = it.type?.deepStrip()) }, nullable)
25+
}
26+
27+
data class KTypeProperty(
28+
val name: String,
29+
val type: KType,
30+
val source: KProperty1<*, *>
31+
)
32+
33+
val KType.memberProperties: List<KTypeProperty>
34+
get() {
35+
val typeParameters = jvmErasure.typeParameters.zip(arguments).associate { Pair(it.first.name, it.second.type) }
36+
return jvmErasure.memberProperties.map {
37+
val retType = it.returnType
38+
val properType = when (val classifier = retType.classifier) {
39+
is KTypeParameter -> typeParameters[classifier.name] ?: it.returnType
40+
else -> it.returnType
41+
}
42+
KTypeProperty(it.name, properType, it)
43+
}
44+
}
45+
46+
val KClass<*>.isInterface get() = java.isInterface

0 commit comments

Comments
 (0)