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

Commit

Permalink
Fix wrong bytes size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
vovapolu authored and fomkin committed Mar 17, 2019
1 parent a95cebd commit 94399cc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion core/src/main/scala/zhukov/SizeMeter.scala
Expand Up @@ -36,7 +36,11 @@ object SizeMeter {
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)
implicit def bytes[B](implicit bytes: Bytes[B]): SizeMeter[B] = SizeMeter(value => {
val len = bytes.size(value).toInt
CodedOutputStream.computeRawVarint32Size(len) + len
})

implicit def iterable[A, Col[_] <: Iterable[A]](implicit sm: SizeMeter[A]): SizeMeter[Col[A]] =
SizeMeter(xs => sm.measureValues(xs.toIterable))
}
23 changes: 21 additions & 2 deletions derivation/src/test/scala/AdtTest.scala
@@ -1,25 +1,36 @@
import minitest.SimpleTestSuite
import zhukov.{Marshaller, Unmarshaller}
import zhukov.{Default, Marshaller, Unmarshaller}
import zhukov.derivation._
import zhukov.Default.auto._
object AdtTest extends SimpleTestSuite {

case class NestedBytes(bs: Array[Byte])

sealed trait Foo
case class A(x: Int, y: Int) extends Foo
case class B(v: String) extends Foo
case object C extends Foo
case class D(n: NestedBytes) extends Foo

case class Bar(foo: Foo = A(0, 0), x: Int = 0)


implicit val nestedsm = sizeMeter[NestedBytes]
implicit val nestedm = marshaller[NestedBytes]
implicit val nestedum = unmarshaller[NestedBytes]
implicit val nestedDefault = Default(NestedBytes(Array.empty))

implicit val aum = unmarshaller[A]
implicit val bum = unmarshaller[B]
implicit val cum = unmarshaller[C.type]
implicit val dum = unmarshaller[D]
implicit val am = marshaller[A]
implicit val bm = marshaller[B]
implicit val cm = marshaller[C.type]
implicit val dm = marshaller[D]
implicit val asm = sizeMeter[A]
implicit val bsm = sizeMeter[B]
implicit val csm = sizeMeter[C.type]
implicit val dsm = sizeMeter[D]
implicit val foosm = sizeMeter[Foo]
implicit val foom = marshaller[Foo]
implicit val fooum = unmarshaller[Foo]
Expand Down Expand Up @@ -47,6 +58,14 @@ object AdtTest extends SimpleTestSuite {
assert(result == C)
}

test("ADT with case class: Foo.D") {
val sample = D(NestedBytes("foobar".getBytes))
val bytes = Marshaller[Foo].write(sample)
val result = Unmarshaller[Foo].read(bytes)
assert(result.isInstanceOf[D])
assert(result.asInstanceOf[D].n.bs sameElements sample.n.bs)
}

test("ADT as part of another message") {
val sample = Bar(C, 10)
val bytes = Marshaller[Bar].write(sample)
Expand Down

0 comments on commit 94399cc

Please sign in to comment.