2017-04-17 48 views
0
val list = List(A(None,None,Some("dummyText")), 
      "DummmyText", None, None, None, None, None, None, Some("322"), 
      Some("1233")) 

的情况下,I类需要将其转换为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) 

我想这Instantiating a case class from a list of parameters

但不工作,因为我的List有子类型。

是可以转换List这样可以转换成case class

回答

2

您有一个错误在你则params的列表,如果你遵循谨慎引用Instantiating a case class from a list of parameters,它的工作:

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] 
+0

感谢@mavarazy。我编辑了参数。当我尝试这样做时,在“find(x => x.getName ==”)行处得到错误“play.api.http.HttpErrorHandlerExceptions $$ anon $ 1:Execution exception [[NoSuchElementException:None.get]]” “&& x.isBridge)。get。” – sowmiyaksr

+0

我想,还有一些其他问题。我会找到确切的原因。谢谢:-) – sowmiyaksr

+0

我得到了原因这是因为我的案例类超过22个领域。确切24场。有24种方法可以做到这一点吗? – sowmiyaksr