Skip to content

Instantly share code, notes, and snippets.

@stephanos
Created March 21, 2011 12:47
Show Gist options
  • Save stephanos/879398 to your computer and use it in GitHub Desktop.
Save stephanos/879398 to your computer and use it in GitHub Desktop.
private def shrinkDBO(dbo: DBObject): DBObject = {
def isDBO(v: AnyRef) = v.isInstanceOf[DBObject]
def isEmptyDBO(v: AnyRef) = isDBO(v) && v.asInstanceOf[DBObject].isEmpty
def shrink(v: AnyRef) = if (isDBO(v)) shrinkDBO(v.asInstanceOf[DBObject]) else v
dbo match {
case _: BasicDBList =>
val ls = new BasicDBList()
(dbo.mapValues(shrink(_)).filter(!isEmptyDBO(_)).values).foreach(ls.add(_))
ls
case _ =>
dbo.filter(e => e._1.length <= 3) // don't allow keys with more than 3 characters
.mapValues(v => shrink(v)) // recursively
.filter(e => !isEmptyDBO(e._2)) // finally: purge empty lists
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment