项目作者: evolution-gaming

项目描述 :
Scala macros for compile-time generation of Kryo serializers
高级语言: Scala
项目地址: git://github.com/evolution-gaming/kryo-macros.git
创建时间: 2016-10-18T15:07:18Z
项目社区:https://github.com/evolution-gaming/kryo-macros

开源协议:Other

下载


Kryo Macros Build Status license version

Scala macros that generate com.esotericsoftware.kryo.Serializer implementations in compile time, based on compile time reflection.

Features and limitations

  • On top level only case classes are supported
  • Fields of case classes can be other case classes, Scala collections, options, primitive or AnyVal types & classes,
    tuples, Scala enums, standard types & classes: String, Either, BigDecimal, java.time.Instant,
    scala.concurrent.duration.FiniteDuration, org.joda.time.DateTime
  • Fields can be annotated as transient or just be not defined in constructor to avoid parsing and serializing
  • For nested structures need to generate serializers for all case classes
  • Implicitly defined mapping helpers are supported for ADT structures, simple alternative mappings, etc.
  • Manual serializers can be used in generated code when defined as implicits

How to use

Add the following resolver

  1. resolvers += Resolver.bintrayRepo("evolutiongaming", "maven")

Add the library to your dependencies list

  1. libraryDependencies += "com.evolutiongaming" %% "kryo-macros" % "1.3.0"

Generate some serializers for your case classes

  1. import com.evolutiongaming.kryo.Serializer
  2. case class Player(name: String)
  3. val serializer = Serializer.make[Player]

That’s it! You have generated a com.esotericsoftware.kryo.Serializer implementation for your Player.
You must know what to do with it if you are here :)

To serialize objects that extends sealed traits/class use Serializer.makeCommon call:

  1. import com.evolutiongaming.kryo.{ConstSerializer, Serializer}
  2. sealed trait Reason
  3. object Reason {
  4. case object Close extends Reason
  5. case object Pause extends Reason
  6. }
  7. val reasonSerializer = Serializer.makeCommon[Reason] {
  8. case 0 => ConstSerializer(Reason.Close)
  9. case 1 => ConstSerializer(Reason.Pause)
  10. }
  11. sealed abstract class Message(val text: String)
  12. object Message {
  13. case object Common extends Message("common")
  14. case object Notification extends Message("notification")
  15. }
  16. private implicit val messageSerializer = Serializer.makeMapping[Message] {
  17. case 0 => Message.Common
  18. case 1 => Message.Notification
  19. }

To see generated code just add the following line to your sbt build file

  1. scalacOptions += "-Xmacro-settings:print-serializers"

For more examples, please, check out
SerializerMacroSpec

How to develop

Run tests, check coverage & binary compatibility for both supported Scala versions

  1. sbt clean +coverage +test +coverageReport +mimaReportBinaryIssues

Run benchmarks

  1. sbt -no-colors clean 'benchmark/jmh:run -prof gc .*SerializerBenchmark.*' >results.txt

Release

For version numbering use Recommended Versioning Scheme
that is widely adopted in the Scala ecosystem.

Double-check binary & source compatibility and release using following command (credentials required):

  1. sbt release