Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
add rest of protobuf types
Browse files Browse the repository at this point in the history
  • Loading branch information
fomkin committed Mar 1, 2019
1 parent 1d9b4b9 commit 7282f05
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 46 deletions.
51 changes: 40 additions & 11 deletions core/src/main/scala/zhukov/Marshaller.scala
@@ -1,8 +1,6 @@
package zhukov

import java.nio.ByteBuffer

import zhukov.protobuf.{CodedInputStream, CodedOutputStream}
import zhukov.protobuf.CodedOutputStream

sealed trait Marshaller[@specialized T] {

Expand All @@ -18,6 +16,15 @@ sealed trait Marshaller[@specialized T] {

object Marshaller {

def apply[T: Marshaller]: Marshaller[T] =
implicitly[Marshaller[T]]

def apply[T](f: (CodedOutputStream, T) => Unit): Marshaller[T] = new Marshaller[T] {
def write(stream: CodedOutputStream, value: T): Unit = f(stream, value)
}

// This types required to select wire type during derivation

sealed trait VarintMarshaller[A] extends Marshaller[A] { self =>
def contramap[B](f: B => A): VarintMarshaller[B] = new VarintMarshaller[B] {
def write(stream: CodedOutputStream, value: B): Unit =
Expand Down Expand Up @@ -46,25 +53,47 @@ object Marshaller {
}
}

def apply[T: Marshaller]: Marshaller[T] =
implicitly[Marshaller[T]]

def apply[T](f: (CodedOutputStream, T) => Unit): Marshaller[T] = new Marshaller[T] {
def write(stream: CodedOutputStream, value: T): Unit = f(stream, value)
}

// Primitives default instances
// Default instances

implicit object IntMarshaller extends VarintMarshaller[Int] {
def write(stream: CodedOutputStream, value: Int): Unit = {
stream.writeRawVarint32(value)
}
}

implicit object LongMarshaller extends VarintMarshaller[Long] {
def write(stream: CodedOutputStream, value: Long): Unit = {
stream.writeRawVarint64(value)
}
}

implicit object FloatMarshaller extends Fixed32Marshaller[Float] {
def write(stream: CodedOutputStream, value: Float): Unit = {
stream.writeFixed32NoTag(java.lang.Float.floatToRawIntBits(value))
}
}

implicit object DoubleMarshaller extends Fixed64Marshaller[Double] {
def write(stream: CodedOutputStream, value: Double): Unit = {
stream.writeFixed64NoTag(java.lang.Double.doubleToRawLongBits(value))
}
}

implicit object BooleanMarshaller extends VarintMarshaller[Boolean] {
def write(stream: CodedOutputStream, value: Boolean): Unit = {
stream.writeBoolNoTag(value)
}
}

implicit object StringMarshaller extends CodedMarshaller[String] {
def write(stream: CodedOutputStream, value: String): Unit = {
stream.writeStringNoTag(value)
}
}

implicit def bytesMarshaller[B: Bytes]: CodedMarshaller[B] = new CodedMarshaller[B] {
def write(stream: CodedOutputStream, value: B): Unit = {
stream.writeBytesNoTag(value)
}
}
}
14 changes: 8 additions & 6 deletions core/src/main/scala/zhukov/SizeMeter.scala
@@ -1,6 +1,7 @@
package zhukov

import zhukov.protobuf.CodedOutputStream
import zhukov.protobuf.CodedOutputStream.{LITTLE_ENDIAN_32_SIZE, LITTLE_ENDIAN_64_SIZE}

sealed trait SizeMeter[T] {

Expand All @@ -23,10 +24,11 @@ object SizeMeter {
def measure(value: T): Int = f(value)
}

implicit val int: SizeMeter[Int] =
SizeMeter(CodedOutputStream.computeRawVarint32Size _)

CodedOutputStream
implicit val string: SizeMeter[String] =
SizeMeter(CodedOutputStream.computeStringSizeNoTag _)
implicit val int: SizeMeter[Int] = SizeMeter(CodedOutputStream.computeRawVarint32Size _)
implicit val long: SizeMeter[Long] = SizeMeter(CodedOutputStream.computeRawVarint64Size _)
implicit val float: SizeMeter[Float] = SizeMeter(_ => LITTLE_ENDIAN_32_SIZE)
implicit val double: SizeMeter[Double] = SizeMeter(_ => LITTLE_ENDIAN_64_SIZE)
implicit val boolean: SizeMeter[Boolean] = SizeMeter(CodedOutputStream.computeBoolSizeNoTag _)
implicit val string: SizeMeter[String] = SizeMeter(CodedOutputStream.computeStringSizeNoTag _)
implicit def bytes[B](implicit bytes: Bytes[B]): SizeMeter[B] = SizeMeter(value => bytes.size(value).toInt)
}
64 changes: 48 additions & 16 deletions core/src/main/scala/zhukov/Unmarshaller.scala
Expand Up @@ -2,7 +2,7 @@ package zhukov

import zhukov.protobuf.CodedInputStream

trait Unmarshaller[A] {
sealed trait Unmarshaller[A] {

def read(stream: CodedInputStream): A

Expand All @@ -19,30 +19,62 @@ object Unmarshaller {
* For length-delimited unmarshaller with
* length-reading defined inside CodedInputStream.
*/
trait CodedUnmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): CodedUnmarshaller[B] =
(stream: CodedInputStream) => f(self.read(stream))
sealed trait CodedUnmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): CodedUnmarshaller[B] = new CodedUnmarshaller[B] {
def read(stream: CodedInputStream): B = f(self.read(stream))
}
}

trait VarintUnmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): VarintUnmarshaller[B] =
(stream: CodedInputStream) => f(self.read(stream))
sealed trait VarintUnmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): VarintUnmarshaller[B] = new VarintUnmarshaller[B] {
def read(stream: CodedInputStream): B = f(self.read(stream))
}
}

trait Fixed32Unmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): Fixed32Unmarshaller[B] =
(stream: CodedInputStream) => f(self.read(stream))
sealed trait Fixed32Unmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): Fixed32Unmarshaller[B] = new Fixed32Unmarshaller[B] {
def read(stream: CodedInputStream): B = f(self.read(stream))
}
}

trait Fixed64Unmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): Fixed64Unmarshaller[B] =
(stream: CodedInputStream) => f(self.read(stream))
sealed trait Fixed64Unmarshaller[A] extends Unmarshaller[A] { self =>
def map[B](f: A => B): Fixed64Unmarshaller[B] = new Fixed64Unmarshaller[B] {
def read(stream: CodedInputStream): B = f(self.read(stream))
}
}

def apply[T](implicit unmarshaller: Unmarshaller[T]): Unmarshaller[T] =
unmarshaller

implicit val int: VarintUnmarshaller[Int] = _.readRawVarint32()
implicit val long: VarintUnmarshaller[Long] = _.readRawVarint64()
implicit val string: CodedUnmarshaller[String] = _.readString()
def apply[T](f: CodedInputStream => T): Unmarshaller[T] = new Unmarshaller[T] {
def read(stream: CodedInputStream): T = f(stream)
}

implicit val bool: VarintUnmarshaller[Boolean] = new VarintUnmarshaller[Boolean] {
def read(stream: CodedInputStream): Boolean = stream.readBool()
}

implicit val int: VarintUnmarshaller[Int] = new VarintUnmarshaller[Int] {
def read(stream: CodedInputStream): Int = stream.readRawVarint32()
}

implicit val long: VarintUnmarshaller[Long] = new VarintUnmarshaller[Long] {
def read(stream: CodedInputStream): Long = stream.readRawVarint64()
}

implicit val float: Fixed32Unmarshaller[Float] = new Fixed32Unmarshaller[Float] {
def read(stream: CodedInputStream): Float = stream.readFloat()
}

implicit val double: Fixed64Unmarshaller[Double] = new Fixed64Unmarshaller[Double] {
def read(stream: CodedInputStream): Double = stream.readDouble()
}

implicit val string: CodedUnmarshaller[String] = new CodedUnmarshaller[String] {
def read(stream: CodedInputStream): String = stream.readString()
}

implicit def bytes[T: Bytes]: CodedUnmarshaller[T] = new CodedUnmarshaller[T] {
def read(stream: CodedInputStream): T = stream.readBytes
}
}
Expand Up @@ -311,11 +311,9 @@ class ZhukovDerivationMacro(val c: blackbox.Context) {
private def sealedTraitUnmarshaller(T: Type, ts: ClassSymbol): Tree = {
val fields = resolveSealedTraitFields(T, ts)
q"""
new zhukov.Unmarshaller[$T] {
def read(_stream: zhukov.protobuf.CodedInputStream) = {
..${commonUnmarshaller(T, fields)}
_value
}
zhukov.Unmarshaller[$T] { (_stream: zhukov.protobuf.CodedInputStream) =>
..${commonUnmarshaller(T, fields)}
_value
}
"""
}
Expand Down Expand Up @@ -352,11 +350,9 @@ class ZhukovDerivationMacro(val c: blackbox.Context) {
q"$originalName = $varName"
}
q"""
new zhukov.Unmarshaller[$T] {
def read(_stream: zhukov.protobuf.CodedInputStream) = {
..${commonUnmarshaller(T, fields)}
$module.apply(..$applyArgs)
}
zhukov.Unmarshaller[$T] { (_stream: zhukov.protobuf.CodedInputStream) =>
..${commonUnmarshaller(T, fields)}
$module.apply(..$applyArgs)
}
"""
}
Expand Down
11 changes: 11 additions & 0 deletions derivation/src/test/protobuf/messages.proto
Expand Up @@ -17,6 +17,11 @@ message MessageWithRepeatedString {
repeated string myStrings = 2;
}

message WithBytes {
bytes myBytes = 1;
int64 myLong = 2;
}

message Lit {
int32 value = 1;
}
Expand All @@ -30,4 +35,10 @@ message Expr {
Lit lit = 1;
Add add = 2;
}
}

message OtherTypes {
float f = 1;
double d = 2;
bool b = 3;
}

0 comments on commit 7282f05

Please sign in to comment.