• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Macros类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中reactivemongo.bson.Macros的典型用法代码示例。如果您正苦于以下问题:Scala Macros类的具体用法?Scala Macros怎么用?Scala Macros使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Macros类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: write

//设置package包名称以及导入依赖的类
package com.realizationtime.btdogg.persist

import java.time.{Instant, LocalDate}

import com.realizationtime.btdogg.TKey
import com.realizationtime.btdogg.parsing.ParsingResult.{FileEntry, TorrentDir, TorrentFile}
import com.realizationtime.btdogg.persist.MongoPersist.{Liveness, TorrentDocument}
import com.realizationtime.btdogg.persist.MongoTorrentWriter.localDateToString
import reactivemongo.bson.{BSONDateTime, BSONDocument, BSONDocumentWriter, BSONInteger, BSONString, BSONWriter, Macros}

trait MongoTorrentWriter {

  implicit val tkeyWriter = new BSONWriter[TKey, BSONString] {
    override def write(k: TKey): BSONString = BSONString(k.hash)
  }
  implicit val torrentDataFileWriter: BSONDocumentWriter[TorrentFile] = Macros.writer[TorrentFile]
  implicit val torrentDataDirWriter: BSONDocumentWriter[TorrentDir] = Macros.writer[TorrentDir]
  implicit val torrentDataWriter: BSONDocumentWriter[FileEntry] = Macros.writer[FileEntry]
  implicit val instantWriter = new BSONWriter[Instant, BSONDateTime] {
    override def write(t: Instant): BSONDateTime = BSONDateTime(t.toEpochMilli)
  }

  implicit val torrentWriter: BSONDocumentWriter[TorrentDocument] = Macros.writer[TorrentDocument]

  implicit val livenessWriter: BSONDocumentWriter[Liveness] = Macros.writer[Liveness]

  implicit val localDateWriter = new BSONWriter[LocalDate, BSONString] {
    override def write(t: LocalDate): BSONString = BSONString(localDateToString(t))
  }

  implicit val mapWriter: BSONDocumentWriter[Map[LocalDate, Int]] = new BSONDocumentWriter[Map[LocalDate, Int]] {
    def write(map: Map[LocalDate, Int]): BSONDocument = {
      val elements = map.toStream.map { tuple =>
        localDateToString(tuple._1) -> BSONInteger(tuple._2)
      }
      BSONDocument(elements)
    }
  }

}

object MongoTorrentWriter {

  def localDateToString(date: LocalDate): String = date.toString

} 
开发者ID:bwrega,项目名称:btdogg,代码行数:47,代码来源:MongoTorrentWriter.scala


示例2: AggregateVote

//设置package包名称以及导入依赖的类
package lila.puzzle

case class AggregateVote(up: Int, down: Int, nb: Int, ratio: Int) {

  def add(v: Boolean) = copy(
    up = up + v.fold(1, 0),
    down = down + v.fold(0, 1)
  ).computeNbAndRatio

  def change(from: Boolean, to: Boolean) = if (from == to) this else copy(
    up = up + to.fold(1, -1),
    down = down + to.fold(-1, 1)
  ).computeNbAndRatio

  def count = up + down

  def sum = up - down

  def computeNbAndRatio = if (up + down > 0) copy(
    ratio = 100 * (up - down) / (up + down),
    nb = up + down
  )
  else copy(
    ratio = 1,
    nb = 0
  )
}

object AggregateVote {

  val default = AggregateVote(1, 0, 1, 100)
  val disable = AggregateVote(0, 9000, 9000, -100).computeNbAndRatio

  val minRatio = -50
  val minVotes = 30

  import reactivemongo.bson.Macros
  implicit val aggregatevoteBSONHandler = Macros.handler[AggregateVote]
} 
开发者ID:DrNixx,项目名称:line,代码行数:40,代码来源:AggregateVote.scala


示例3: Metadata

//设置package包名称以及导入依赖的类
package lila.game

import java.security.MessageDigest
import lila.db.ByteArray
import org.joda.time.DateTime

private[game] case class Metadata(
    source: Option[Source],
    pgnImport: Option[PgnImport],
    tournamentId: Option[String],
    simulId: Option[String],
    tvAt: Option[DateTime],
    analysed: Boolean
) {

  def pgnDate = pgnImport flatMap (_.date)

  def pgnUser = pgnImport flatMap (_.user)

  def isEmpty = this == Metadata.empty
}

private[game] object Metadata {

  val empty = Metadata(None, None, None, None, None, false)
}

case class PgnImport(
  user: Option[String],
  date: Option[String],
  pgn: String,
  // hashed PGN for DB unicity
  h: Option[ByteArray]
)

object PgnImport {

  def hash(pgn: String) = ByteArray {
    MessageDigest getInstance "MD5" digest
      pgn.lines.map(_.replace(" ", "")).filter(_.nonEmpty).mkString("\n").getBytes("UTF-8") take 12
  }

  def make(
    user: Option[String],
    date: Option[String],
    pgn: String
  ) = PgnImport(
    user = user,
    date = date,
    pgn = pgn,
    h = hash(pgn).some
  )

  import reactivemongo.bson.Macros
  import ByteArray.ByteArrayBSONHandler
  implicit val pgnImportBSONHandler = Macros.handler[PgnImport]
} 
开发者ID:DrNixx,项目名称:line,代码行数:58,代码来源:Metadata.scala


示例4: OcrEntity

//设置package包名称以及导入依赖的类
package model

import ocr.model.OcrTextAnnotation
import reactivemongo.bson.{BSONDocument, BSONDocumentReader, BSONDocumentWriter, Macros}

case class OcrEntity(
    id: String,
    userId: String,
    result: OcrTextAnnotation
) extends WithId

case class OcrText(text: String)

case class OcrTextOnly(
    id: String,
    result: OcrText
) extends WithId

object OcrTextOnly {
  implicit def ocrTextReader: BSONDocumentReader[OcrText] = Macros.reader[OcrText]
  implicit object OcrTextOnlyBSONReader extends BSONDocumentReader[OcrTextOnly] {

    def read(doc: BSONDocument): OcrTextOnly =
      Serialization.deserialize(doc,
                                OcrTextOnly(
                                  id = doc.getAs[String]("_id").get,
                                  result = doc.getAs[OcrText]("result").get
                                ))
  }
}

object OcrEntity {

  implicit object OcrEntityBSONReader extends BSONDocumentReader[OcrEntity] {

    def read(doc: BSONDocument): OcrEntity =
      Serialization.deserialize(doc,
                                OcrEntity(
                                  id = doc.getAs[String]("_id").get,
                                  userId = doc.getAs[String]("userId").get,
                                  result = doc.getAs[OcrTextAnnotation]("result").get
                                ))
  }

  implicit object OcrEntityBSONWriter extends BSONDocumentWriter[OcrEntity] {

    def write(ocrResult: OcrEntity): BSONDocument = {
      BSONDocument(
        "_id"    -> ocrResult.id,
        "userId" -> ocrResult.userId,
        "result" -> ocrResult.result
      )
    }
  }
} 
开发者ID:Leonti,项目名称:receipts-rest-service,代码行数:56,代码来源:OcrEntity.scala



注:本文中的reactivemongo.bson.Macros类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala ShimLoader类代码示例发布时间:2022-05-23
下一篇:
Scala MailerClient类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap