1- @file:OptIn(InternalSerializationApi ::class )
2-
31package dev.adamko.kxstsgen
42
5- import kotlinx.serialization.InternalSerializationApi
63import kotlinx.serialization.KSerializer
74import kotlinx.serialization.modules.SerializersModule
85
@@ -25,306 +22,3 @@ class KxsTsGenerator(
2522 return processor.process()
2623 }
2724}
28-
29-
30- // class TsConverter(
31- // private val kxsTsConfig: KxsTsConfig,
32- // elementsState: Map<SerialDescriptor, TsElement> = emptyMap(),
33- // ) {
34- // private val elementsState: MutableMap<SerialDescriptor, TsElement> = elementsState.toMutableMap()
35- //
36- // private val descriptorExtractor = DescriptorExtractor(kxsTsConfig)
37- //
38- // val convertedElements: Set<TsElement>
39- // get() = elementsState.values.toSet()
40- //
41- // operator fun invoke(serializer: KSerializer<*>) {
42- // val descriptors: Map<SerialDescriptor, TsTypeRef> = descriptorExtractor(serializer)
43- // descriptors.keys.forEach { descriptor -> convertToTsElement(descriptor, descriptors) }
44- // }
45- //
46- // private fun convertToTsElement(
47- // descriptor: SerialDescriptor,
48- // typeRefs: Map<SerialDescriptor, TsTypeRef>,
49- // ): TsElement {
50- // return elementsState.getOrPut(descriptor) {
51- //
52- // when (descriptor.kind) {
53- // SerialKind.ENUM -> convertEnum(descriptor)
54- // SerialKind.CONTEXTUAL -> {
55- // // TODO contextual
56- // TsLiteral.Primitive.TsAny
57- // }
58- //
59- // PrimitiveKind.BOOLEAN -> TsLiteral.Primitive.TsBoolean
60- //
61- // PrimitiveKind.CHAR,
62- // PrimitiveKind.STRING -> TsLiteral.Primitive.TsString
63- //
64- // PrimitiveKind.BYTE,
65- // PrimitiveKind.SHORT,
66- // PrimitiveKind.INT,
67- // PrimitiveKind.LONG,
68- // PrimitiveKind.FLOAT,
69- // PrimitiveKind.DOUBLE -> TsLiteral.Primitive.TsNumber
70- //
71- // StructureKind.LIST -> convertList(descriptor, typeRefs)
72- // StructureKind.MAP -> convertMap(descriptor, typeRefs)
73- //
74- // StructureKind.CLASS,
75- // StructureKind.OBJECT,
76- // PolymorphicKind.SEALED,
77- // PolymorphicKind.OPEN -> when {
78- // descriptor.isInline -> convertTypeAlias(descriptor, typeRefs)
79- // else -> convertInterface(descriptor, typeRefs)
80- // }
81- // }
82- // }
83- // }
84- //
85- //
86- // private fun convertTypeAlias(
87- // structDescriptor: SerialDescriptor,
88- // typeRefs: Map<SerialDescriptor, TsTypeRef>,
89- // ): TsDeclaration {
90- // val resultId = createElementId(structDescriptor)
91- // val fieldDescriptor = structDescriptor.elementDescriptors.first()
92- // val fieldTypeRef = typeRefs[fieldDescriptor] ?: TsTypeRef.Unknown
93- // return TsDeclaration.TsTypeAlias(resultId, fieldTypeRef)
94- //
95- // }
96- //
97- //
98- // private fun convertInterface(
99- // structDescriptor: SerialDescriptor,
100- // typeRefs: Map<SerialDescriptor, TsTypeRef>,
101- // ): TsDeclaration {
102- // val resultId = createElementId(structDescriptor)
103- //
104- // val properties = structDescriptor.elementDescriptors.mapIndexed { index, fieldDescriptor ->
105- // val name = structDescriptor.getElementName(index)
106- // val fieldTypeRef = typeRefs[fieldDescriptor] ?: TsTypeRef.Unknown
107- // when {
108- // structDescriptor.isElementOptional(index) -> TsProperty.Optional(name, fieldTypeRef)
109- // else -> TsProperty.Required(name, fieldTypeRef)
110- // }
111- // }.toSet()
112- // return TsDeclaration.TsInterface(resultId, properties, TsPolymorphicDiscriminator.Open)
113- // }
114- //
115- //
116- // private fun convertEnum(
117- // enumDescriptor: SerialDescriptor,
118- // ): TsDeclaration.TsEnum {
119- // val resultId = createElementId(enumDescriptor)
120- // return TsDeclaration.TsEnum(resultId, enumDescriptor.elementNames.toSet())
121- // }
122- //
123- //
124- // private fun convertList(
125- // listDescriptor: SerialDescriptor,
126- // typeRefs: Map<SerialDescriptor, TsTypeRef>,
127- // ): TsLiteral.TsList {
128- // val elementDescriptor = listDescriptor.elementDescriptors.first()
129- // val elementTypeRef = typeRefs[elementDescriptor] ?: TsTypeRef.Unknown
130- // return TsLiteral.TsList(elementTypeRef)
131- // }
132- //
133- //
134- // private fun convertMap(
135- // mapDescriptor: SerialDescriptor,
136- // typeRefs: Map<SerialDescriptor, TsTypeRef>,
137- // ): TsLiteral.TsMap {
138- //
139- // val (keyDescriptor, valueDescriptor) = mapDescriptor.elementDescriptors.toList()
140- //
141- // val keyTypeRef = typeRefs[keyDescriptor] ?: TsTypeRef.Unknown
142- // val valueTypeRef = typeRefs[valueDescriptor] ?: TsTypeRef.Unknown
143- //
144- // val type = convertMapType(keyDescriptor)
145- //
146- // return TsLiteral.TsMap(keyTypeRef, valueTypeRef, type)
147- // }
148- //
149- //
150- // }
151- //
152- //
153- // class DescriptorExtractor(
154- // private val kxsTsConfig: KxsTsConfig,
155- // ) {
156- //
157- // operator fun invoke(serializer: KSerializer<*>): Map<SerialDescriptor, TsTypeRef> {
158- // return sequence {
159- // when (serializer) {
160- // is PolymorphicSerializer<*> -> {
161- // val x = kxsTsConfig.serializersModule.getPolymorphicDescriptors(serializer.descriptor)
162- // yieldAll(x)
163- // }
164- // is SealedClassSerializer<*> ->
165- // yield(serializer.descriptor)
166- // is ContextualSerializer<*> ->
167- // yield(extractContextualSerializer(serializer, kxsTsConfig)?.descriptor)
168- // else ->
169- // yield(serializer.descriptor)
170- // }
171- // }.filterNotNull()
172- // .flatMap { descriptor -> extractAll(descriptor) }
173- // .distinct()
174- // .associateWith { descriptor ->
175- // createTypeRef(descriptor)
176- // }
177- // }
178- //
179- // private fun extractAll(descriptor: SerialDescriptor): Sequence<SerialDescriptor> {
180- // return sequence {
181- // val seen = mutableSetOf<SerialDescriptor>()
182- // val deque = ArrayDeque<SerialDescriptor>()
183- // deque.addLast(descriptor)
184- // while (deque.isNotEmpty()) {
185- // val next = deque.removeFirst()
186- //
187- // val nextElementDescriptors = extractElementDescriptors(next)
188- //
189- // nextElementDescriptors
190- // .filter { it !in seen }
191- // .forEach { deque.addLast(it) }
192- //
193- // seen.add(next)
194- // yield(next)
195- // }
196- // }.distinct()
197- // }
198- //
199- //
200- // private fun extractElementDescriptors(serialDescriptor: SerialDescriptor): Iterable<SerialDescriptor> {
201- // return when (serialDescriptor.kind) {
202- // SerialKind.ENUM -> emptyList()
203- //
204- // SerialKind.CONTEXTUAL -> emptyList()
205- //
206- // PrimitiveKind.BOOLEAN,
207- // PrimitiveKind.BYTE,
208- // PrimitiveKind.CHAR,
209- // PrimitiveKind.SHORT,
210- // PrimitiveKind.INT,
211- // PrimitiveKind.LONG,
212- // PrimitiveKind.FLOAT,
213- // PrimitiveKind.DOUBLE,
214- // PrimitiveKind.STRING -> emptyList()
215- //
216- // StructureKind.CLASS,
217- // StructureKind.LIST,
218- // StructureKind.MAP,
219- // StructureKind.OBJECT -> serialDescriptor.elementDescriptors
220- //
221- // PolymorphicKind.SEALED,
222- // PolymorphicKind.OPEN -> serialDescriptor
223- // .elementDescriptors
224- // .filter { it.kind is PolymorphicKind }
225- // .flatMap { it.elementDescriptors }
226- // }
227- // }
228- //
229- //
230- // private fun createTypeRef(descriptor: SerialDescriptor): TsTypeRef {
231- // return when (descriptor.kind) {
232- // is PrimitiveKind -> {
233- // val tsPrimitive = when (descriptor.kind as PrimitiveKind) {
234- // PrimitiveKind.BOOLEAN -> TsLiteral.Primitive.TsBoolean
235- //
236- // PrimitiveKind.BYTE,
237- // PrimitiveKind.SHORT,
238- // PrimitiveKind.INT,
239- // PrimitiveKind.LONG,
240- // PrimitiveKind.FLOAT,
241- // PrimitiveKind.DOUBLE -> TsLiteral.Primitive.TsNumber
242- //
243- // PrimitiveKind.CHAR,
244- // PrimitiveKind.STRING -> TsLiteral.Primitive.TsString
245- // }
246- // TsTypeRef.Literal(tsPrimitive, descriptor.isNullable)
247- // }
248- //
249- // StructureKind.LIST -> {
250- // val elementDescriptor = descriptor.elementDescriptors.first()
251- // val elementTypeRef = createTypeRef(elementDescriptor)
252- // val listRef = TsLiteral.TsList(elementTypeRef)
253- // TsTypeRef.Literal(listRef, descriptor.isNullable)
254- // }
255- // StructureKind.MAP -> {
256- // val (keyDescriptor, valueDescriptor) = descriptor.elementDescriptors.toList()
257- // val keyTypeRef = createTypeRef(keyDescriptor)
258- // val valueTypeRef = createTypeRef(valueDescriptor)
259- // val type = convertMapType(keyDescriptor)
260- // val map = TsLiteral.TsMap(keyTypeRef, valueTypeRef, type)
261- // TsTypeRef.Literal(map, descriptor.isNullable)
262- // }
263- //
264- // SerialKind.CONTEXTUAL,
265- // PolymorphicKind.SEALED,
266- // PolymorphicKind.OPEN,
267- // SerialKind.ENUM,
268- // StructureKind.CLASS,
269- // StructureKind.OBJECT -> {
270- // val id = createElementId(descriptor)
271- // TsTypeRef.Named(id, descriptor.isNullable)
272- // }
273- // }
274- // }
275- //
276- // }
277- //
278- //
279- //
280- //
281- // private fun createElementId(descriptor: SerialDescriptor): TsElementId {
282- //
283- // val targetId = TsElementId(descriptor.serialName.removeSuffix("?"))
284- //
285- // return when (descriptor.kind) {
286- // PolymorphicKind.OPEN -> TsElementId(
287- // targetId.namespace + "." + targetId.name.substringAfter("<").substringBeforeLast(">")
288- // )
289- // PolymorphicKind.SEALED,
290- // PrimitiveKind.BOOLEAN,
291- // PrimitiveKind.BYTE,
292- // PrimitiveKind.CHAR,
293- // PrimitiveKind.DOUBLE,
294- // PrimitiveKind.FLOAT,
295- // PrimitiveKind.INT,
296- // PrimitiveKind.LONG,
297- // PrimitiveKind.SHORT,
298- // PrimitiveKind.STRING,
299- // SerialKind.CONTEXTUAL,
300- // SerialKind.ENUM,
301- // StructureKind.CLASS,
302- // StructureKind.LIST,
303- // StructureKind.MAP,
304- // StructureKind.OBJECT -> targetId
305- // }
306- // }
307- //
308- // private fun convertMapType(keyDescriptor: SerialDescriptor): TsLiteral.TsMap.Type {
309- // return when (keyDescriptor.kind) {
310- // SerialKind.ENUM -> TsLiteral.TsMap.Type.MAPPED_OBJECT
311- //
312- // PrimitiveKind.STRING -> TsLiteral.TsMap.Type.INDEX_SIGNATURE
313- //
314- // SerialKind.CONTEXTUAL,
315- // PrimitiveKind.BOOLEAN,
316- // PrimitiveKind.BYTE,
317- // PrimitiveKind.CHAR,
318- // PrimitiveKind.SHORT,
319- // PrimitiveKind.INT,
320- // PrimitiveKind.LONG,
321- // PrimitiveKind.FLOAT,
322- // PrimitiveKind.DOUBLE,
323- // StructureKind.CLASS,
324- // StructureKind.LIST,
325- // StructureKind.MAP,
326- // StructureKind.OBJECT,
327- // PolymorphicKind.SEALED,
328- // PolymorphicKind.OPEN -> TsLiteral.TsMap.Type.MAP
329- // }
330- // }
0 commit comments