blackmo18 / kotlin-grass

Kotlin Csv to Data Class Parser
Apache License 2.0
38 stars 8 forks source link

Improve handling of missing target fields in CSV #28

Open stefanozanella opened 1 year ago

stefanozanella commented 1 year ago

I was recently parsing a CSV into a target class, where one of the fields of said class wasn't present in the CSV. This resulted in this exception being thrown:

java.lang.ClassCastException: Cannot cast kotlin.Unit to java.lang.String

Upon further analysis this seems to happen because when creating the array of parameters for the constructor of the target class in Root.kt an empty default value block is specified (the Unit in the exception).

I think this is a corner case with no clear-cut solution, at the same time I think the behaviour towards the user could be improved (it took me a debugging session to understand what was going on).

A couple of ideas:

For now I can solve the problem in my code by removing the offending fields, but I'd love to help alleviate this problem. Please let me know your thoughts, I'd be happy to PR a solution afterwards.

blackmo18 commented 1 year ago

Can you provide a sample csv file and data class you are using.

Version of kotlin grass and kotlin-csv?

Tho it should throw MissMatchedNumberOfFieldsException if csv length and number fields of data class.

stefanozanella commented 1 year ago

This gist reproduces the problem for me (sorry I might have forgotten that I'm setting ignoreUnknownFields).

Here's an excerpt from the Gradle configuration of the project with all relevant dependencies and versions:

  implementation("com.github.doyaaaaaken:kotlin-csv-jvm:0.15.2")
  implementation("io.github.blackmo18:kotlin-grass-core-jvm:1.0.0")
  implementation("io.github.blackmo18:kotlin-grass-parser-jvm:0.8.0")