-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from brudaswen/feature/issue-9
Support `ignoreUnknownColumns`
- Loading branch information
Showing
11 changed files
with
230 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
library/src/main/kotlin/kotlinx/serialization/csv/CsvException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
@file:Suppress("FunctionName") | ||
|
||
package kotlinx.serialization.csv | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
|
||
/** | ||
* Generic exception indicating a problem with CSV serialization and deserialization. | ||
*/ | ||
internal open class CsvException(message: String) : SerializationException(message) | ||
|
||
/** | ||
* Thrown when [Csv] has failed to create a CSV string from the given value. | ||
*/ | ||
internal class CsvEncodingException(message: String) : CsvException(message) | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun UnsupportedSerialDescriptorException(descriptor: SerialDescriptor) = CsvEncodingException ( | ||
"CSV does not support '${descriptor.kind}'." | ||
) | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun HeadersNotSupportedForSerialDescriptorException(descriptor: SerialDescriptor) = CsvEncodingException ( | ||
"CSV headers are not supported for variable sized type '${descriptor.kind}'." | ||
) | ||
|
||
/** | ||
* Thrown when [Csv] has failed to parse the given CSV string or deserialize it to a target class. | ||
*/ | ||
internal class CsvDecodingException(message: String) : CsvException(message) | ||
|
||
internal fun CsvDecodingException(offset: Int?, message: String) = | ||
CsvDecodingException(if (offset != null) "Unexpected CSV token at offset $offset: $message" else message) | ||
|
||
internal fun UnknownColumnHeaderException(offset: Int, header: String) = CsvDecodingException( | ||
offset, | ||
""" | ||
|Encountered unknown column header '$header'. | ||
|Use 'ignoreUnknownColumns = true' in 'Csv {}' builder to ignore unknown columns. | ||
|""".trimMargin() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
library/src/test/kotlin/kotlinx/serialization/csv/config/CsvIgnoreUnknownKeysTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package kotlinx.serialization.csv.config | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.csv.Csv | ||
import kotlinx.serialization.csv.CsvConfiguration | ||
import kotlinx.serialization.csv.records.Data | ||
import kotlinx.serialization.csv.records.IntStringRecord | ||
import kotlinx.serialization.csv.records.Location | ||
import kotlinx.serialization.csv.records.NestedRecord | ||
import kotlinx.serialization.test.assertParse | ||
import kotlinx.serialization.test.assertParseFails | ||
import kotlin.test.Test | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal class CsvIgnoreUnknownKeysTest { | ||
|
||
@Test | ||
fun testMultipleColumns() = assertParse( | ||
"a,b,IGNORED\r\n1,testing,ignored", | ||
IntStringRecord(1, "testing"), | ||
IntStringRecord.serializer(), | ||
Csv( | ||
CsvConfiguration( | ||
hasHeaderRecord = true, | ||
ignoreUnknownColumns = true | ||
) | ||
) | ||
) | ||
|
||
@Test | ||
fun testMultipleColumns_failure() = assertParseFails( | ||
"a,b,IGNORED\r\n1,testing,ignored", | ||
IntStringRecord.serializer(), | ||
Csv( | ||
CsvConfiguration( | ||
hasHeaderRecord = true | ||
) | ||
) | ||
) | ||
|
||
@Test | ||
fun testMultipleColumnsReordered() = assertParse( | ||
"IGNORED,b,a\r\nignored,testing,1", | ||
IntStringRecord(1, "testing"), | ||
IntStringRecord.serializer(), | ||
Csv( | ||
CsvConfiguration( | ||
hasHeaderRecord = true, | ||
ignoreUnknownColumns = true | ||
) | ||
) | ||
) | ||
|
||
@Test | ||
fun testMultipleColumnsReordered_failure() = assertParseFails( | ||
"IGNORED,b,a\r\nignored,testing,1", | ||
IntStringRecord.serializer(), | ||
Csv( | ||
CsvConfiguration( | ||
hasHeaderRecord = true | ||
) | ||
) | ||
) | ||
|
||
@Test | ||
fun testNestedRecordListWithHeaderReordered() = assertParse( | ||
"""IGNORED,time,name,data.location.lon,data.location.IGNORED,data.location.lat,data.speed,data.info,IGNORED | ||
|IGNORED,0,Alice,1.0,IGNORED,0.0,100,info,IGNORED | ||
|IGNORED,1,Bob,20.0,IGNORED,10.0,50,info2,IGNORED | ||
|""".trimMargin().replace("\n", "\r\n"), | ||
listOf( | ||
NestedRecord( | ||
time = 0, | ||
name = "Alice", | ||
data = Data( | ||
location = Location( | ||
lat = 0.0, | ||
lon = 1.0 | ||
), | ||
speed = 100, | ||
info = "info" | ||
) | ||
), | ||
NestedRecord( | ||
time = 1, | ||
name = "Bob", | ||
data = Data( | ||
location = Location( | ||
lat = 10.0, | ||
lon = 20.0 | ||
), | ||
speed = 50, | ||
info = "info2" | ||
) | ||
) | ||
), | ||
ListSerializer(NestedRecord.serializer()), | ||
Csv( | ||
CsvConfiguration( | ||
hasHeaderRecord = true, | ||
ignoreUnknownColumns = true | ||
) | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters