Kotlin reflection - creating objects from CSV

Viewed 3471

I have a data class named Member:

data class Member(){
    val first_name: String
    val last_name: String
    //30 more

    //a few simple methods
}

I am trying to import a CSV file. Each line of the file contains the fields I want to use to instantiate my Members.

fun ReadCsvFileKotlin() {
    val csvFile = "C:\\Data.csv"
    var memberList = mutableListOf<Member>()
    var reader = File(csvFile).readLines()
    var mbr: Member
    class Member(val p: Int)
    val prop = Member::p
    for(line in reader){
        val mbrProperties = line.split(",")
        for(i in 0..mbrProperties.lastIndex){
            //assign mbrProperties[i] to mbr property
            //mbrProperties[0] = "Bob"
            //mbr.first_name = "Bob"
            //Member::p = mbrProperties[i]
    }
    memberList.add(mbr)
}

I've done my research, but I'm failing to wrap my head around the information I've read and just can't bridge the gap between where I am and where I want to be.

Any advice would be appreciated! Walking me through or giving me examples using my example code would be welcome.

Background: I am automating tests for a website. Prior to executing each test, the member meeting the necessary test criteria is selected from a list, populated from the CSV file. I am looking to create my member objects in this fashion to reduce maintenance overhead as the test scope expands and additional criteria (and Member class fields) are added.

Why am I doing any of this in the manner I am doing? Because I still have much to learn... (So, if you see a better way, please, educate me!)

Thanks!

3 Answers

While I did play around with yole's response, and reflection even further, I found a better way to handle me issue.

As a less experienced programmer, I was trying to use the wrong tool for the job. I wanted to take an object, that had a number of functions that i would need for a number of different automated tests, simulating a user - Code I wanted in every instance, whether I needed it for a test or not.

But what I wanted was the flexibility in my data. So instead of a single object with a growing amount of attributes, or multiple objects, each custom to a specific data set, I went to one attribute. The best solution, was a key/value pair attribute. I apologize if my Kotlin speak is rusty, I've been focused on python the last two+ years. Dictionary?

Essentially, I stripped off the header of my csv, mapped it to each row as a dictionary/key,value pair which was then added as I instantiated an object and added the object to a list. Similar if querying a database directly rather than working with csv or Excel files.

So as long as I don't write queries that change the column names of my tables for my results, and I know the column names in the database (They are not changing anytime soon), and I'm already mapping that data to the process and/or gui (or both) things turned quite nicely without the reflection overhead.

If you still look to do that, I created my own CSV library that uses reflection to instantiate POJOs directly from the CSV input and manages errors in the CSV much better than regular parsers I tested, called Kotlin CSV Stream

I actually created it after giving up on traditional parsers because they all did not fill my needs.

Related