Skip to content

Commit

Permalink
All tests passing...
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Nov 21, 2023
1 parent 8515728 commit 643a157
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ internal class ClassCsvDecoder(
private var elementIndex = 0
private var columnIndex = 0

override fun decodeElementIndex(descriptor: SerialDescriptor): Int = when {
reader.isDone -> DECODE_DONE
elementIndex >= descriptor.elementsCount -> DECODE_DONE
classHeaders != null && columnIndex >= classHeaders.size -> DECODE_DONE
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
// if(descriptor != classHeaders?.descriptor) throw Exception("ClassCsvDecoder with ${descriptor.serialName} cannot decode an index for ${classHeaders?.descriptor?.serialName}")
return when {
reader.isDone -> DECODE_DONE
elementIndex >= descriptor.elementsCount -> DECODE_DONE
classHeaders != null && columnIndex >= classHeaders.size -> DECODE_DONE

classHeaders != null -> {
when (val result = classHeaders[columnIndex]) {
UNKNOWN_NAME -> {
ignoreColumn()
decodeElementIndex(descriptor)
}
classHeaders != null -> {
println("${descriptor.serialName} decoded column index ${columnIndex} to be ${classHeaders[columnIndex]} (${classHeaders[columnIndex]?.takeIf { it >= 0 }?.let { descriptor.getElementName(it)}}) due to column ${columnIndex} in ${classHeaders}")
when (val result = classHeaders[columnIndex]) {
UNKNOWN_NAME -> {
ignoreColumn()
decodeElementIndex(descriptor)
}

null -> UNKNOWN_NAME
else -> result.also { println("${descriptor.serialName} decoded ${it} (${descriptor.getElementName(it)}) due to column ${columnIndex} in ${classHeaders.toString(descriptor)}") }
null -> UNKNOWN_NAME
else -> result
}
}
}

else -> elementIndex.also { println("${descriptor.serialName} decoded ${it} (${descriptor.getElementName(it)})") }
else -> elementIndex.also { println("${descriptor.serialName} decoded ${it} (${descriptor.getElementName(it)}) because there were no class headers") }
}
}

override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
Expand All @@ -51,7 +55,7 @@ internal class ClassCsvDecoder(
csv,
reader,
this,
classHeaders?.getSubHeaders(decodeElementIndex(descriptor))
classHeaders?.let { it.getSubHeaders(columnIndex) ?: throw IllegalStateException("Could not find sub headers at index $elementIndex; see $classHeaders}") }
)

else ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kotlinx.serialization.csv.decode

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.csv.Csv
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.serialization.csv.UnsupportedSerialDescriptorException
import kotlinx.serialization.csv.config.CsvConfig
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.SerialKind
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.CompositeDecoder.Companion.UNKNOWN_NAME
Expand Down Expand Up @@ -133,7 +134,7 @@ internal abstract class CsvDecoder(
}

private fun readHeaders(descriptor: SerialDescriptor, prefix: String): Headers {
val headers = Headers()
val headers = Headers(descriptor)
var position = 0
while (!reader.isDone && reader.isFirstRecord) {
val offset = reader.offset
Expand All @@ -153,14 +154,18 @@ internal abstract class CsvDecoder(
headers[position] = headerIndex
println("SET $position TO $header")
reader.unmark()
val desc = descriptor.getElementDescriptor(headerIndex)
if(desc.kind == StructureKind.CLASS && desc.isNullable) position--
} else {
val name = header.substringBefore(config.headerSeparator)
val nameIndex = descriptor.getElementIndex(name)
if (nameIndex != UNKNOWN_NAME) {
val childDesc = descriptor.getElementDescriptor(nameIndex)
if (childDesc.kind is StructureKind.CLASS) {
reader.reset()
headers[nameIndex] = readHeaders(childDesc, "$prefix$name.")
if(headers[position] == null)
headers[position] = nameIndex
headers[position] = readHeaders(childDesc, "$prefix$name.")
} else {
reader.unmark()
}
Expand All @@ -175,7 +180,7 @@ internal abstract class CsvDecoder(
}
position++
}
return headers.also { println(it.toString(descriptor)) }
return headers.also { println(it.toString()) }
}

protected fun readTrailingDelimiter() {
Expand All @@ -184,7 +189,7 @@ internal abstract class CsvDecoder(
}
}

internal class Headers {
internal class Headers(val descriptor: SerialDescriptor) {
private val map = mutableMapOf<Int, Int>()
private val subHeaders = mutableMapOf<Int, Headers>()

Expand All @@ -206,10 +211,7 @@ internal abstract class CsvDecoder(
}

override fun toString(): String {
return "Headers(map=${map}, subHeaders=${subHeaders})"
}
fun toString(context: SerialDescriptor): String {
return "Headers(map=${map.mapValues { if(it.value in 0 until context.elementsCount) context.getElementName(it.value) else "???" }})"
return "Headers(descriptor=${descriptor.serialName}, map=${map.mapValues { if(it.value in 0 until descriptor.elementsCount) descriptor.getElementName(it.value) else "???" }}, subHeaders=${subHeaders})"
}
}

Expand All @@ -224,7 +226,7 @@ internal abstract class CsvDecoder(
else -> {}
}
}
if(deserializer.descriptor.isNullable && deserializer.descriptor.kind == StructureKind.CLASS && deserializer.descriptor.elementsCount > 0) {
if(deserializer.descriptor.isNullable && deserializer.descriptor.kind == StructureKind.CLASS) {
val isPresent = reader.readColumn().toBoolean()
println("READ PRESENT $isPresent")
if(isPresent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kotlinx.serialization.csv.decode

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.csv.Csv
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.encoding.CompositeDecoder
Expand Down Expand Up @@ -70,4 +72,19 @@ internal class RecordListCsvDecoder(
reader.reset()
}
}

override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
println("DECODING")
if (config.deferToFormatWhenVariableColumns != null) {
when (deserializer.descriptor.kind) {
is StructureKind.LIST,
is StructureKind.MAP,
is PolymorphicKind.OPEN -> {
return config.deferToFormatWhenVariableColumns!!.decodeFromString(deserializer, decodeColumn())
}
else -> {}
}
}
return deserializer.deserialize(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal class RootCsvDecoder(
}

override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
println("BEGIN STRUCT ${descriptor.kind}")
return when (descriptor.kind) {
StructureKind.LIST ->
// Top level list is treated as list of multiple records
Expand Down Expand Up @@ -65,7 +66,7 @@ internal class RootCsvDecoder(
else -> {}
}
}
if(deserializer.descriptor.isNullable && deserializer.descriptor.kind == StructureKind.CLASS && deserializer.descriptor.elementsCount > 0) {
if(deserializer.descriptor.isNullable && deserializer.descriptor.kind == StructureKind.CLASS) {
val isPresent = decodeBoolean()
println("Decoded present boolean ${isPresent}")
if(isPresent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kotlinx.serialization.csv.encode

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.csv.Csv
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.encoding.CompositeEncoder

/**
Expand Down Expand Up @@ -41,4 +44,23 @@ internal class RecordListCsvEncoder(
super.encodeColumn(value, isNumeric, isNull)
writer.endRecord()
}

override fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {
if (config.deferToFormatWhenVariableColumns != null) {
when (serializer.descriptor.kind) {
is StructureKind.LIST,
is StructureKind.MAP,
is PolymorphicKind.OPEN -> {
encodeColumn(
value = config.deferToFormatWhenVariableColumns!!.encodeToString(serializer, value),
isNumeric = false,
isNull = false
)
return
}
else -> {}
}
}
serializer.serialize(this, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ class CsvHasHeaderRecordTest {
0.0,
1.0
), 100, "info"
),
"Albert"
)
),
NestedRecord.serializer()
)
Expand All @@ -144,8 +143,7 @@ class CsvHasHeaderRecordTest {
0.0,
1.0
), 100, "info"
),
"Albert"
)
),
NestedRecord(
1,
Expand All @@ -155,8 +153,7 @@ class CsvHasHeaderRecordTest {
10.0,
20.0
), 50, "info2"
),
"Bill"
)
)
),
ListSerializer(NestedRecord.serializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ internal class CsvIgnoreUnknownColumnsTest {
),
speed = 100,
info = "info"
),
alternative = "Albert"
)
),
NestedRecord(
time = 1,
Expand All @@ -83,8 +82,7 @@ internal class CsvIgnoreUnknownColumnsTest {
),
speed = 50,
info = "info2"
),
alternative = "Bill"
)
)
),
ListSerializer(NestedRecord.serializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class CsvNestedRecordTest {
0.0,
1.0
), 100, "info"
),
"Albert"
)
),
NestedRecord.serializer()
)
Expand All @@ -38,8 +37,7 @@ class CsvNestedRecordTest {
0.0,
1.0
), 100, "info"
),
"Albert"
)
),
NestedRecord(
1,
Expand All @@ -49,8 +47,7 @@ class CsvNestedRecordTest {
10.0,
20.0
), 50, "info2"
),
"Bob"
)
)
),
ListSerializer(NestedRecord.serializer())
Expand All @@ -69,8 +66,7 @@ class CsvNestedRecordTest {
0.0,
1.0
), 100, "info"
),
"Albert"
)
),
NestedRecord.serializer()
)
Expand All @@ -89,8 +85,7 @@ class CsvNestedRecordTest {
0.0,
1.0
), 100, "info"
),
"Albert"
)
),
NestedRecord(
1,
Expand All @@ -100,8 +95,7 @@ class CsvNestedRecordTest {
10.0,
20.0
), 50, "info2"
),
"Bill"
)
)
),
ListSerializer(NestedRecord.serializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ data class SerialNameRecord(
data class NestedRecord(
val time: Int,
val name: String,
val data: Data,
val alternative: String
val data: Data
)

@Serializable
Expand Down

0 comments on commit 643a157

Please sign in to comment.