Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit b5b8054

Browse files
wajdarashtao
authored andcommitted
arango #8 - add support for various immutable map types
1 parent 5565e6b commit b5b8054

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

src/main/scala/com/arangodb/velocypack/module/scala/VPackScalaModule.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.arangodb.velocypack.VPackSetupContext
55
import com.arangodb.velocypack.module.scala.internal.VPackScalaSerializers
66
import com.arangodb.velocypack.module.scala.internal.VPackScalaDeserializers
77

8+
import scala.collection.immutable.{HashMap, ListMap, TreeMap}
9+
810
class VPackScalaModule extends VPackModule {
911

1012
def setup[C <: VPackSetupContext[C]](context: C): Unit = {
@@ -16,6 +18,7 @@ class VPackScalaModule extends VPackModule {
1618
context.registerDeserializer(classOf[List[_]], VPackScalaDeserializers.LIST)
1719

1820
context.registerDeserializer(classOf[Map[Any, Any]], VPackScalaDeserializers.MAP)
21+
1922
context.registerDeserializer(classOf[Option[Any]], VPackScalaDeserializers.OPTION, true)
2023
context.registerDeserializer(classOf[BigInt], VPackScalaDeserializers.BIG_INT)
2124
context.registerDeserializer(classOf[BigDecimal], VPackScalaDeserializers.BIG_DECIMAL)
@@ -31,7 +34,16 @@ class VPackScalaModule extends VPackModule {
3134
Nil.getClass
3235
).foreach(context.registerSerializer(_, VPackScalaSerializers.SEQ))
3336

37+
Set(
38+
classOf[HashMap.HashMap1[_, _]],
39+
classOf[HashMap.HashTrieMap[_, _]],
40+
classOf[TreeMap[_, _]],
41+
ListMap(Unit -> Unit).getClass,
42+
classOf[Map[_, _]]
43+
).foreach(context.registerSerializer(_, VPackScalaSerializers.MAP))
44+
3445
context.registerEnclosingSerializer(classOf[Map[Any, Any]], VPackScalaSerializers.MAP)
46+
3547
context.registerSerializer(classOf[BigInt], VPackScalaSerializers.BIG_INT)
3648
context.registerSerializer(classOf[Option[Any]], VPackScalaSerializers.OPTION)
3749
context.registerSerializer(classOf[BigDecimal], VPackScalaSerializers.BIG_DECIMAL)

src/test/scala/com/arangodb/velocypack/module/scala/VPackMapTest.scala

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.arangodb.velocypack.module.scala
22

3-
import org.scalatest.FunSuite
4-
import org.scalatest.Matchers
3+
import com.arangodb.velocypack.module.scala.VPackMapTest._
4+
import com.arangodb.velocypack.{VPack, VPackBuilder, ValueType}
5+
import org.scalatest.{FunSuite, Matchers}
6+
57
import scala.beans.BeanProperty
6-
import com.arangodb.velocypack.VPack
7-
import com.arangodb.velocypack.VPackBuilder
8-
import com.arangodb.velocypack.ValueType
8+
import scala.collection.immutable._
99

1010
case class MapTestEntity(@BeanProperty var m: Map[String, Any] = Map()) {
1111
def this() = this(Map())
@@ -47,6 +47,61 @@ class VPackMapTest extends FunSuite with Matchers {
4747
vpack.get("o").get("ss").getAsString should be("hello world")
4848
}
4949

50+
51+
test("serialize different kinds of nested maps") {
52+
val vp = new VPack.Builder().registerModule(new VPackScalaModule).build()
53+
54+
val entity = MapTestEntity(m =
55+
HashTrieMap("seq" -> Seq(
56+
HashTrieMap("foo" -> 42),
57+
SortedMap("foo" -> 42),
58+
ListMap("foo" -> 42),
59+
HashMap("foo" -> 42),
60+
Map("foo" -> 42),
61+
ListMap.empty,
62+
Map.empty
63+
)))
64+
65+
val vpack = vp.serialize(entity)
66+
vpack should not be null
67+
vpack.isObject should be(true)
68+
vpack.size should be(1)
69+
vpack.get("m").isObject should be(true)
70+
vpack.get("m").size should be(1)
71+
vpack.get("m").get("seq").isArray should be(true)
72+
73+
vpack.get("m").get("seq").get(0).isObject should be(true)
74+
vpack.get("m").get("seq").get(0).size should be(1)
75+
vpack.get("m").get("seq").get(0).get("foo").isInt should be(true)
76+
vpack.get("m").get("seq").get(0).get("foo").getAsInt should be(42)
77+
78+
vpack.get("m").get("seq").get(1).isObject should be(true)
79+
vpack.get("m").get("seq").get(1).size should be(1)
80+
vpack.get("m").get("seq").get(1).get("foo").isInt should be(true)
81+
vpack.get("m").get("seq").get(1).get("foo").getAsInt should be(42)
82+
83+
vpack.get("m").get("seq").get(2).isObject should be(true)
84+
vpack.get("m").get("seq").get(2).size should be(1)
85+
vpack.get("m").get("seq").get(2).get("foo").isInt should be(true)
86+
vpack.get("m").get("seq").get(2).get("foo").getAsInt should be(42)
87+
88+
vpack.get("m").get("seq").get(3).isObject should be(true)
89+
vpack.get("m").get("seq").get(3).size should be(1)
90+
vpack.get("m").get("seq").get(3).get("foo").isInt should be(true)
91+
vpack.get("m").get("seq").get(3).get("foo").getAsInt should be(42)
92+
93+
vpack.get("m").get("seq").get(4).isObject should be(true)
94+
vpack.get("m").get("seq").get(4).size should be(1)
95+
vpack.get("m").get("seq").get(4).get("foo").isInt should be(true)
96+
vpack.get("m").get("seq").get(4).get("foo").getAsInt should be(42)
97+
98+
vpack.get("m").get("seq").get(5).isObject should be(true)
99+
vpack.get("m").get("seq").get(5).size should be(0)
100+
101+
vpack.get("m").get("seq").get(5).isObject should be(true)
102+
vpack.get("m").get("seq").get(5).size should be(0)
103+
}
104+
50105
test("deserialize map") {
51106
val builder = new VPackBuilder
52107
builder add ValueType.OBJECT
@@ -60,10 +115,15 @@ class VPackMapTest extends FunSuite with Matchers {
60115
val entity: MapTestEntity = vp.deserialize(builder.slice, classOf[MapTestEntity])
61116
entity should not be null
62117
entity.m.size should be(2)
63-
entity.m.get("s").isDefined should be(true)
64-
entity.m.get("s").get should be("hello world")
65-
entity.m.get("i").isDefined should be(true)
66-
entity.m.get("i").get should be(69)
118+
entity.m.get("s") should be(Some("hello world"))
119+
entity.m.get("i") should be(Some(69))
67120
}
121+
}
122+
123+
object VPackMapTest {
68124

69-
}
125+
object HashTrieMap {
126+
def apply[A, B](pairs: (A, B)*): Map[A, B] = new HashMap.HashTrieMap(0, Array(HashMap(pairs: _*)), pairs.size)
127+
}
128+
129+
}

0 commit comments

Comments
 (0)