使用 ReactiveMongo 更容易在 Play Framework 中创建 Form 对象
Making it easier to create a Form object in Play Framework with ReactiveMongo
编辑: 对于那些想知道我打算如何根据接受的答案解决它的人,请参阅嵌套值 here。
我正在使用带有 Scala 和 Reactive 的 Play Framework Mongo。
目前我正在创建我的案例 类 并且表格如下:
case class Person(
_id : Option[BSONObjectID],
name: string,
city: string)
object Person {
val form: Form[Person] = Form {
mapping(
"_id" -> optional(of[String] verifying pattern(
"""[a-fA-F0-9]{24}""".r,
"constraint.objectId",
"error.objectId")),
"name"-> text,
"city"-> text)
{ (id,name, city) => Person(id.map(new BSONObjectID(_)), name, city) }
{ person =>Some(person._id.map(_.stringify), person.name, person.city) }
}
}
如果我在 _id
属性 中使用简单类型,例如 String
,我可以做一些更简单的事情,例如:
object Person {
val form: Form[Person] = Form {
mapping(
"_id" -> text,
"name"-> text,
"city"-> text
)(Person.apply)(Person.unapply)
}
}
所以我想我可以创建自己的 apply 方法来更改第一个参数,使用柯里化。我会这样定义:
def apply2(id: Option[String]) = {
val bsonid = id.map(new BSONObjectID(_))
(Person.apply _).curried(bsonid)
}
我的理论是,哪个实现不起作用,我会将 BSONObjectID
参数部分应用于 Person.apply
函数,该值将来自 apply2
参数调用id
。没用。
我是一个懒惰的人,不想输入一堆东西只是因为现在我有一个默认情况下不支持的新情况......柯里化是我的赌注之一,但任何可以更轻松地创建表单的解决方案是可以接受的。
我只需要一种方法来识别对象,这样我就可以删除或更新它,但目前的方法有点无聊,我认为 [=41= 创建的 _id
字段]数据库是完美的。
有没有办法让事情变得更简单,或者我只需要停止偷懒?
您可以通过创建 2 个案例 类 让事情变得更简单。
数据输入到位时使用的一个
case class PersonData(name: String, city: String)
还有另一个代表你模型中的真人
case class Person(_id: BSONObjectID, name: String, city: String)
并在 Person 对象中创建方法:
def fromData(data: PersonData) = Person(
id = new BSONObjectID(),
name = data.name,
city = data.city)
那么你的映射 From[PersonData] 可以更简单,并且你可以避免 Option[BSONObjectID] 在你的模型周围飞来飞去。
编辑: 对于那些想知道我打算如何根据接受的答案解决它的人,请参阅嵌套值 here。
我正在使用带有 Scala 和 Reactive 的 Play Framework Mongo。
目前我正在创建我的案例 类 并且表格如下:
case class Person(
_id : Option[BSONObjectID],
name: string,
city: string)
object Person {
val form: Form[Person] = Form {
mapping(
"_id" -> optional(of[String] verifying pattern(
"""[a-fA-F0-9]{24}""".r,
"constraint.objectId",
"error.objectId")),
"name"-> text,
"city"-> text)
{ (id,name, city) => Person(id.map(new BSONObjectID(_)), name, city) }
{ person =>Some(person._id.map(_.stringify), person.name, person.city) }
}
}
如果我在 _id
属性 中使用简单类型,例如 String
,我可以做一些更简单的事情,例如:
object Person {
val form: Form[Person] = Form {
mapping(
"_id" -> text,
"name"-> text,
"city"-> text
)(Person.apply)(Person.unapply)
}
}
所以我想我可以创建自己的 apply 方法来更改第一个参数,使用柯里化。我会这样定义:
def apply2(id: Option[String]) = {
val bsonid = id.map(new BSONObjectID(_))
(Person.apply _).curried(bsonid)
}
我的理论是,哪个实现不起作用,我会将 BSONObjectID
参数部分应用于 Person.apply
函数,该值将来自 apply2
参数调用id
。没用。
我是一个懒惰的人,不想输入一堆东西只是因为现在我有一个默认情况下不支持的新情况......柯里化是我的赌注之一,但任何可以更轻松地创建表单的解决方案是可以接受的。
我只需要一种方法来识别对象,这样我就可以删除或更新它,但目前的方法有点无聊,我认为 [=41= 创建的 _id
字段]数据库是完美的。
有没有办法让事情变得更简单,或者我只需要停止偷懒?
您可以通过创建 2 个案例 类 让事情变得更简单。 数据输入到位时使用的一个
case class PersonData(name: String, city: String)
还有另一个代表你模型中的真人
case class Person(_id: BSONObjectID, name: String, city: String)
并在 Person 对象中创建方法:
def fromData(data: PersonData) = Person(
id = new BSONObjectID(),
name = data.name,
city = data.city)
那么你的映射 From[PersonData] 可以更简单,并且你可以避免 Option[BSONObjectID] 在你的模型周围飞来飞去。