【问题标题】:how to reuse an anorm parser in playframework 2.0 with scala如何使用 scala 在 playframework 2.0 中重用异常解析器
【发布时间】:2012-10-02 01:48:14
【问题描述】:

我一直在查看computer-database sample,我注意到为了重用 Computer 解析器,list 方法使用了 Computer.withCompany 解析器,它返回 (Computer, Company) 的元组

在我必须处理的情况下,我想要一个 Computer 对象,而不是对计算机 id 的引用,就像这样

case class Computer(id: Pk[Long] = NotAssigned, name: String, 引入: Option[Date], 停产: Option[Date], company: Company)

所以我在想我怎样才能实现以下目标(当然是伪代码)

val simple = {
  get[Pk[Long]]("computer.id") ~
  get[String]("computer.name") ~
  get[Option[Date]]("computer.introduced") ~
  get[Option[Date]]("computer.discontinued") ~
  get[Company]("company.*") map {
    case id~name~introduced~discontinued~company => Computer(id, name, introduced, discontinued, company)
  }
}

显然,棘手的部分是如何解决 getCompany

有什么想法???

【问题讨论】:

    标签: parsing playframework-2.0 anorm


    【解决方案1】:

    我有一个 Idea 实体和一个 IdeaType 实体(类似于计算机和公司,在计算机数据库示例中)

    case class IdeaTest(
      val id: Pk[Long]          = NotAssigned,
      val name: String          = "unknown idea",
      val description: String   = "no description",
      val kind: IdeaType        = IdeaType()
    )
    
    case class IdeaType (
      val id: Pk[Long] = NotAssigned,
      val name: String = "unknown idea type",
      val description: String = "no description"
    )
    

    我定义了一个 TypeParser

    val typeParser: RowParser[IdeaType] = {
      get[Pk[Long]]("idea_type.id") ~
      get[String]("idea_type.name") ~
      get[String]("idea_type.description") map {
        case id~name~description => IdeaType(
          id, name, description
        )
      }
    }
    

    我尝试的第一件事是:

    val ideaParser: RowParser[IdeaTest] = {
      get[Pk[Long]]("idea.id") ~
      get[String]("idea.name") ~
      get[String]("idea.description") ~
      typeParser map {
        case id~name~description~ideaType => IdeaTest(
          id, name, description, ideaType
        )
      }
    }
    

    即使编译正常,加载ideaType总是有问题。

    最后,我不得不定义一个没有ideaType的ideaParser,并用typeParser组合它:

    val typeParser: RowParser[IdeaType] = {
      get[Pk[Long]]("idea_type.id") ~
      get[String]("idea_type.name") ~
      get[String]("idea_type.description") map {
        case id~name~description => IdeaType(
          id, name, description
        )
      }
    }
    
    val ideaWithTypeParser = ideaParser ~ typeParser map {
      case idea~kind => (idea.copy(kind=kind))
    }
    

    这是使用它的代码:

    def ideaById(id: Long): Option[IdeaTest] = {
      DB.withConnection { implicit connection =>
        SQL("""
          select * from
          idea inner join idea_type 
          on idea.idea_type_id = idea_type.id 
          where idea.id = {id}""").
          on('id -> id).
          as(ideaParser.singleOpt)
      }
    }
    

    我看到的唯一麻烦是,我必须在不一致的状态下创建 IdeaTest 对象(没有ideaType),然后将其复制到另一个具有正确 IdeaType 的实例。

    【讨论】:

    • 这个答案非常有用,有助于巩固我对编写解析器的理解,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2015-05-30
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多