@@ -15,7 +15,7 @@ import kotlin.reflect.full.findAnnotation
1515import com.expedia.graphql.annotations.GraphQLDirective as DirectiveAnnotation
1616
1717internal fun KAnnotatedElement.graphQLDescription (): String? {
18- val directiveNames = listOfDirectives().map { normalizeDirectiveName(it ) }
18+ val directiveNames = listOfDirectives().map { it. normalizeDirectiveName() }
1919
2020 val description = this .findAnnotation<GraphQLDescription >()?.value
2121
@@ -36,13 +36,8 @@ private fun KAnnotatedElement.listOfDirectives(): List<String> {
3636
3737 return this .annotations.asSequence()
3838 .mapNotNull { it.getDirectiveInfo() }
39- .map {
40- val directiveName = it.getDirectiveInfo()?.name
41- val simpleName = it.annotationClass.simpleName
42-
43- when {
44- directiveName.isNullOrEmpty().not () -> " @$directiveName "
45- simpleName.isNullOrEmpty().not () -> " @$simpleName "
39+ .map { when {
40+ it.effectiveName != null -> " @${it.effectiveName} "
4641 else -> null
4742 }
4843 }
@@ -66,8 +61,13 @@ internal fun KAnnotatedElement.isGraphQLIgnored() = this.findAnnotation<GraphQLI
6661
6762internal fun KAnnotatedElement.isGraphQLID () = this .findAnnotation<GraphQLID >() != null
6863
69- internal fun Annotation.getDirectiveInfo (): DirectiveAnnotation ? =
70- this .annotationClass.annotations.find { it is DirectiveAnnotation } as ? DirectiveAnnotation
64+ private fun Annotation.getDirectiveInfo (): DirectiveInfo ? {
65+ val directiveAnnotation = this .annotationClass.annotations.find { it is DirectiveAnnotation } as ? DirectiveAnnotation
66+ return when {
67+ directiveAnnotation != null -> DirectiveInfo (this .annotationClass.simpleName ? : " " , directiveAnnotation)
68+ else -> null
69+ }
70+ }
7171
7272internal fun KAnnotatedElement.directives () =
7373 this .annotations.asSequence()
@@ -76,19 +76,16 @@ internal fun KAnnotatedElement.directives() =
7676 .toList()
7777
7878@Throws(CouldNotGetNameOfAnnotationException ::class )
79- private fun DirectiveAnnotation .getGraphQLDirective (): GraphQLDirective {
80- val kClass: KClass <out DirectiveAnnotation > = this .annotationClass
79+ private fun DirectiveInfo .getGraphQLDirective (): GraphQLDirective {
80+ val kClass: KClass <out DirectiveAnnotation > = this .annotation. annotationClass
8181 val builder = GraphQLDirective .newDirective()
82- val name: String = if (this .name.isNotEmpty()) {
83- this .name
84- } else {
85- kClass.simpleName ? : throw CouldNotGetNameOfAnnotationException (kClass)
86- }
82+ val name: String = this .effectiveName ? : throw CouldNotGetNameOfAnnotationException (kClass)
8783
8884 @Suppress(" Detekt.SpreadOperator" )
89- builder.name(normalizeDirectiveName(name))
90- .validLocations(* this .locations)
91- .description(this .description)
85+
86+ builder.name(name.normalizeDirectiveName())
87+ .validLocations(* this .annotation.locations)
88+ .description(this .annotation.description)
9289
9390 kClass.getValidFunctions().forEach { kFunction ->
9491 val propertyName = kFunction.name
@@ -106,4 +103,12 @@ private fun DirectiveAnnotation.getGraphQLDirective(): GraphQLDirective {
106103 return builder.build()
107104}
108105
109- private fun normalizeDirectiveName (string : String ) = CaseFormat .UPPER_CAMEL .to(CaseFormat .LOWER_CAMEL , string)
106+ private fun String.normalizeDirectiveName () = CaseFormat .UPPER_CAMEL .to(CaseFormat .LOWER_CAMEL , this )
107+
108+ private data class DirectiveInfo (private val name : String , val annotation : DirectiveAnnotation ) {
109+ val effectiveName: String? = when {
110+ annotation.name.isNotEmpty() -> annotation.name
111+ name.isNotEmpty() -> name
112+ else -> null
113+ }
114+ }
0 commit comments