斯卡拉MongoDB:可选字段和不可变字段
我有这个域对象:
case class Person (
name : String,
age: Option[Int],
email : String
) extends MongoObject
带有年龄可选字段。所以我定义了我的工厂:
object Person extends MongoObjectShape[Person] {
lazy val name = Field.scalar("name", _.name)
lazy val age = Field.optional("age", t => t.age)
lazy val email = Field.scalar("email", _.email)
override lazy val * = name :: age :: email :: Nil
override def factory(dbo: DBObject): Option[Person] =
for {
n <- name from dbo
t <- age from dbo
z <- email from dbo
} yield new Person(n, t, z)
}
但它无法编译,因为我得到:
[error] found : Int
[error] required: Option[Int]
[error] } yield new Person(n, t, z)
[error] ^
这有什么问题?
I have this domain object:
case class Person (
name : String,
age: Option[Int],
email : String
) extends MongoObject
with the age optional field. So I defined my factory:
object Person extends MongoObjectShape[Person] {
lazy val name = Field.scalar("name", _.name)
lazy val age = Field.optional("age", t => t.age)
lazy val email = Field.scalar("email", _.email)
override lazy val * = name :: age :: email :: Nil
override def factory(dbo: DBObject): Option[Person] =
for {
n <- name from dbo
t <- age from dbo
z <- email from dbo
} yield new Person(n, t, z)
}
but it does not compile, since I get:
[error] found : Int
[error] required: Option[Int]
[error] } yield new Person(n, t, z)
[error] ^
What is wrong with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许,你可以写
maybe, you could write
amsayk 答案的一个变体,但稍微更对称
A variant of amsayk's answer, but slightly more symmetrical
您需要将 t 包装在 Some 对象中
You need to wrap t in a Some object