发布于 2016-04-10 23:58:42 | 101 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

form-binder 微型数据绑定和校验框架

form-binder 是一个容易使用和定制的微型数据绑定和校验框架。


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 微型数据绑定和校验框架
form-binder v0.12.0 发布
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务