fomkin / zhukov   0.4.0

Apache License 2.0 GitHub

WIP Scala-driven Protocol Buffers and gRPC

Scala versions: 2.12

Zhukov

Zhukov is a library for marshaling Scala types in Google Protocol Buffers format without writing .proto files. Unlike other Protocol Buffers libraries, Zhukov doesn't depend on com.google.protobuf : protobuf-java. It means that you can use desirable implementation of byte sequence without conversion from com.google.protobuf.ByteString. Zhukov has written using pure Scala macros without generic programming. It's fast in compile time and runtime.

Implemented features

  1. Case classes to messages
  2. Default case class parameters
  3. Sealed traits to messages with oneof
  4. Autoderivation
  5. Recursive types

Plan

  1. Polymorphic messages (when the value of the type parameter known only in runtime).
  2. Code generator for .proto files.
  3. gRPC services
  4. Optional code generation from .proto files.

Usage

Autoderivation

import zhukov.derivation.auto._
import zhukov.{Marshaller, Unmarshaller}

case class SimpleMessage(myNumber: Int = 0, myString: String = "")
val message = SimpleMessage(42, "cow")
Marshaller[SimpleMessage].write(message)

Implementing bytes

import zhukov.Bytes
import com.google.protobuf.ByteString

implicit object ByteStringBytes extends Bytes[ByteString] {
  def empty: ByteString = ByteString.EMPTY
  def copyFromArray(bytes: Array[Byte]): ByteString = ByteString.copyFrom(bytes)
  def copyFromArray(bytes: Array[Byte], offset: Long, size: Long): ByteString = ByteString.copyFrom(bytes, offset.toInt, size.toInt)
  def copyToArray(value: ByteString, array: Array[Byte], sourceOffset: Int, targetOffset: Int, length: Int): Unit = value.copyTo(array, sourceOffset, targetOffset, length)
  def wrapArray(bytes: Array[Byte]): ByteString = ByteString.copyFrom(bytes)
  def copyBuffer(buffer: ByteBuffer): ByteString = ByteString.copyFrom(buffer)
  def toArray(bytes: ByteString): Array[Byte] = bytes.toByteArray
  def toBuffer(bytes: ByteString): ByteBuffer = bytes.asReadOnlyByteBuffer()
  def get(bytes: ByteString, i: Long): Int = bytes.byteAt(i.toInt)
  def size(bytes: ByteString): Long = bytes.size().toLong
  def concat(left: ByteString, right: ByteString): ByteString = left.concat(right)
  def slice(value: ByteString, start: Long, end: Long): ByteString = value.substring(start.toInt, end.toInt)
}