Quantcast
Channel: OSCHINA 社区最新新闻
Viewing all articles
Browse latest Browse all 44787

form-binder v0.12.2 发布,Scala 微型数据绑定和校验框架

$
0
0

form-binder v0.12.2 发布,主要改动是,调整了 Mapping 附件/扩展信息 的使用方式。

现在,经过这样的准备后:

  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])
  }

可以这样用:

  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")))
  }

使用方式更优雅,同时 Mapping trait 中去掉了不再需要的 `$ext` 方法。完整代码看 这里

另外,就是 Mapping trait 中 `mapTo` 重命名为 `map` (含义和 collection、option 的 map 一致,就不取另外的名字了)。


Viewing all articles
Browse latest Browse all 44787

Trending Articles