Skip to content

Commit

Permalink
removed fallback map codecs that use key-value pair list encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Mar 29, 2018
1 parent b77e116 commit 5e6be68
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait GenCodec[T] {
def write(output: Output, value: T): Unit
}

object GenCodec extends FallbackMapCodecs with TupleGenCodecs {
object GenCodec extends RecursiveAutoCodecs with TupleGenCodecs {
/**
* Macro that automatically materializes a [[GenCodec]] for some type `T`, which must be one of:
* <ul>
Expand Down Expand Up @@ -405,48 +405,6 @@ object GenCodec extends FallbackMapCodecs with TupleGenCodecs {
implicit lazy val NothingAutoCodec: GenCodec.Auto[Nothing] = GenCodec.Auto[Nothing](NothingCodec)
}

/**
* Contains readers for maps where there is no DBKeyCodec for key type. In such case, we assume reading from a list
* of key-value pairs instead of JSON object.
*/
trait FallbackMapCodecs extends RecursiveAutoCodecs { this: GenCodec.type =>
private def readKVPair[K: GenCodec, V: GenCodec](input: ObjectInput): (K, V) = {
val key = read[K](input.nextField().assertField("k"))
val value = read[V](input.nextField().assertField("v"))
(key, value)
}

private def writeKVPair[K, V](output: ObjectOutput, key: K, value: V)(implicit keyCodec: GenCodec[K], valueCodec: GenCodec[V]): Unit = {
keyCodec.write(output.writeField("k"), key)
valueCodec.write(output.writeField("v"), value)
output.finish()
}

private def collectPairsTo[K: GenCodec, V: GenCodec, C](li: ListInput)(implicit cbf: CanBuildFrom[Nothing, (K, V), C]): C = {
val b = cbf()
while (li.hasNext) {
b += readKVPair[K, V](li.nextElement().readObject())
}
b.result()
}

implicit def fallbackMapCodec[M[X, Y] <: BMap[X, Y], K: GenCodec, V: GenCodec](
implicit cbf: CanBuildFrom[Nothing, (K, V), M[K, V]]): GenCodec[M[K, V] with BMap[K, V]] =
createList[M[K, V] with BMap[K, V]](
collectPairsTo[K, V, M[K, V]],
(lo, map) => map.iterator.foreach({ case (k, v) => writeKVPair(lo.writeElement().writeObject(), k, v) }),
allowNull = true
)

implicit def fallbackJMapCodec[M[X, Y] <: JMap[X, Y], K: GenCodec, V: GenCodec](
implicit cbf: JCanBuildFrom[(K, V), M[K, V]]): GenCodec[M[K, V] with JMap[K, V]] =
createList[M[K, V] with JMap[K, V]](
collectPairsTo[K, V, M[K, V]],
(lo, map) => map.asScala.iterator.foreach({ case (k, v) => writeKVPair(lo.writeElement().writeObject(), k, v) }),
allowNull = true
)
}

trait RecursiveAutoCodecs { this: GenCodec.type =>
/**
* Like `materialize`, but descends into types that `T` is made of (e.g. case class field types).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ class GenCodecTest extends CodecTestBase {
testWriteReadAndAutoWriteRead[JHashMap[String, Int]](jHashMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
testWriteReadAndAutoWriteRead[JLinkedHashMap[String, Int]](jLinkedHashMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
testWriteReadAndAutoWriteRead[JHashMap[Int, Int]](jIntHashMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
testSameElementsWriteRead[JHashMap[Double, Int]](jDoubleHashMap,
List(Map[String, Any]("k" -> 1.0, "v" -> 1), Map[String, Any]("k" -> 2.0, "v" -> 2), Map[String, Any]("k" -> 3.0, "v" -> 3))
)
}

test("NoState test") {
Expand All @@ -92,8 +89,6 @@ class GenCodecTest extends CodecTestBase {
testWriteReadAndAutoWriteRead[Set[Int]](set, set.toList)
testWriteReadAndAutoWriteRead[Map[String, Int]](map, map)
testWriteReadAndAutoWriteRead[Map[Int, Int]](intMap, ListMap("1" -> 1, "2" -> 2, "3" -> 3))
testWriteReadAndAutoWriteRead[Map[Double, Int]](doubleMap,
List(Map[String, Any]("k" -> 1.0, "v" -> 1), Map[String, Any]("k" -> 2.0, "v" -> 2), Map[String, Any]("k" -> 3.0, "v" -> 3)))
testWriteReadAndAutoWriteRead[IHashMap[String, Int]](hashMap, hashMap)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.avsystem.commons
package mongo

import com.avsystem.commons.serialization.GenCodec
import com.avsystem.commons.serialization.{GenCodec, GenKeyCodec}
import org.bson.types.ObjectId

trait BsonGenCodecs {
implicit def objectIdCodec: GenCodec[ObjectId] = BsonGenCodecs.objectIdCodec
implicit def objectIdKeyCodec: GenKeyCodec[ObjectId] = BsonGenCodecs.objectIdKeyCodec
}

object BsonGenCodecs {
Expand All @@ -19,4 +20,6 @@ object BsonGenCodecs {
case (otherOutput, objectId) => otherOutput.writeString(objectId.toHexString)
}
)
implicit val objectIdKeyCodec: GenKeyCodec[ObjectId] =
GenKeyCodec.create(new ObjectId(_), _.toHexString)
}

0 comments on commit 5e6be68

Please sign in to comment.