Skip to content

Commit

Permalink
Merge pull request #11 from brudaswen/cleanup/naming
Browse files Browse the repository at this point in the history
Cleanup/naming
  • Loading branch information
brudaswen authored Nov 8, 2020
2 parents 5505bf0 + ecd971a commit 878396f
Show file tree
Hide file tree
Showing 27 changed files with 733 additions and 907 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ internal abstract class CsvDecoder(

protected open fun decodeColumn() = reader.readColumn()

protected fun readHeaders(desc: SerialDescriptor) {
protected fun readHeaders(descriptor: SerialDescriptor) {
if (configuration.hasHeaderRecord && headers == null) {
this.headers = readHeaders(desc, "")
this.headers = readHeaders(descriptor, "")

readTrailingDelimiter()
}
}

private fun readHeaders(desc: SerialDescriptor, prefix: String): Headers {
private fun readHeaders(descriptor: SerialDescriptor, prefix: String): Headers {
val headers = Headers()
var position = 0
while (!reader.isDone && reader.isFirstRecord) {
Expand All @@ -138,15 +138,15 @@ internal abstract class CsvDecoder(

// If there is an exact name match, store the header, otherwise try reading class structure
val header = value.substringAfter(prefix)
val headerIndex = desc.getElementIndex(header)
val headerIndex = descriptor.getElementIndex(header)
if (headerIndex != CompositeDecoder.UNKNOWN_NAME) {
headers[position] = headerIndex
reader.unmark()
} else {
val name = header.substringBefore(configuration.headerSeparator)
val nameIndex = desc.getElementIndex(name)
val nameIndex = descriptor.getElementIndex(name)
if (nameIndex != CompositeDecoder.UNKNOWN_NAME) {
val childDesc = desc.getElementDescriptor(nameIndex)
val childDesc = descriptor.getElementDescriptor(nameIndex)
if (childDesc.kind is StructureKind.CLASS) {
reader.reset()
headers[position] = nameIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ internal class SealedCsvDecoder(
return value
}

private fun readEmptyColumns(desc: SerialDescriptor) {
for (innerDesc in desc.elementDescriptors.toList()) {
private fun readEmptyColumns(descriptor: SerialDescriptor) {
for (innerDesc in descriptor.elementDescriptors.toList()) {
decodeNull()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal abstract class CsvEncoder(
parent?.endChildStructure(descriptor)
}

protected open fun endChildStructure(desc: SerialDescriptor) {
protected open fun endChildStructure(descriptor: SerialDescriptor) {
}

override fun encodeByte(value: Byte) {
Expand Down Expand Up @@ -109,24 +109,24 @@ internal abstract class CsvEncoder(
encodeColumn(enumDescriptor.getElementName(index))
}

protected fun printHeaderRecord(desc: SerialDescriptor) {
protected fun printHeaderRecord(descriptor: SerialDescriptor) {
writer.beginRecord()
printHeader("", desc)
printHeader("", descriptor)
writer.endRecord()
}

private fun printHeader(prefix: String, desc: SerialDescriptor) {
when (desc.kind) {
private fun printHeader(prefix: String, descriptor: SerialDescriptor) {
when (descriptor.kind) {
is StructureKind.LIST,
is StructureKind.MAP,
is PolymorphicKind.OPEN -> {
error("CSV headers are not supported for variable sized type '${desc.kind}'.")
error("CSV headers are not supported for variable sized type '${descriptor.kind}'.")
}
}

for (i in 0 until desc.elementsCount) {
val name = prefix + desc.getElementName(i)
val childDesc = desc.getElementDescriptor(i)
for (i in 0 until descriptor.elementsCount) {
val name = prefix + descriptor.getElementName(i)
val childDesc = descriptor.getElementDescriptor(i)

when {
// TODO Check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class RecordListCsvEncoder(
return super.beginStructure(descriptor)
}

override fun endChildStructure(desc: SerialDescriptor) {
override fun endChildStructure(descriptor: SerialDescriptor) {
// For complex records: End the record here
writer.endRecord()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal class RootCsvEncoder(
return super.beginStructure(descriptor)
}

override fun endChildStructure(desc: SerialDescriptor) {
override fun endChildStructure(descriptor: SerialDescriptor) {
writer.endRecord()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ internal class SealedCsvEncoder(
}
}

override fun endChildStructure(desc: SerialDescriptor) {
override fun endChildStructure(descriptor: SerialDescriptor) {
val sealedChildren = sealedDesc.getElementDescriptor(1).elementDescriptors.toList()
val index = sealedChildren.indexOf(desc)
val index = sealedChildren.indexOf(descriptor)
for (innerDesc in sealedChildren.subList(index + 1, sealedChildren.size)) {
printEmptyColumns(innerDesc)
}
}

private fun printEmptyColumns(desc: SerialDescriptor) {
for (innerDesc in desc.elementDescriptors.toList()) {
private fun printEmptyColumns(descriptor: SerialDescriptor) {
for (innerDesc in descriptor.elementDescriptors.toList()) {
encodeNull()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.serialization.csv.Csv
import kotlinx.serialization.csv.CsvConfiguration
import kotlinx.serialization.csv.records.ComplexRecord
import kotlinx.serialization.csv.records.Enum
import kotlinx.serialization.test.assertStringFormAndRestored
import kotlinx.serialization.test.assertEncodeAndDecode
import kotlin.test.Test

/**
Expand All @@ -15,7 +15,7 @@ import kotlin.test.Test
class CsvDelimiterTest {

@Test
fun testDefault() = assertStringFormAndRestored(
fun testDefault() = Csv.assertEncodeAndDecode(
"-150,-1,42,9223372036854775807,-2.0,24.24,true,testing,,kotlin.Unit,FIRST",
ComplexRecord(
-150,
Expand All @@ -30,12 +30,15 @@ class CsvDelimiterTest {
Unit,
Enum.FIRST
),
ComplexRecord.serializer(),
Csv
ComplexRecord.serializer()
)

@Test
fun testComma() = assertStringFormAndRestored(
fun testComma() = Csv(
CsvConfiguration(
delimiter = ','
)
).assertEncodeAndDecode(
"-150,-1,42,9223372036854775807,-2.0,24.24,true,testing,,kotlin.Unit,FIRST",
ComplexRecord(
-150,
Expand All @@ -50,16 +53,15 @@ class CsvDelimiterTest {
Unit,
Enum.FIRST
),
ComplexRecord.serializer(),
Csv(
CsvConfiguration(
delimiter = ','
)
)
ComplexRecord.serializer()
)

@Test
fun testColon() = assertStringFormAndRestored(
fun testColon() = Csv(
CsvConfiguration(
delimiter = ';'
)
).assertEncodeAndDecode(
"-150;-1;42;9223372036854775807;-2.0;24.24;true;testing;;kotlin.Unit;FIRST",
ComplexRecord(
-150,
Expand All @@ -74,16 +76,15 @@ class CsvDelimiterTest {
Unit,
Enum.FIRST
),
ComplexRecord.serializer(),
Csv(
CsvConfiguration(
delimiter = ';'
)
)
ComplexRecord.serializer()
)

@Test
fun testDot() = assertStringFormAndRestored(
fun testDot() = Csv(
CsvConfiguration(
delimiter = '.'
)
).assertEncodeAndDecode(
"-150.-1.42.9223372036854775807.\"-2.0\".\"24.24\".true.testing..\"kotlin.Unit\".FIRST",
ComplexRecord(
-150,
Expand All @@ -98,16 +99,15 @@ class CsvDelimiterTest {
Unit,
Enum.FIRST
),
ComplexRecord.serializer(),
Csv(
CsvConfiguration(
delimiter = '.'
)
)
ComplexRecord.serializer()
)

@Test
fun testTab() = assertStringFormAndRestored(
fun testTab() = Csv(
CsvConfiguration(
delimiter = '\t'
)
).assertEncodeAndDecode(
"-150\t-1\t42\t9223372036854775807\t-2.0\t24.24\ttrue\ttesting\t\tkotlin.Unit\tFIRST",
ComplexRecord(
-150,
Expand All @@ -122,11 +122,6 @@ class CsvDelimiterTest {
Unit,
Enum.FIRST
),
ComplexRecord.serializer(),
Csv(
CsvConfiguration(
delimiter = '\t'
)
)
ComplexRecord.serializer()
)
}
Loading

0 comments on commit 878396f

Please sign in to comment.