Giter Site home page Giter Site logo

Comments (8)

doyaaaaaken avatar doyaaaaaken commented on July 19, 2024

Hi @jnfeinstein, thank for the question.
With the readNext() method, you can read one row at a time.
https://github.com/doyaaaaaken/kotlin-csv#read-line-by-line

I think this feature is enough to deal with your proposed use-cases, isn't it?

from kotlin-csv.

jnfeinstein avatar jnfeinstein commented on July 19, 2024

@doyaaaaaken thanks for the timely reply. I'll attempt to use that and report back.

from kotlin-csv.

jnfeinstein avatar jnfeinstein commented on July 19, 2024

This line prevents the reader from being used in an asynchronous context as it is closing the stream on return. I believe the intended usage would be similar to:

val reader = csvReader().open(inputStream)
val nextRow = reader.readNext()

This allows the caller to take advantage of the state machine within the CSV reader while giving them the freedom to play with the input stream as desired.

In regards to headers, having fun readHeader(): List<String> and fun readNextWithHeader(): Map<String, String> would be helpful.

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 19, 2024

As I wrote on the principle section on README, I'd like to hide file close process.
So, if you could, I'd like you to use open and readAllAsSequence methods.

But, if there is the use-case to open and close files manually, we need to implement that feature.
On CSV writer, that use-case exists. So I provide openAndGetRawWriter method to control file close process manually. The background is here. #59

What do you think whether the same structure is needed on CSV reader?

from kotlin-csv.

jnfeinstein avatar jnfeinstein commented on July 19, 2024

My use case is similar to #59 except from the read perspective.

on Java, we always need to close file. but it's boilerplate code and not friendly for non-JVM user.

In my opinion this makes sense for the methods that accept File or fileName: String as an input. I would expect that the library would handle opening and closing the file because anything else would be an unintended side-effect. However, an input of type InputStream means that the user has manually opened the stream. The parallel expectation would be that the user is also expected to close the stream. It actually took some time debugging to determine that kotlin-csv was doing this. The syntax would look like:

csvContent.toByteArrayOutputStream().use {
  csvReader().open(it) {
    doForEachRow(readNext())
  }
  
  doComplexOperationWithInputStream(it)
}

profit()

I've taken advantage of the same auto-close functionality used internally in kotlin-csv to reduce boilerplate, but now have the capability to perform a complex operation on this stream that may be unique to my use case.

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 19, 2024

Actually, I partly agree with your below point. Thanks for your insightful opinion.

In my opinion this makes sense for the methods that accept File or fileName: String as an input. I would expect that the library would handle opening and closing the file because anything else would be an unintended side-effect. However, an input of type InputStream means that the user has manually opened the stream. The parallel expectation would be that the user is also expected to close the stream.

But, your providing example seems not to be possible because we can read InputStream at most once.
In your example, you read InputStream twice.

On your use-case, you could write codes like a following snippet.

val ips: InputStream = "a1,b1\r\na2,b2".toByteArray().inputStream()
csvReader().open(ips) {
    var row: List<String>? = readNext()
    while(row != null) {
        doComplexOperationWithInputStream(row)
        row = readNext()
    }
}

from kotlin-csv.

jnfeinstein avatar jnfeinstein commented on July 19, 2024

InputStream is an abstract class, allowing the caller to create an implementation that behaves however they wish. Some of the standard implementations may even be reset. The overall point is that closing prematurely may be an undesirable side-effect to some calling applications. 😁

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 19, 2024

I can understand your opinion, but in my sense, to close file like below snippet are not unnatural.
The reason is written as code comment on below snippet.

val ips1: InputStream = "a1,b1\r\na2,b2".toByteArray().inputStream()
val csv1 = csvReader().readAll(ips1) // to close the InputStream, BECAUSE it was all read.

val ips2: InputStream = "a1,b1\r\na2,b2".toByteArray().inputStream()
val csv2 = csvReader().open(ips2) {
  readAllAsSequence().toList()
}  // to close the InputStream, BECAUSE it is enclosed by `open` method.

from kotlin-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.