This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(csv-#45): update internal implementation for reading a CSV file
- Loading branch information
Showing
3 changed files
with
80 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package kotools.csv | ||
|
||
import kotools.types.collections.NotEmptyMap | ||
import kotools.types.collections.toNotEmptyMapOrThrow | ||
import kotools.types.string.NotBlankString | ||
import kotools.types.string.toNotBlankStringOrNull | ||
import kotools.types.string.toNotBlankStringOrThrow | ||
|
||
private fun NotEmptyMap<NotBlankString, NotBlankString?>.toRecord(): Record = | ||
Record(this) | ||
|
||
private fun Map<String, String>.toRecordOrNull(): Record? = | ||
takeIf(Map<String, String>::hasNotBlankKeys) | ||
?.mapKeys { it.key.toNotBlankStringOrThrow() } | ||
?.mapValues { it.value.toNotBlankStringOrNull() } | ||
?.toNotEmptyMapOrThrow() | ||
?.toRecord() | ||
|
||
internal inline fun List<Map<String, String>>.toRecordsOrElse( | ||
action: (Map<String, String>) -> Record | ||
): List<Record> = map { it.toRecordOrNull() ?: action(it) } | ||
|
||
private fun Map<String, String>.hasNotBlankKeys(): Boolean = | ||
all { it.key.isNotBlank() } | ||
|
||
@JvmInline | ||
internal value class Record( | ||
val fields: NotEmptyMap<NotBlankString, NotBlankString?> | ||
) |
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