@@ -12,13 +12,19 @@ import io.github.ermadmi78.kobby.generator.kotlin.JacksonAnnotations.JSON_TYPE_I
1212import io.github.ermadmi78.kobby.generator.kotlin.JacksonAnnotations.JSON_TYPE_NAME
1313import io.github.ermadmi78.kobby.model.KobbyNode
1414import 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 */
2122internal 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 }
0 commit comments