1. XenForo 1.5.14 中文版——支持中文搜索!现已发布!查看详情
  2. Xenforo 爱好者讨论群:215909318 XenForo专区

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

Discussion in '软件资讯' started by 漂亮的石头, 2016-04-11.

  1. 漂亮的石头

    漂亮的石头 版主 Staff Member

    Joined:
    2012-02-10
    Messages:
    487,584
    Likes Received:
    47
    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 一致,就不取另外的名字了)。
    form-binder v0.12.2 发布,Scala 微型数据绑定和校验框架下载地址
     
Loading...