播放框架表单映射递归项

时间:2014-05-05 13:37:47

标签: scala recursion playframework playframework-2.4 playframework-2.5

我有以下案例类,其中包含表单映射的伴随对象:

case class ContractItem(description: String, price: Option[BigDecimal], durationMonths: Option[Int], subItems: List[ContractItem])
object ContractItem {
  val mapping: Mapping[ContractItem] = Forms.mapping(
    "description" -> text,
    "price" -> optional(bigDecimal),
    "durationMonths" -> optional(number),
    "subItems" -> Forms.list(ContractItem.mapping)
  )(ContractItem.apply)(ContractItem.unapply)
}

正如您所看到的,case类是递归的,因为它包含相同类型的列表,并且我试图在映射中包含它。这一切都编译好,但是当我尝试运行这个项目时,我得到以下内容:

java.lang.NullPointerException
at play.api.data.RepeatedMapping.<init>(Form.scala:636)
at play.api.data.Forms$.list(Forms.scala:389)
at models.Installation$ContractItem$.<init>(Installation.scala:17)
at models.Installation$ContractItem$.<clinit>(Installation.scala)
at models.Installation$Contract$.<init>(Installation.scala:23)
at models.Installation$Contract$.<clinit>(Installation.scala)
at models.Installation$.<init>(Installation.scala:31)
at models.Installation$.<clinit>(Installation.scala)
at Temp.<init>(Temp.scala:82)
at .<init>(<console>:7)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

有没有想过这样的事情是否可能?应该是我会想到的!或者任何解决方法的想法?

谢谢! NFV

更新

使用

lazy val mapping 

结果如下:

java.lang.StackOverflowError
at play.api.data.Mapping$class.$init$(Form.scala:416)
at play.api.data.OptionalMapping.<init>(Form.scala:704)
at play.api.data.Forms$.optional(Forms.scala:363)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:15)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)

1 个答案:

答案 0 :(得分:2)

变化:

val mapping: Mapping[ContractItem]

为:

lazy val mapping: Mapping[ContractItem]

通过使mapping懒惰,它可以在不投掷NPE的情况下引用自身。