Skip to content
Browse files

demonstration of recursive data structure [issue #2]

  • Loading branch information...
1 parent 90c9493 commit 879a67a4b7c68294ac989a794c5d85ddf4e5ada3 @alaz alaz committed May 17, 2013
Showing with 38 additions and 0 deletions.
  1. +20 −0 src/test/scala/Rec.scala
  2. +12 −0 src/test/scala/parserSpec.scala
  3. +6 −0 src/test/scala/writableSpec.scala
View
20 src/test/scala/Rec.scala
@@ -0,0 +1,20 @@
+package com.osinka.subset
+
+/*
+ * Recursive data structure
+ */
+case class Rec(id: Int, children: Option[List[Rec]])
+
+object Rec {
+ // writer
+ implicit object asBson extends BsonWritable[Rec] {
+ override def apply(rec: Rec) =
+ Some( DBO("id" -> rec.id, "children" -> rec.children)() )
+ }
+
+ // reader
+ import DocParser._
+
+ implicit lazy val recField = Field({ case Doc(rec) => rec })
+ lazy val Doc: DocParser[Rec] = get[Int]("id") ~ get[List[Rec]]("children").opt map { case id ~ children => new Rec(id, children) }
+}
View
12 src/test/scala/parserSpec.scala
@@ -75,4 +75,16 @@ class parserSpec extends FunSpec with ShouldMatchers with MongoMatchers with Rou
parser(dbo("event", new BsonSym("ourtype")).add("version", 2).add("l", 10).get) should be('right)
}
}
+ describe("Parser for recursive structures") {
+ it("is possible") {
+ Rec.Doc( DBO("id" -> 123)() ) should equal(Right(Rec(123, None)))
+
+ }
+ it("can be used as another field") {
+ val y = DocParser.get[Rec]("y")
+ val r = Rec(123, Some(Rec(234, None) :: Rec(345, Some(Rec(456, None) :: Nil)) :: Nil))
+ val dbo = DBO("y" -> r)()
+ y(dbo) should equal(Right(r))
+ }
+ }
}
View
6 src/test/scala/writableSpec.scala
@@ -46,4 +46,10 @@ class writableSpec extends FunSpec with ShouldMatchers with MongoMatchers with R
unpack[Tuple2[String,Int]](Array("s", 10)) should equal(Some("s" -> 10))
}
}
+ describe("Writer for recursive structures") {
+ it("is possible") {
+ DBO("r" -> Rec(123, None))() should containField("r")
+ DBO("r" -> Rec(123, Some(Rec(234,None) :: Nil)))() should containField("r")
+ }
+ }
}

0 comments on commit 879a67a

Please sign in to comment.
Something went wrong with that request. Please try again.