Search code examples
scalacase-class

Creating some random case class dynamically using List[Strings] or Array[Strings]


Without using shapeless library, can we create a case class dynamically using some bunch of Strings? (Array of strings or list of strings)

I have a list of strings and I want to some how create a case class instance so that I can map them to some other table.

For example, let's say, these are the strings

DRUG_NAME, DRUG_TYPE, COMPANY, STATE, OFFICER

I want them to be in case class, like this:

case class DrugStore(DRUG_NAME: String, DRUG_TYPE: String, COMPANY: String, STATE: String, OFFICER: String)

Solution

  • You could use reflection for that:

      classOf[DrugStore]
        .getConstructors 
        .head
        .newInstance(listOfParameters)
        .asInstanceOf[DrugStore]