0
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?

Community
  • 1
  • 1
sowmiyaksr
  • 169
  • 5
  • 18

1 Answers1

3

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]
Community
  • 1
  • 1
mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • Thanks @mavarazy. I edited the params. When I tried this, got the Error " play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NoSuchElementException: None.get]]" at the line of "find(x => x.getName == "apply" && x.isBridge). get." – sowmiyaksr Apr 17 '17 at 08:10
  • I think, there is some other issue. I will find the exact reason. Thanks :-) – sowmiyaksr Apr 17 '17 at 08:25
  • I got the reason It is because of my case class as more than 22 fields. Exactly 24 fields. Is there a way to do this with 24 fields? – sowmiyaksr Apr 17 '17 at 10:00
  • https://stackoverflow.com/questions/4290955/instantiating-a-case-class-from-a-list-of-parameters answered time ago – LowFieldTheory Sep 24 '17 at 18:20