1

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)
Suma
  • 33,181
  • 16
  • 123
  • 191

1 Answers1

3

You could use reflection for that:

  classOf[DrugStore]
    .getConstructors 
    .head
    .newInstance(listOfParameters)
    .asInstanceOf[DrugStore]
Dima
  • 39,570
  • 6
  • 44
  • 70