form-binder v0.12.2 发布,主要改动是,调整了 Mapping 附件/扩展信息 的使用方式。
现在,经过这样的准备后:[code] case class Attachment(
_in: Option[String] = None,
_desc: Option[String] = None
)
case class AttachmentBuilder[T](mapping: Mapping[T], attachment: Attachment) {
def in(in: String) = copy(mapping, attachment.copy(_in = Some(in)))
def desc(desc: String) = copy(mapping, attachment.copy(_desc = Some(desc)))
def $$ = mapping.options(_.copy(_attachment = Some(attachment)))
}
implicit class AttachmentImplicit[T](mapping: Mapping[T]) {
def $ = AttachmentBuilder(mapping, mapping.options._attachment.getOrElse(Attachment()).asInstanceOf[Attachment])
}[/code]可以这样用:[code] tmapping(
"id" -> long().$.in("path").$$.$.desc("pet id").$$,
"name" -> text().$.in("query").desc("pet name").$$
).fields.foreach {
case ("id", id) => id.options._attachment.orNull should be (Attachment(Some("path"), Some("pet id")))
case ("name", name) => name.options._attachment.orNull should be (Attachment(Some("query"), Some("pet name")))
}[/code]使用方式更优雅,同时 Mapping trait 中去掉了不再需要的 `$ext` 方法。完整代码看 这里。
另外,就是 Mapping trait 中 `mapTo` 重命名为 `map` (含义和 collection、option 的 map 一致,就不取另外的名字了)。
软件详情:https://github.com/tminglei/form-binder/releases
来自:开源中国社区

