Skip to content

Commit 0073a4f

Browse files
committed
remove duplicate branches of if (dto.serialization.enabled) flows
1 parent c523452 commit 0073a4f

6 files changed

Lines changed: 72 additions & 166 deletions

File tree

kobby-generator-kotlin/src/main/kotlin/io/github/ermadmi78/kobby/generator/kotlin/_layout.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,8 @@ private fun String.isKotlinIdentifier(): Boolean {
119119
internal val KotlinDtoLayout.errorsType: TypeName
120120
get() = LIST.parameterizedBy(graphql.errorClass)
121121

122-
internal val KotlinDtoLayout.extensionsType: TypeName
123-
get() = if (serialization.enabled) {
124-
SerializationJson.JSON_OBJECT
125-
} else {
126-
MAP.parameterizedBy(STRING, ANY.nullable())
127-
}
128-
122+
internal val KotlinLayout.extensionsType: TypeName
123+
get() = objectType()
129124
//******************************************************************************************************************
130125
// KotlinDtoGraphQLLayout
131126
//******************************************************************************************************************
@@ -303,11 +298,7 @@ internal val KotlinContextLayout.adapterArgQuery: Pair<String, TypeName>
303298
get() = "query" to STRING
304299

305300
internal val KotlinLayout.adapterArgVariables: Pair<String, TypeName>
306-
get() = if (dto.serialization.enabled) {
307-
"variables" to SerializationJson.JSON_OBJECT
308-
} else {
309-
"variables" to MAP.parameterizedBy(STRING, ANY.nullable())
310-
}
301+
get() = "variables" to objectType()
311302

312303
internal val KotlinContextLayout.adapterArgBlock: String
313304
get() = "block"
@@ -435,12 +426,7 @@ internal val KotlinAdapterKtorLayout.compositePropertyRequestHeaders: String get
435426
internal val KotlinAdapterKtorLayout.compositePropertySubscriptionPayload: String get() = "subscriptionPayload"
436427

437428
internal val KotlinLayout.compositePropertySubscriptionPayloadType: TypeName
438-
get() = if (dto.serialization.enabled) {
439-
SerializationJson.JSON_OBJECT.nullable()
440-
} else {
441-
MAP.parameterizedBy(STRING, ANY.nullable()).nullable()
442-
}
443-
429+
get() = objectType().nullable()
444430
internal val KotlinAdapterKtorLayout.compositePropertySubscriptionReceiveTimeoutMillis: String
445431
get() = "subscriptionReceiveTimeoutMillis"
446432
internal val KotlinAdapterKtorLayout.compositePropertyHttpTokenHeader: String
@@ -469,4 +455,16 @@ internal val KotlinAdapterKtorLayout.compositeFunReceiveMessage: String get() =
469455
//******************************************************************************************************************
470456
@Suppress("CHANGING_ARGUMENTS_EXECUTION_ORDER_FOR_NAMED_VARARGS")
471457
internal val KotlinDtoLayout.adapterListenerLambda: LambdaTypeName
472-
get() = LambdaTypeName.get(parameters = arrayOf(graphql.requestClass), returnType = UNIT)
458+
get() = LambdaTypeName.get(parameters = arrayOf(graphql.requestClass), returnType = UNIT)
459+
460+
internal fun KotlinLayout.objectCode(arg: Any): CodeBlock = if (dto.serialization.enabled) {
461+
CodeBlock.of("%T(%L)", SerializationJson.JSON_OBJECT, arg)
462+
} else {
463+
CodeBlock.of("%L", arg)
464+
}
465+
466+
internal fun KotlinLayout.objectType(nullable: Boolean = true): TypeName = if (dto.serialization.enabled) {
467+
SerializationJson.JSON_OBJECT
468+
} else {
469+
MAP.parameterizedBy(STRING, if (nullable) ANY.nullable() else ANY)
470+
}

kobby-generator-kotlin/src/main/kotlin/io/github/ermadmi78/kobby/generator/kotlin/dto.kt

Lines changed: 36 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ import io.github.ermadmi78.kobby.generator.kotlin.JacksonAnnotations.JSON_TYPE_I
1212
import io.github.ermadmi78.kobby.generator.kotlin.JacksonAnnotations.JSON_TYPE_NAME
1313
import io.github.ermadmi78.kobby.model.KobbyNode
1414
import io.github.ermadmi78.kobby.model.KobbySchema
15+
import io.github.ermadmi78.kobby.model.KobbyType
1516

1617
/**
1718
* Created on 23.01.2021
1819
*
1920
* @author Dmitry Ermakov (ermadmi78@gmail.com)
2021
*/
2122
internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSpec> = with(layout) {
23+
24+
fun KobbyType.dto(): TypeName = if (dto.serialization.enabled) dtoTypeWithSerializer else dtoType
25+
fun elementType() = if (dto.serialization.enabled) SerializationJson.JSON_ELEMENT else ANY
26+
fun arrayType(): TypeName = if (dto.serialization.enabled) SerializationJson.JSON_ARRAY else LIST.parameterizedBy(ANY)
27+
2228
val files = mutableListOf<FileSpec>()
2329

2430
//******************************************************************************************************************
@@ -51,12 +57,7 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
5157
if (immutable) {
5258
buildPrimaryConstructorProperties {
5359
node.fields { field ->
54-
val fieldType = if (dto.serialization.enabled) {
55-
field.type.dtoTypeWithSerializer.nullable()
56-
} else {
57-
field.type.dtoType.nullable()
58-
}
59-
buildProperty(field.name, fieldType) {
60+
buildProperty(field.name, field.type.dto().nullable()) {
6061
field.comments {
6162
addKdoc("%L", it)
6263
}
@@ -71,12 +72,7 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
7172
}
7273
} else {
7374
node.fields { field ->
74-
val fieldType = if (dto.serialization.enabled) {
75-
field.type.dtoTypeWithSerializer.nullable()
76-
} else {
77-
field.type.dtoType.nullable()
78-
}
79-
buildProperty(field.name, fieldType) {
75+
buildProperty(field.name, field.type.dto().nullable()) {
8076
field.comments {
8177
addKdoc("%L", it)
8278
}
@@ -295,17 +291,12 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
295291
if (immutable) {
296292
buildPrimaryConstructorProperties {
297293
node.fields { field ->
298-
val fieldType = if (dto.serialization.enabled) {
299-
field.type.dtoTypeWithSerializer
300-
} else {
301-
field.type.dtoType
302-
}
303294
val defaultValue: CodeBlock? = field.defaultValue?.let { literal ->
304295
val args = mutableListOf<Any?>()
305296
val format = literal.buildInitializer(field.type, args)
306297
CodeBlock.of(format, *args.toTypedArray())
307298
}
308-
buildPropertyWithDefault(field.name, fieldType, defaultValue) {
299+
buildPropertyWithDefault(field.name, field.type.dto(), defaultValue) {
309300
field.comments {
310301
addKdoc("%L", it)
311302
}
@@ -323,17 +314,12 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
323314
}
324315
} else {
325316
node.fields { field ->
326-
val fieldType = if (dto.serialization.enabled) {
327-
field.type.dtoTypeWithSerializer.nullable()
328-
} else {
329-
field.type.dtoType.nullable()
330-
}
331317
val defaultValue: CodeBlock? = field.defaultValue?.let { literal ->
332318
val args = mutableListOf<Any?>()
333319
val format = literal.buildInitializer(field.type, args)
334320
CodeBlock.of(format, *args.toTypedArray())
335321
}
336-
buildProperty(field.name, fieldType) {
322+
buildProperty(field.name, field.type.dto().nullable()) {
337323
field.comments {
338324
addKdoc("%L", it)
339325
}
@@ -405,15 +391,9 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
405391
addModifiers(KModifier.DATA)
406392
buildPrimaryConstructorProperties {
407393
buildProperty("query", STRING)
408-
409-
if (dto.serialization.enabled) {
410-
buildProperty("variables", SerializationJson.JSON_OBJECT.nullable())
411-
} else {
412-
buildProperty("variables", MAP.parameterizedBy(STRING, ANY.nullable()).nullable()) {
413-
jacksonIncludeNonEmpty()
414-
}
394+
buildProperty("variables", objectType().nullable()) {
395+
jacksonIncludeNonEmpty()
415396
}
416-
417397
buildProperty("operationName", STRING.nullable()) {
418398
jacksonIncludeNonAbsent()
419399
}
@@ -451,24 +431,18 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
451431
buildProperty("errorType", STRING.nullable()) {
452432
jacksonIncludeNonAbsent()
453433
}
454-
455-
if (dto.serialization.enabled) {
456-
buildProperty("path", SerializationJson.JSON_ARRAY.nullable())
457-
buildProperty("extensions", SerializationJson.JSON_OBJECT.nullable())
458-
} else {
459-
buildProperty("path", LIST.parameterizedBy(ANY).nullable()) {
460-
jacksonIncludeNonEmpty()
461-
}
462-
buildProperty("extensions", MAP.parameterizedBy(STRING, ANY.nullable()).nullable()) {
463-
jacksonIncludeNonEmpty()
464-
}
434+
buildProperty("path", arrayType().nullable()) {
435+
jacksonIncludeNonEmpty()
436+
}
437+
buildProperty("extensions", objectType().nullable()) {
438+
jacksonIncludeNonEmpty()
465439
}
466440
}
467441
}
468442
}
469443

470444
val argErrors = "errors" to dto.errorsType.nullable()
471-
val argExtensions = "extensions" to dto.extensionsType.nullable()
445+
val argExtensions = "extensions" to extensionsType.nullable()
472446

473447
// GraphQL Exception
474448
files += buildFile(dto.graphql.packageName, dto.graphql.exceptionName) {
@@ -710,83 +684,59 @@ internal fun generateDto(schema: KobbySchema, layout: KotlinLayout): List<FileSp
710684
WsMessage.WS_CLIENT_MESSAGE_CONNECTION_INIT -> {
711685
addModifiers(KModifier.DATA)
712686
buildPrimaryConstructorProperties {
713-
if (dto.serialization.enabled) {
714-
buildProperty("payload", SerializationJson.JSON_OBJECT.nullable())
715-
} else {
716-
buildProperty("payload", MAP.parameterizedBy(STRING, ANY.nullable()).nullable())
717-
customizeConstructor {
718-
jacksonizeConstructor()
719-
}
687+
buildProperty("payload", objectType().nullable())
688+
customizeConstructor {
689+
jacksonizeConstructor()
720690
}
721691
}
722692
}
723693

724694
WsMessage.WS_SERVER_MESSAGE_CONNECTION_ACK -> {
725695
addModifiers(KModifier.DATA)
726696
buildPrimaryConstructorProperties {
727-
if (dto.serialization.enabled) {
728-
buildProperty("payload", SerializationJson.JSON_ELEMENT.nullable())
729-
} else {
730-
buildProperty("payload", ANY.nullable())
731-
customizeConstructor {
732-
jacksonizeConstructor()
733-
}
697+
buildProperty("payload", elementType().nullable())
698+
customizeConstructor {
699+
jacksonizeConstructor()
734700
}
735701
}
736702
}
737703

738704
WsMessage.WS_CLIENT_MESSAGE_PING -> {
739705
addModifiers(KModifier.DATA)
740706
buildPrimaryConstructorProperties {
741-
if (dto.serialization.enabled) {
742-
buildProperty("payload", SerializationJson.JSON_ELEMENT.nullable())
743-
} else {
744-
buildProperty("payload", ANY.nullable())
745-
customizeConstructor {
746-
jacksonizeConstructor()
747-
}
707+
buildProperty("payload", elementType().nullable())
708+
customizeConstructor {
709+
jacksonizeConstructor()
748710
}
749711
}
750712
}
751713

752714
WsMessage.WS_CLIENT_MESSAGE_PONG -> {
753715
addModifiers(KModifier.DATA)
754716
buildPrimaryConstructorProperties {
755-
if (dto.serialization.enabled) {
756-
buildProperty("payload", SerializationJson.JSON_ELEMENT.nullable())
757-
} else {
758-
buildProperty("payload", ANY.nullable())
759-
customizeConstructor {
760-
jacksonizeConstructor()
761-
}
717+
buildProperty("payload", elementType().nullable())
718+
customizeConstructor {
719+
jacksonizeConstructor()
762720
}
763721
}
764722
}
765723

766724
WsMessage.WS_SERVER_MESSAGE_PING -> {
767725
addModifiers(KModifier.DATA)
768726
buildPrimaryConstructorProperties {
769-
if (dto.serialization.enabled) {
770-
buildProperty("payload", SerializationJson.JSON_ELEMENT.nullable())
771-
} else {
772-
buildProperty("payload", ANY.nullable())
773-
customizeConstructor {
774-
jacksonizeConstructor()
775-
}
727+
buildProperty("payload", elementType().nullable())
728+
customizeConstructor {
729+
jacksonizeConstructor()
776730
}
777731
}
778732
}
779733

780734
WsMessage.WS_SERVER_MESSAGE_PONG -> {
781735
addModifiers(KModifier.DATA)
782736
buildPrimaryConstructorProperties {
783-
if (dto.serialization.enabled) {
784-
buildProperty("payload", SerializationJson.JSON_ELEMENT.nullable())
785-
} else {
786-
buildProperty("payload", ANY.nullable())
787-
customizeConstructor {
788-
jacksonizeConstructor()
789-
}
737+
buildProperty("payload", elementType().nullable())
738+
customizeConstructor {
739+
jacksonizeConstructor()
790740
}
791741
}
792742
}

kobby-generator-kotlin/src/main/kotlin/io/github/ermadmi78/kobby/generator/kotlin/entity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private fun FileSpecBuilder.buildEntity(node: KobbyNode, layout: KotlinLayout) =
8484
buildFunction(entity.extensionsFunName) {
8585
addModifiers(ABSTRACT)
8686
addModifiers(OVERRIDE)
87-
returns(dto.extensionsType)
87+
returns(extensionsType)
8888

8989
addKdoc(
9090
"%L",

0 commit comments

Comments
 (0)