-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
150 lines (134 loc) · 5.68 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
group = "com.zhelenskiy"
version = "1.0"
fun generateClassSerializers() = buildString {
operator fun String.unaryPlus() = appendLine(this)
val tab = " "
val generateIndices = 0u..64u
fun generateClassSerializerByPropertiesCount(n: UInt) {
+"open class ClassKSerializer$n<${(listOf("T") + (1u..n).map { "R$it" }).joinToString(", ")}>("
+"${tab}private val descriptorName: String,"
for (i in 1u..n) {
+"${tab}private val serializerPart$i: Property<T, R$i>,"
}
+"${tab}private val build: (${(1u..n).joinToString(", ") { "R$it" }}) -> T,"
+") : KSerializer<T> {"
+"${tab}final override val descriptor: SerialDescriptor = buildClassSerialDescriptor(descriptorName) {"
for (i in 1u..n) {
+"${tab}${tab}element(serializerPart${i}.name, serializerPart${i}.serializer.descriptor)"
}
+"${tab}}"
+""
+"${tab}final override fun serialize(encoder: Encoder, value: T) = encoder.encodeStructure(descriptor) {"
for (i in 1u..n) {
+"${tab}${tab}encodeSerializableElement(descriptor, ${i - 1u}, serializerPart${i}.serializer, serializerPart${i}.generator(value))"
}
+"${tab}}"
+""
if (n > 0u) {
+"${tab}@OptIn(ExperimentalSerializationApi::class)"
}
+"${tab}final override fun deserialize(decoder: Decoder): T = decoder.decodeStructure(descriptor) {"
if (n == 0u) {
+"${tab}${tab}build()"
} else {
+"${tab}${tab}if (decodeSequentially()) {"
+"${tab}${tab}${tab}build("
for (i in 1u..n) {
+"${tab}${tab}${tab}${tab}decodeSerializableElement(descriptor, ${i - 1u}, serializerPart${i}.serializer),"
}
+"${tab}${tab}${tab})"
+"${tab}${tab}} else {"
for (i in 1u..n) {
+"${tab}${tab}${tab}var value$i: R$i? = null"
}
+"${tab}${tab}${tab}while (true) {"
+"${tab}${tab}${tab}${tab}when (val index = decodeElementIndex(descriptor)) {"
for (i in 1u..n) {
+"${tab}${tab}${tab}${tab}${tab}${i - 1u} -> value$i = decodeSerializableElement(descriptor, ${i - 1u}, serializerPart${i}.serializer)"
}
+"${tab}${tab}${tab}${tab}${tab}CompositeDecoder.DECODE_DONE -> break"
+"${tab}${tab}${tab}${tab}${tab}else -> error(\"Unexpected index: \$index\")"
+"${tab}${tab}${tab}${tab}}"
+"${tab}${tab}${tab}}"
+"${tab}${tab}${tab}return@decodeStructure build("
for (i in 1u..n) {
+"${tab}${tab}${tab}${tab}value${i} ?: error(\"Absent field: \${serializerPart${i}.name}\"),"
}
+"${tab}${tab}${tab})"
+"${tab}${tab}}"
}
+"${tab}}"
+"}"
+""
+"fun <${(listOf("T") + (1u..n).map { "R$it" }).joinToString(", ")}> classSerializer("
+"${tab}descriptorName: String,"
for (i in 1u..n) {
+"${tab}serializerPart$i: Property<T, R$i>,"
}
+"${tab}build: (${(1u..n).joinToString(", ") { "R$it" }}) -> T,"
+") : KSerializer<T> = ClassKSerializer$n("
+"${tab}descriptorName,"
for (i in 1u..n) {
+"${tab}serializerPart$i,"
}
+"${tab}build,"
+")"
}
+"@file:Suppress(\"DuplicatedCode\")"
+""
+"package com.zhelenskiy.serialization"
+""
+"import kotlinx.serialization.ExperimentalSerializationApi"
+"import kotlinx.serialization.KSerializer"
+"import kotlinx.serialization.descriptors.SerialDescriptor"
+"import kotlinx.serialization.descriptors.buildClassSerialDescriptor"
+"import kotlinx.serialization.encoding.CompositeDecoder"
+"import kotlinx.serialization.encoding.Decoder"
+"import kotlinx.serialization.encoding.Encoder"
+"import kotlinx.serialization.encoding.decodeStructure"
+"import kotlinx.serialization.encoding.encodeStructure"
+"import kotlinx.serialization.serializer"
+"import kotlin.reflect.KProperty1"
+""
+"// This file is generated by a :generateClassSerializers task in build.gradle.kts"
+""
+""
+"data class Property<T, R>("
+"${tab}val name: String,"
+"${tab}val serializer: KSerializer<R>,"
+"${tab}val generator: (T) -> R,"
+") {"
+"${tab}constructor(property: KProperty1<T, R>, serializer: KSerializer<R>) : this(property.name, serializer, property)"
+"${tab}companion object {"
+"${tab}${tab}inline operator fun <T, reified R> invoke(name: String, noinline generator: (T) -> R): Property<T, R> ="
+"${tab}${tab}${tab}Property(name, serializer<R>(), generator)"
+""
+"${tab}${tab}inline operator fun <T, reified R> invoke(property: KProperty1<T, R>): Property<T, R> ="
+"${tab}${tab}${tab}Property(property, serializer<R>())"
+"${tab}}"
+"}"
for (n in generateIndices) {
+""
generateClassSerializerByPropertiesCount(n)
}
}
fun File.withCreatedParentDirectories() = apply { parentFile?.mkdirs() }
tasks.register("generateClassSerializers") {
val sourceFile = generateClassSerializers()
projectDir.resolve("src/com/zhelenskiy/serialization/ClassSerializer.kt").withCreatedParentDirectories().writeText(sourceFile)
}
tasks.withType<KotlinCompile> {
dependsOn(":generateClassSerializers")
}
publishing {
repositories {
maven {
url = uri("https://maven.pkg.github.com/zhelenskiy/kotlinx-serialization-builder")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}