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

Commit 5565e6b

Browse files
wajdarashtao
authored andcommitted
arango #9 - add support for Vector + sequences wrapped as Any
1 parent f899001 commit 5565e6b

File tree

4 files changed

+94
-32
lines changed

4 files changed

+94
-32
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@ import com.arangodb.velocypack.VPackModule
44
import com.arangodb.velocypack.VPackSetupContext
55
import com.arangodb.velocypack.module.scala.internal.VPackScalaSerializers
66
import com.arangodb.velocypack.module.scala.internal.VPackScalaDeserializers
7-
import com.arangodb.velocypack.VPackInstanceCreator
87

98
class VPackScalaModule extends VPackModule {
109

1110
def setup[C <: VPackSetupContext[C]](context: C): Unit = {
12-
context.registerDeserializer(classOf[Option[Any]], VPackScalaDeserializers.OPTION, true)
13-
context.registerDeserializer(classOf[Seq[Any]], VPackScalaDeserializers.SEQ)
14-
context.registerDeserializer(classOf[List[Any]], VPackScalaDeserializers.LIST)
11+
12+
// deserializers
13+
14+
context.registerDeserializer(classOf[Seq[Any]], VPackScalaDeserializers.VECTOR)
15+
context.registerDeserializer(classOf[Vector[_]], VPackScalaDeserializers.VECTOR)
16+
context.registerDeserializer(classOf[List[_]], VPackScalaDeserializers.LIST)
17+
1518
context.registerDeserializer(classOf[Map[Any, Any]], VPackScalaDeserializers.MAP)
19+
context.registerDeserializer(classOf[Option[Any]], VPackScalaDeserializers.OPTION, true)
1620
context.registerDeserializer(classOf[BigInt], VPackScalaDeserializers.BIG_INT)
1721
context.registerDeserializer(classOf[BigDecimal], VPackScalaDeserializers.BIG_DECIMAL)
1822

19-
context.registerSerializer(classOf[Option[Any]], VPackScalaSerializers.OPTION)
20-
context.registerSerializer(classOf[Seq[Any]], VPackScalaSerializers.SEQ)
21-
context.registerSerializer(classOf[List[Any]], VPackScalaSerializers.LIST)
22-
context.registerSerializer(classOf[Map[Any, Any]], VPackScalaSerializers.MAP)
23+
// serializers
24+
25+
Set(
26+
classOf[List[Any]],
27+
classOf[Vector[Any]],
28+
classOf[Seq[Any]],
29+
Seq(Unit).getClass,
30+
Seq.empty.getClass,
31+
Nil.getClass
32+
).foreach(context.registerSerializer(_, VPackScalaSerializers.SEQ))
33+
2334
context.registerEnclosingSerializer(classOf[Map[Any, Any]], VPackScalaSerializers.MAP)
2435
context.registerSerializer(classOf[BigInt], VPackScalaSerializers.BIG_INT)
36+
context.registerSerializer(classOf[Option[Any]], VPackScalaSerializers.OPTION)
2537
context.registerSerializer(classOf[BigDecimal], VPackScalaSerializers.BIG_DECIMAL)
2638
}
2739

28-
}
40+
}

src/main/scala/com/arangodb/velocypack/module/scala/internal/VPackScalaDeserializers.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.arangodb.velocypack.VPackSlice
66
import com.arangodb.velocypack.VPackDeserializerParameterizedType
77
import java.lang.reflect.ParameterizedType
88
import scala.collection.JavaConversions._
9-
import scala.collection.JavaConverters._
109

1110
object VPackScalaDeserializers {
1211

@@ -23,23 +22,23 @@ object VPackScalaDeserializers {
2322
}
2423
}
2524

26-
val SEQ = new VPackDeserializerParameterizedType[Seq[Any]] {
27-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Seq[Any] =
25+
val LIST = new VPackDeserializerParameterizedType[List[Any]] {
26+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): List[Any] =
2827
throw new UnsupportedOperationException
2928

30-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): Seq[Any] = {
29+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): List[Any] = {
3130
val clazz = t.getActualTypeArguments()(0).asInstanceOf[Class[Any]]
32-
vpack.arrayIterator().map { slice: VPackSlice => context.deserialize[Any](slice, clazz) }.toSeq
31+
vpack.arrayIterator().map { slice: VPackSlice => context.deserialize[Any](slice, clazz) }.toList
3332
}
3433
}
3534

36-
val LIST = new VPackDeserializerParameterizedType[List[Any]] {
37-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): List[Any] =
35+
val VECTOR = new VPackDeserializerParameterizedType[Vector[Any]] {
36+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Vector[Any] =
3837
throw new UnsupportedOperationException
3938

40-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): List[Any] = {
39+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): Vector[Any] = {
4140
val clazz = t.getActualTypeArguments()(0).asInstanceOf[Class[Any]]
42-
vpack.arrayIterator().map { slice: VPackSlice => context.deserialize[Any](slice, clazz) }.toList
41+
vpack.arrayIterator().map { slice: VPackSlice => context.deserialize[Any](slice, clazz) }.toVector
4342
}
4443
}
4544

@@ -58,4 +57,4 @@ object VPackScalaDeserializers {
5857
BigDecimal.javaBigDecimal2bigDecimal(context.deserialize(vpack, classOf[java.math.BigDecimal]))
5958
}
6059

61-
}
60+
}

src/main/scala/com/arangodb/velocypack/module/scala/internal/VPackScalaSerializers.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ object VPackScalaSerializers {
2020
}
2121
}
2222

23-
val LIST = new VPackSerializer[List[Any]] {
24-
def serialize(builder: VPackBuilder, attribute: String, value: List[Any], context: VPackSerializationContext): Unit = {
25-
val list: _root_.java.util.List[Any] = ListBuffer(value: _*)
26-
context.serialize(builder, attribute, list)
27-
}
28-
}
29-
3023
val MAP = new VPackSerializer[Map[Any, Any]] {
3124
def serialize(builder: VPackBuilder, attribute: String, value: Map[Any, Any], context: VPackSerializationContext): Unit =
3225
context.serialize(builder, attribute, mapAsJavaMap(value))
@@ -41,4 +34,4 @@ object VPackScalaSerializers {
4134
def serialize(builder: VPackBuilder, attribute: String, value: BigDecimal, context: VPackSerializationContext): Unit =
4235
context.serialize(builder, attribute, value.bigDecimal)
4336
}
44-
}
37+
}

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

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

3-
import org.scalatest.Matchers
4-
import org.scalatest.FunSuite
3+
import java.{util => ju}
4+
5+
import com.arangodb.velocypack.{VPack, VPackBuilder, ValueType}
6+
import org.scalatest.{FunSuite, Matchers}
7+
58
import scala.beans.BeanProperty
6-
import com.arangodb.velocypack.VPack
7-
import com.arangodb.velocypack.VPackBuilder
8-
import com.arangodb.velocypack.ValueType
99

1010
case class SeqTestEntity(@BeanProperty var s: Seq[String] = Seq(), @BeanProperty var i: Seq[Int] = Seq(), @BeanProperty var o: Seq[SeqTestEntity] = Seq()) {
1111
def this() = this(s = Seq())
1212
}
1313

14+
case class WrappedSeqTestEntity(map: Map[String, Any], seq: Seq[Any], opt: Option[Seq[Any]]) {
15+
def this() = this(Map.empty, Nil, None)
16+
}
17+
1418
class VPackSeqTest extends FunSuite with Matchers {
1519

1620
test("serialize seq") {
@@ -31,6 +35,31 @@ class VPackSeqTest extends FunSuite with Matchers {
3135
vpack.get("o").get(0).isObject should be(true)
3236
}
3337

38+
test("serialize wrapped sequence") {
39+
val testEntity = WrappedSeqTestEntity(
40+
map = Map("foo" -> Seq("a", "b")),
41+
seq = Seq(List(1, 2, 3)),
42+
opt = Some(Nil)
43+
)
44+
val vp = new VPack.Builder().registerModule(new VPackScalaModule).build()
45+
val vpack = vp.serialize(testEntity)
46+
vpack should not be null
47+
vpack.isObject should be(true)
48+
vpack.get("map").isObject should be(true)
49+
vpack.get("map").get("foo").isArray should be(true)
50+
vpack.get("map").get("foo").size should be(2)
51+
vpack.get("map").get("foo").get(0).getAsString should be("a")
52+
vpack.get("map").get("foo").get(1).getAsString should be("b")
53+
vpack.get("seq").isArray should be(true)
54+
vpack.get("seq").get(0).isArray should be(true)
55+
vpack.get("seq").get(0).size() should be(3)
56+
vpack.get("seq").get(0).get(0).getAsInt should be(1)
57+
vpack.get("seq").get(0).get(1).getAsInt should be(2)
58+
vpack.get("seq").get(0).get(2).getAsInt should be(3)
59+
vpack.get("opt").isArray should be(true)
60+
vpack.get("opt").size() should be(0)
61+
}
62+
3463
test("deserialize seq") {
3564
val builder = new VPackBuilder()
3665
builder add ValueType.OBJECT
@@ -58,4 +87,33 @@ class VPackSeqTest extends FunSuite with Matchers {
5887
entity.o(0) shouldBe a [SeqTestEntity]
5988
}
6089

90+
test("deserialize wrapped sequence") {
91+
val builder = new VPackBuilder()
92+
builder add ValueType.OBJECT
93+
builder add ("map", ValueType.OBJECT)
94+
builder add ("foo", ValueType.ARRAY)
95+
builder add "a"
96+
builder add "b"
97+
builder.close
98+
builder.close
99+
builder add ("seq", ValueType.ARRAY)
100+
builder add ValueType.ARRAY
101+
builder add new Integer(1)
102+
builder add new Integer(2)
103+
builder add new Integer(3)
104+
builder.close
105+
builder.close
106+
builder add ("opt", ValueType.ARRAY)
107+
builder.close
108+
builder.close
109+
110+
val vp = new VPack.Builder().registerModule(new VPackScalaModule).build()
111+
val entity: WrappedSeqTestEntity = vp.deserialize(builder.slice, classOf[WrappedSeqTestEntity])
112+
113+
entity shouldEqual WrappedSeqTestEntity(
114+
map = Map("foo" -> ju.Arrays.asList("a", "b")),
115+
seq = Seq(ju.Arrays.asList[Long](1, 2, 3)),
116+
opt = Some(Nil)
117+
)
118+
}
61119
}

0 commit comments

Comments
 (0)