Skip to content

Commit

Permalink
Pickler[Int] * pickler
Browse files Browse the repository at this point in the history
  • Loading branch information
teigen committed May 9, 2012
1 parent a5bef2a commit 9a7328b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/byteme/Pickler.scala
Expand Up @@ -27,6 +27,9 @@ trait Pickler[A] { self =>

def * (times:Int) =
Pickler[List[A]](Parser(unpickle) * times, Output * (times, pickle))

def *[B](pickler: => Pickler[B])(implicit ev:A => Int, ev1:Int => A) =
Pickler[List[B]](Parser[Int](unpickle(_).map(ev)) >> (Parser(pickler.unpickle) *), l => pickle(l.size) ++ (Output * pickler.pickle)(l))

def wrap[B](w:A => B)(u:B => A) =
Pickler[B](unpickle(_).map(w), v => pickle(u(v)))
Expand Down
10 changes: 8 additions & 2 deletions src/test/scala/bson/BsonPicklers.scala
Expand Up @@ -7,7 +7,7 @@ import Literal.int

object BsonPicklers {

lazy val document = int32 lengthInclusive (elist <~ 0x00).wrap(Document)(Document.unapply(_).get)
lazy val document = int32 lengthInclusive (elist <~ 0x00).wrap(Document)(_.elements)

lazy val elist = element.*

Expand Down Expand Up @@ -72,7 +72,7 @@ object BsonPicklers {

val mDouble = Wrap[Double, MDouble](MDouble, _.value)
val mString = Wrap[String, MString](MString, _.value)
val mArray = Wrap[Document, MArray](BsonParsers.toArray, BsonOutput.toDoc)
val mArray = Wrap[Document, MArray](toArray, toDoc)
val mSymbol = Wrap[String, MSymbol](s => MSymbol(Symbol(s)), _.symbol.name)
val mCode = Wrap[String, MCode](MCode, _.code)
val mInt = Wrap[Int, MInt](MInt, _.value)
Expand All @@ -83,4 +83,10 @@ object BsonPicklers {
val mBoolean = Wrap[Boolean, MBoolean](MBoolean, _.value)
val mObjectId = collections.array[Byte] ^^ Wrap[Array[Byte], MObjectId](MObjectId, _.data)
val mRegexp = Wrap[String ~ String, MRegexp]({ case p ~ o => MRegexp(p, o)}, { case MRegexp(p, o) => new ~(p, o) })

def toArray(document:Document) =
MArray(document.elements.map{ case (name, value) => value })

def toDoc(m:MArray) =
Document(m.values.zipWithIndex.map{ case (v, i) => (i.toString, v) })
}

0 comments on commit 9a7328b

Please sign in to comment.