在 Lift 的记录框架中创建专门字段
Scala 的优点之一是类型安全,因此应用程序中不会出现未定义的值。然而,Mongo 根本不是类型安全的。所以,我认为,一种与 Mongo 之间的转换是好的,以确保只保存正确的值(作为字符串)。我的 Scala 中有这种类型:
sealed trait Tribe
object Tribe {
def fromString(s:String) = s match {
case "Earth Pony" => EarthPony
case "Pegasus" => Pegasus
case "Unicorn" => Unicorn
case "Alicorn" => Alicorn
case _ => throw new NoSuchElementException
}
}
case object EarthPony extends Tribe {
override def toString = "Earth Pony"
}
case object Pegasus extends Tribe {
override def toString = "Pegasus"
}
case object Unicorn extends Tribe {
override def toString = "Unicorn"
}
case object Alicorn extends Tribe {
override def toString = "Alicorn"
}
现在我想创建一个字段 TribeField
,我可以在 MongoRecord
类中使用它,以确保在读取记录时完成此转换,或保存它。
不幸的是,Lift's Record 上的文档似乎很少,到目前为止我还没有找到任何关于如何执行此操作的有用信息。也许这里有人可以给我一些提示?
One of the nice advantages of Scala is the way you can be type safe, so that no undefined values will appear in the application. Mongo, however, is not type safe at all. So, I thought, a kind of conversion to and from Mongo is good, to make sure only the right values are saved (as strings). I have this type in my Scala:
sealed trait Tribe
object Tribe {
def fromString(s:String) = s match {
case "Earth Pony" => EarthPony
case "Pegasus" => Pegasus
case "Unicorn" => Unicorn
case "Alicorn" => Alicorn
case _ => throw new NoSuchElementException
}
}
case object EarthPony extends Tribe {
override def toString = "Earth Pony"
}
case object Pegasus extends Tribe {
override def toString = "Pegasus"
}
case object Unicorn extends Tribe {
override def toString = "Unicorn"
}
case object Alicorn extends Tribe {
override def toString = "Alicorn"
}
Now I want to make a field TribeField
which I can employ in a MongoRecord
class to make sure this conversion is done when I read the Record, or save it.
Unfortunately, the documentation on Lift's Record seems sparse, and so far I have not found any helpful information on how to do this. Maybe someone here can give me some hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相当确定 lift-record-mongodb 使用 lift-record Field 实例的功能通过 Field.asJValue 和 Field.setFromJValue 与 JSON 进行序列化/反序列化。要创建完全类型安全的 Tribe 字段,您需要创建自己的 TypedField[Tribe] 并实现这些方法以及设置和访问字段的其他抽象方法。我建议查看 StringField 或其他具体字段类型之一,以获取有关如何执行此操作的指针。
一个更简单的替代方案是扩展 StringField 本身并添加 setTribe/asTribe 方法。
如果您需要更多信息,特别是有关 Lift 的 Mongodb 集成的信息,我建议您尝试 Lift Google Group。维护该代码的蒂姆·尼尔森通常会很快回答问题。
I'm fairly sure that lift-record-mongodb uses the ability of lift-record Field instances to serialize/deserialize to and from JSON via Field.asJValue and Field.setFromJValue. To make a completely type safe Tribe Field, you'd want to create your own TypedField[Tribe] and implement those methods along with the other abstract methods that set and access your field. I'd recommend taking a look at StringField or one of the other concrete Field types for pointers on how to do that.
An easier alternative would be to extend StringField itself and add setTribe/asTribe methods.
If you need more info, particularly on Lift's Mongodb integration, I'd recommend you try the Lift Google Group. Tim Nelson who maintains that code is usually pretty quick to respond to questions.