Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Typeful reader API #45

Open
1 of 8 tasks
LVMVRQUXL opened this issue Nov 16, 2022 · 2 comments
Open
1 of 8 tasks

Typeful reader API #45

LVMVRQUXL opened this issue Nov 16, 2022 · 2 comments
Assignees
Labels
csv Related to Kotools CSV. enhancement New feature or request. jvm Should work on JVM platform.
Milestone

Comments

@LVMVRQUXL
Copy link
Contributor

LVMVRQUXL commented Nov 16, 2022

Description

Based on #14, introduce a new reader API in the kotools.csv package for reading CSV files and deprecate the old one in the io.github.kotools.csv package.

Declarations in kotools.csv should use explicit types from Kotools Types for having a typeful design.

Dependencies

This issue needs the following ones to be done:

Checklist

  • Implement and test a basic reader API.
  • Improve the reader for filtering returned records.
  • Improve the reader for paginating returned records.
  • Improve the reader for returning a custom type.
  • Deprecate the old reader.
  • Refactor.
  • Update Work in progress section in changelog.
@LVMVRQUXL LVMVRQUXL added enhancement New feature or request. jvm Should work on JVM platform. csv Related to Kotools CSV. labels Nov 16, 2022
@LVMVRQUXL LVMVRQUXL added this to the CSV v2.3.0 milestone Nov 16, 2022
@LVMVRQUXL LVMVRQUXL assigned LVMVRQUXL and unassigned LVMVRQUXL Nov 16, 2022
@LVMVRQUXL
Copy link
Contributor Author

Here's the code for retrieving the file from the "resources" or the "${user.home}/.kotools/csv" folders:

data class CsvFile(val file: File)

/**
 * Returns the [CSV file][CsvFile] matching the given [path], or returns `null`
 * if no file was found from the `"resources"` and the
 * `"${user.home}/.kotools/csv"` folders.
 *
 * The `.csv` extension is **optional** in the [path].
 * For example, calling this function with `"folder/file.csv"` produces the same
 * output as calling it with `"folder/file"`.
 */
fun csvFileOrNull(path: NotBlankString): CsvFile? {
    val filePath: NotBlankString = path.takeIf { it.value.endsWith(".csv") }
        ?: NotBlankString("$path.csv")
    val file: File? = ClassLoader.getSystemResource(filePath.value)
        ?.let { File(it.path) }
        ?: System.getProperty("user.home")
            .let(::Path)
            .resolve(".kotools")
            .resolve("csv")
            .resolve(filePath.value)
            .takeIf { it.exists() }
            ?.toFile()
    return file?.let(::CsvFile)
}

@LVMVRQUXL LVMVRQUXL self-assigned this Nov 18, 2022
LVMVRQUXL added a commit that referenced this issue Nov 21, 2022
Declarations of this function and its dependencies are internal for now.
@LVMVRQUXL
Copy link
Contributor Author

For converting a record to a custom type, we could use the serialization pattern (see kotlinx.serialization library).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
csv Related to Kotools CSV. enhancement New feature or request. jvm Should work on JVM platform.
Projects
None yet
Development

No branches or pull requests

1 participant