-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify type cache (#127)
* refactor: simplify type cache * test: recursive example
- Loading branch information
1 parent
6054dad
commit 6f4b189
Showing
14 changed files
with
131 additions
and
116 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
12 changes: 12 additions & 0 deletions
12
example/src/main/kotlin/com/expedia/graphql/sample/model/Node.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,12 @@ | ||
package com.expedia.graphql.sample.model | ||
|
||
/** | ||
* Represents a recursive type that references itself, | ||
* similar to a tree node structure. | ||
*/ | ||
data class Node( | ||
val id: Int, | ||
val value: String, | ||
val parent: Node?, | ||
var children: List<Node> | ||
) |
22 changes: 22 additions & 0 deletions
22
example/src/main/kotlin/com/expedia/graphql/sample/query/RecursiveQuery.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,22 @@ | ||
package com.expedia.graphql.sample.query | ||
|
||
import com.expedia.graphql.annotations.GraphQLDescription | ||
import com.expedia.graphql.sample.model.Node | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class RecursiveQuery : Query { | ||
|
||
private final val root = Node(id = 0, value = "root", parent = null, children = emptyList()) | ||
private final val nodeA = Node(id = 1, value = "A", parent = root, children = emptyList()) | ||
private final val nodeB = Node(id = 2, value = "B", parent = root, children = emptyList()) | ||
private final val nodeC = Node(id = 3, value = "C", parent = nodeB, children = emptyList()) | ||
|
||
init { | ||
root.children = listOf(nodeA, nodeB) | ||
nodeB.children = listOf(nodeC) | ||
} | ||
|
||
@GraphQLDescription("Returns the root of a node graph") | ||
fun nodeGraph(): Node = root | ||
} |
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/com/expedia/graphql/exceptions/CouldNotCastToKClassException.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/com/expedia/graphql/exceptions/CouldNotGetNameOfKTypeException.kt
This file was deleted.
Oops, something went wrong.
6 changes: 4 additions & 2 deletions
6
src/main/kotlin/com/expedia/graphql/exceptions/TypeNotSupportedException.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,7 +1,9 @@ | ||
package com.expedia.graphql.exceptions | ||
|
||
import kotlin.reflect.KType | ||
|
||
/** | ||
* Thrown when the generator does not have a type to map to in GraphQL or in the hooks. | ||
*/ | ||
class TypeNotSupportedException(typeName: String, packageList: List<String>) | ||
: GraphQLKotlinException("Cannot convert $typeName since it is outside the supported packages $packageList") | ||
class TypeNotSupportedException(kType: KType, packageList: List<String>) | ||
: GraphQLKotlinException("Cannot convert $kType since it is outside the supported packages $packageList") |
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
47 changes: 27 additions & 20 deletions
47
src/main/kotlin/com/expedia/graphql/generator/extensions/kTypeExtensions.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,38 +1,45 @@ | ||
package com.expedia.graphql.generator.extensions | ||
|
||
import com.expedia.graphql.exceptions.CouldNotCastToKClassException | ||
import com.expedia.graphql.exceptions.CouldNotGetNameOfKTypeException | ||
import com.expedia.graphql.exceptions.InvalidListTypeException | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.createType | ||
import kotlin.reflect.full.isSubclassOf | ||
import kotlin.reflect.jvm.jvmErasure | ||
|
||
private val primitiveArrayTypes = mapOf( | ||
IntArray::class to Int::class, | ||
LongArray::class to Long::class, | ||
ShortArray::class to Short::class, | ||
FloatArray::class to Float::class, | ||
DoubleArray::class to Double::class, | ||
CharArray::class to Char::class, | ||
BooleanArray::class to Boolean::class | ||
) | ||
|
||
internal fun KType.getKClass() = this.jvmErasure | ||
|
||
@Throws(InvalidListTypeException::class) | ||
internal fun KType.getTypeOfFirstArgument(): KType = | ||
this.arguments.firstOrNull()?.type ?: throw InvalidListTypeException(this) | ||
|
||
@Throws(CouldNotCastToKClassException::class) | ||
internal fun KType.getKClass() = this.classifier as? KClass<*> ?: throw CouldNotCastToKClassException(this) | ||
|
||
internal fun KType.getArrayType(): KType { | ||
val kClass = this.getKClass() | ||
internal fun KType.getWrappedType(): KType { | ||
val primitiveClass = primitiveArrayTypes[this.getKClass()] | ||
return when { | ||
kClass.isSubclassOf(IntArray::class) -> Int::class.createType() | ||
kClass.isSubclassOf(LongArray::class) -> Long::class.createType() | ||
kClass.isSubclassOf(ShortArray::class) -> Short::class.createType() | ||
kClass.isSubclassOf(FloatArray::class) -> Float::class.createType() | ||
kClass.isSubclassOf(DoubleArray::class) -> Double::class.createType() | ||
kClass.isSubclassOf(CharArray::class) -> Char::class.createType() | ||
kClass.isSubclassOf(BooleanArray::class) -> Boolean::class.createType() | ||
primitiveClass != null -> primitiveClass.createType() | ||
else -> this.getTypeOfFirstArgument() | ||
} | ||
} | ||
|
||
@Throws(CouldNotGetNameOfKTypeException::class) | ||
internal fun KType.getSimpleName(): String = | ||
this.jvmErasure.simpleName ?: throw CouldNotGetNameOfKTypeException(this) | ||
internal fun KType.getWrappedName(): String { | ||
val isPrimitiveArray = primitiveArrayTypes.containsKey(this.getKClass()) | ||
return when { | ||
isPrimitiveArray -> this.getSimpleName() | ||
this.getKClass().isList() -> "List<${this.getWrappedType().getSimpleName()}>" | ||
this.getKClass().isArray() -> "Array<${this.getWrappedType().getSimpleName()}>" | ||
else -> this.getSimpleName() | ||
} | ||
} | ||
|
||
internal fun KType.getSimpleName(): String = this.getKClass().getSimpleName() | ||
|
||
internal val KType.qualifiedName: String | ||
get() = this.jvmErasure.getQualifiedName() | ||
get() = this.getKClass().getQualifiedName() |
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
Oops, something went wrong.