sowmiyaksr
sowmiyaksr

Reputation: 169

How to create a case class from List

val list = List(A(None,None,Some("dummyText")),
           "DummmyText", None, None, None, None, None, None, Some("322"), 
          Some("1233"))

I need to convert this to a case class

case class Dummy(code: A, someValue1: String, someValue2: Option[B] = None,
                            someValue3: Option[B] = None, someValue4: Option[B] = None,
                            someValue5: Option[B] = None, someValue6: Option[A] = None,
                            someValue7: Option[List[A]] = None, someValue8: Option[String] = None, someValue9: Option[String] = None)

I tried this Instantiating a case class from a list of parameters

But not working, since my List has sub types.

Is is possible to convert a List like this can be converted to a case class?

Upvotes: 0

Views: 1013

Answers (1)

mavarazy
mavarazy

Reputation: 7735

You have an error in your list of params, if you follow carefully referenced Instantiating a case class from a list of parameters, it does work:

val params = List(
    A(None,None,Some("dummyText")),
    "DummmyText",
    None,
    None,
    None,
    None,
    None,
    None,
    Some("1233")
)

case class Dummy(code: A,
             someValue1: String,
             someValue2: Option[B] = None,
             someValue3: Option[B] = None,
             someValue4: Option[B] = None,
             someValue5: Option[B] = None,
             someValue6: Option[A] = None,
             someValue7: Option[List[A]] = None,
             someValue8: Option[String] = None
)


Dummy.
    getClass.
    getMethods.
    find(x => x.getName == "apply" && x.isBridge).
    get.
    invoke(Dummy, params map (_.asInstanceOf[AnyRef]): _*).
    asInstanceOf[Dummy]

Upvotes: 3

Related Questions