Skip to content

Commit 2a0a17a

Browse files
smyrickbrennantaylor
authored andcommitted
refactor: extract simple name for better test coverage (#114)
* refactor: extract simple name for better test coverage * fix: remove suppression * feat: move input name to extension * doc: update javadocs of exceptions
1 parent af7eea0 commit 2a0a17a

24 files changed

+202
-129
lines changed

src/main/kotlin/com/expedia/graphql/exceptions/CouldNotGetNameOfAnnotationException.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/kotlin/com/expedia/graphql/exceptions/CouldNotGetNameOfArgumentException.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/kotlin/com/expedia/graphql/exceptions/CouldNotGetNameOfEnumException.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/kotlin/com/expedia/graphql/exceptions/CouldNotGetJvmNameOfKTypeException.kt renamed to src/main/kotlin/com/expedia/graphql/exceptions/CouldNotGetNameOfKClassException.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.expedia.graphql.exceptions
22

3-
import kotlin.reflect.KType
3+
import kotlin.reflect.KClass
44

55
/**
6-
* Thrown when trying to generate a class and cannot resolve the jvm erasure name.
6+
* Thrown when trying to generate a class and cannot resolve the name.
77
*/
8-
class CouldNotGetJvmNameOfKTypeException(kType: KType?)
9-
: GraphQLKotlinException("Could not get the name of the KClass $kType")
8+
class CouldNotGetNameOfKClassException(kclass: KClass<*>)
9+
: GraphQLKotlinException("Could not get the name of the KClass $kclass")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.expedia.graphql.exceptions
2+
3+
import kotlin.reflect.KParameter
4+
5+
/**
6+
* Thrown when trying to generate a parameter and cannot resolve the name.
7+
*/
8+
class CouldNotGetNameOfKParameterException(kParameter: KParameter)
9+
: GraphQLKotlinException("Could not get name of the KParameter $kParameter")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.expedia.graphql.exceptions
2+
3+
import kotlin.reflect.KType
4+
5+
/**
6+
* Thrown when trying to generate a type and cannot resolve the name.
7+
*/
8+
class CouldNotGetNameOfKTypeException(kType: KType)
9+
: GraphQLKotlinException("Could not get the name of the KType $kType")

src/main/kotlin/com/expedia/graphql/generator/TypeBuilder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package com.expedia.graphql.generator
33
import com.expedia.graphql.generator.extensions.getKClass
44
import com.expedia.graphql.generator.extensions.isArray
55
import com.expedia.graphql.generator.extensions.isEnum
6-
import com.expedia.graphql.generator.extensions.isGraphQLInterface
7-
import com.expedia.graphql.generator.extensions.isGraphQLUnion
6+
import com.expedia.graphql.generator.extensions.isInterface
87
import com.expedia.graphql.generator.extensions.isList
8+
import com.expedia.graphql.generator.extensions.isUnion
99
import com.expedia.graphql.generator.extensions.wrapInNonNull
1010
import com.expedia.graphql.generator.state.KGraphQLType
1111
import com.expedia.graphql.generator.state.TypesCacheKey
@@ -48,8 +48,8 @@ internal open class TypeBuilder constructor(val generator: SchemaGenerator) {
4848
kClass.isEnum() -> @Suppress("UNCHECKED_CAST") (generator.enumType(kClass as KClass<Enum<*>>))
4949
kClass.isArray() -> generator.arrayType(type, inputType)
5050
kClass.isList() -> generator.listType(type, inputType)
51-
kClass.isGraphQLUnion() -> generator.unionType(kClass)
52-
kClass.isGraphQLInterface() -> generator.interfaceType(kClass)
51+
kClass.isUnion() -> generator.unionType(kClass)
52+
kClass.isInterface() -> generator.interfaceType(kClass)
5353
inputType -> generator.inputObjectType(kClass)
5454
else -> generator.objectType(kClass)
5555
}

src/main/kotlin/com/expedia/graphql/generator/extensions/directiveExtensions.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.expedia.graphql.generator.extensions
22

33
import com.expedia.graphql.annotations.GraphQLDirective
4-
import com.expedia.graphql.exceptions.CouldNotGetNameOfAnnotationException
54
import com.expedia.graphql.generator.SchemaGenerator
65
import com.google.common.base.CaseFormat
76
import graphql.schema.GraphQLArgument
@@ -21,14 +20,12 @@ private fun Annotation.getDirectiveInfo(): DirectiveInfo? {
2120
.firstOrNull()
2221
}
2322

24-
@Throws(CouldNotGetNameOfAnnotationException::class)
2523
private fun DirectiveInfo.getGraphQLDirective(generator: SchemaGenerator): graphql.schema.GraphQLDirective {
2624
val directiveClass = this.directive.annotationClass
27-
val name: String = this.effectiveName ?: throw CouldNotGetNameOfAnnotationException(directiveClass)
2825

2926
@Suppress("Detekt.SpreadOperator")
3027
val builder = graphql.schema.GraphQLDirective.newDirective()
31-
.name(name)
28+
.name(this.effectiveName)
3229
.validLocations(*this.directiveAnnotation.locations)
3330
.description(this.directiveAnnotation.description)
3431

@@ -52,8 +49,8 @@ private fun DirectiveInfo.getGraphQLDirective(generator: SchemaGenerator): graph
5249
private fun String.normalizeDirectiveName() = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, this)
5350

5451
private data class DirectiveInfo(val directive: Annotation, val directiveAnnotation: GraphQLDirective) {
55-
val effectiveName: String? = when {
52+
val effectiveName: String = when {
5653
directiveAnnotation.name.isNotEmpty() -> directiveAnnotation.name
57-
else -> directive.annotationClass.simpleName?.normalizeDirectiveName()
54+
else -> directive.annotationClass.getSimpleName().normalizeDirectiveName()
5855
}
5956
}

src/main/kotlin/com/expedia/graphql/generator/extensions/kClassExtensions.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.expedia.graphql.generator.extensions
22

3-
import com.expedia.graphql.hooks.SchemaGeneratorHooks
3+
import com.expedia.graphql.exceptions.CouldNotGetNameOfKClassException
44
import com.expedia.graphql.generator.functionFilters
55
import com.expedia.graphql.generator.propertyFilters
6+
import com.expedia.graphql.hooks.SchemaGeneratorHooks
67
import kotlin.reflect.KClass
78
import kotlin.reflect.KFunction
89
import kotlin.reflect.KParameter
@@ -27,13 +28,19 @@ internal fun KClass<*>.findConstructorParamter(name: String): KParameter? =
2728
?.parameters
2829
?.find { it.name == name }
2930

30-
internal fun KClass<*>.isGraphQLInterface(): Boolean = this.java.isInterface
31+
internal fun KClass<*>.isInterface(): Boolean = this.java.isInterface
3132

32-
internal fun KClass<*>.isGraphQLUnion(): Boolean =
33-
this.isGraphQLInterface() && this.declaredMemberProperties.isEmpty() && this.declaredMemberFunctions.isEmpty()
33+
internal fun KClass<*>.isUnion(): Boolean =
34+
this.isInterface() && this.declaredMemberProperties.isEmpty() && this.declaredMemberFunctions.isEmpty()
3435

3536
internal fun KClass<*>.isEnum(): Boolean = this.isSubclassOf(Enum::class)
3637

3738
internal fun KClass<*>.isList(): Boolean = this.isSubclassOf(List::class)
3839

3940
internal fun KClass<*>.isArray(): Boolean = this.java.isArray
41+
42+
@Throws(CouldNotGetNameOfKClassException::class)
43+
internal fun KClass<*>.getSimpleName(): String =
44+
this.simpleName ?: throw CouldNotGetNameOfKClassException(this)
45+
46+
internal fun KClass<*>.getInputClassName(): String = "${this.getSimpleName()}Input"
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.expedia.graphql.generator.extensions
22

33
import com.expedia.graphql.annotations.GraphQLContext
4+
import com.expedia.graphql.exceptions.CouldNotGetNameOfKParameterException
45
import com.expedia.graphql.exceptions.InvalidInputFieldTypeException
56
import kotlin.reflect.KParameter
67
import kotlin.reflect.full.findAnnotation
78
import kotlin.reflect.jvm.jvmErasure
89

910
@Throws(InvalidInputFieldTypeException::class)
1011
internal fun KParameter.throwIfUnathorizedInterface() {
11-
if (this.type.jvmErasure.java.isInterface) throw InvalidInputFieldTypeException()
12+
if (this.type.jvmErasure.isInterface()) throw InvalidInputFieldTypeException()
1213
}
1314

1415
internal fun KParameter.isGraphQLContext() = this.findAnnotation<GraphQLContext>() != null
16+
17+
@Throws(CouldNotGetNameOfKParameterException::class)
18+
internal fun KParameter.getName(): String =
19+
this.name ?: throw CouldNotGetNameOfKParameterException(this)

0 commit comments

Comments
 (0)