11package io.github.mstachniuk.graphqlschemafromintrospectiongenerator.internal
22
33import com.beust.klaxon.Klaxon
4+ import io.github.mstachniuk.graphqlschemafromintrospectiongenerator.GeneratorSettings
45
56class GeneratorImpl {
67
78 val margin = " "
89 val primitiveScalars = listOf (" Boolean" , " Float" , " ID" , " Int" , " String" )
910
10- fun generate (input : String ): String {
11+ fun generate (
12+ input : String ,
13+ settings : GeneratorSettings
14+ ): String {
1115 val response = Klaxon ().parse<IntrospectionResponse >(input) ? : return " "
1216
13- val output = printSchema(response) + printTypes(response)
17+ val output = printSchema(response) + printTypes(response, settings )
1418
1519 return output.trimIndent().trimIndent()
1620 }
@@ -42,7 +46,7 @@ class GeneratorImpl {
4246 private fun isCustomSubscriptionType (response : IntrospectionResponse ) =
4347 response.data.schema.subscriptionType != null && response.data.schema.subscriptionType.name != " Subscription"
4448
45- private fun printTypes (response : IntrospectionResponse ): String {
49+ private fun printTypes (response : IntrospectionResponse , settings : GeneratorSettings ): String {
4650 val types = response.data.schema.types!!
4751 .filter { ! it.name.startsWith(" __" ) }
4852 .filter { ! primitiveScalars.contains(it.name) }
@@ -60,14 +64,14 @@ class GeneratorImpl {
6064 else -> " UNKNOWN_TYPE"
6165 }
6266
63- output + = printDescription(it, false )
67+ output + = printDescription(it, settings, false )
6468 output + = " $kind ${it.name}${printInterfaces(it.interfaces)} "
65- output + = printBody(it)
69+ output + = printBody(it, settings )
6670 }
6771 return output
6872 }
6973
70- private fun printBody (type : GraphQLType ): String {
74+ private fun printBody (type : GraphQLType , settings : GeneratorSettings ): String {
7175 if (type.kind == " UNION" ) {
7276 return " = ${type.possibleTypes
7377 .sortedBy { it.name }
@@ -79,23 +83,23 @@ class GeneratorImpl {
7983 var output = " {\n "
8084 type.fields.sortedBy { it.name }
8185 .forEach {
82- output + = printField(it)
86+ output + = printField(it, settings )
8387 }
8488 type.inputFields.sortedBy { it.name }
8589 .forEach {
86- output + = printField(it)
90+ output + = printField(it, settings )
8791 }
88- output + = printEnumTypes(type.enumValues)
92+ output + = printEnumTypes(type.enumValues, settings )
8993
9094 output + = " }\n\n "
9195 return output
9296 }
9397
94- private fun printEnumTypes (enumValues : List <GraphQLEnumType >): String {
98+ private fun printEnumTypes (enumValues : List <GraphQLEnumType >, settings : GeneratorSettings ): String {
9599 if (containsDescription(enumValues)) {
96100 val enums = enumValues
97101 .sortedBy { it.name }
98- .map { " ${printDescription(it)}$margin${it.name} " }
102+ .map { " ${printDescription(it, settings )}$margin${it.name} " }
99103 .joinToString(" \n " )
100104 return " $enums \n "
101105 }
@@ -107,13 +111,17 @@ class GeneratorImpl {
107111 }
108112 }
109113
110- private fun printDescription (it : Descriptable , addMargin : Boolean = true): String {
114+ private fun printDescription (it : Descriptable , settings : GeneratorSettings , addMargin : Boolean = true): String {
111115 var output = " "
112116 if (it.description.isNotBlank()) {
113117 if (addMargin) {
114118 output + = margin
115119 }
116- output + = " # ${it.description.trim().replace(" \n " , " \n $margin # " )} \n "
120+ val desc = when {
121+ settings.trimStartComments -> it.description.trim() // trim start and end
122+ else -> it.description.trimEnd() // trim only end otherwise
123+ }
124+ output + = " #${desc.replace(" \n " , " \n $margin #" )} \n "
117125 }
118126 return output
119127 }
@@ -128,9 +136,9 @@ class GeneratorImpl {
128136 return type.name
129137 }
130138
131- private fun printField (field : GraphQLField ): String {
132- val arguments = printArguments(field.args.sortedBy { it.name })
133- return " ${printDescription(field)}$margin${field.name}$arguments : ${printType(field.type)} " +
139+ private fun printField (field : GraphQLField , settings : GeneratorSettings ): String {
140+ val arguments = printArguments(field.args.sortedBy { it.name }, settings )
141+ return " ${printDescription(field, settings )}$margin${field.name}$arguments : ${printType(field.type)} " +
134142 " ${printDefaultValue(field)} \n "
135143 }
136144
@@ -152,11 +160,11 @@ class GeneratorImpl {
152160 }
153161 }
154162
155- private fun printArguments (args : List <GraphQLField >): String {
163+ private fun printArguments (args : List <GraphQLField >, settings : GeneratorSettings ): String {
156164 if (containsDescription(args)) {
157165 val arguments = args
158166 .map {
159- " ${printDescription(it)}$margin${it.name} : ${printType(it.type)}${printDefaultValue(it)} "
167+ " ${printDescription(it, settings )}$margin${it.name} : ${printType(it.type)}${printDefaultValue(it)} "
160168 }
161169 .joinToString(" \n " )
162170 if (arguments.isNotBlank()) {
0 commit comments