lundi 22 mai 2017

Kotlin reflection - creating objects from CSV

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!





Aucun commentaire:

Enregistrer un commentaire