Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Last active August 29, 2015 14:01
Show Gist options
  • Save DeaconDesperado/9123509cabb72ce51a85 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/9123509cabb72ce51a85 to your computer and use it in GitHub Desktop.
Reactive Mongo Serializer issue
package com.gn.gncore.models
import java.util.UUID
import org.joda.time.DateTime
import org.joda.time.LocalTime
/** Core place model throughout service
*/
case class Place(
id:String,
name:String,
street:String,
city:String,
state:String,
zip:String,
phone:String,
location:List[Double],
description:String,
business_type:String,
created:DateTime,
hours:Option[Hours] = None,
links:Map[String, String] = Map(),
hidden:Boolean = false
)
/** Hours property for places. Every day has an optional list
of TimeBlock instances
*/
case class Hours(
monday:Option[List[TimeBlock]] = None,
tuesday:Option[List[TimeBlock]] = None,
wednesday:Option[List[TimeBlock]] = None,
thursday:Option[List[TimeBlock]] = None,
friday:Option[List[TimeBlock]] = None,
saturday:Option[List[TimeBlock]] = None,
sunday:Option[List[TimeBlock]] = None
)
/** A TimeBlock represents a discrete opening and closing time
*/
case class TimeBlock(
opens:LocalTime,
closes:LocalTime,
label:Option[String] = None
)
package com.gn.gncore.models.serializers.bson
import com.gn.gncore.models.{Place, TimeBlock, Hours, placeFixture}
import org.joda.time.DateTime
import org.joda.time.LocalTime
import org.joda.time.format.DateTimeFormat
import reactivemongo.bson._
import reactivemongo.api.MongoDriver
import com.gn.gncore.GncoreConfig
object BSONPlaceSerializer extends App{
implicit object MapReader extends BSONReader[BSONDocument,Map[String, String]]{
def read(bson: BSONDocument): Map[String, String] = {
val elements = bson.elements.map { tuple =>
tuple._1 -> tuple._2.seeAsTry[String].get
}
elements.toMap[String, String]
}
}
private val timePattern = DateTimeFormat.forPattern("HH:mm:ss")
implicit object TimeBlockBSONReader extends BSONDocumentReader[TimeBlock] {
def read(doc: BSONDocument):TimeBlock = {
val opensString = doc.getAs[String]("opens").get
val closesString = doc.getAs[String]("closes").get
val label = doc.getAs[String]("label")
val opens = LocalTime.parse(opensString, timePattern)
val closes = LocalTime.parse(closesString, timePattern)
TimeBlock(opens,closes,label)
}
}
implicit object HoursBSONReader extends BSONDocumentReader[Hours] {
def read(doc: BSONDocument):Hours = {
Hours(
doc.getAs[List[TimeBlock]]("monday"),
doc.getAs[List[TimeBlock]]("tuesday"),
doc.getAs[List[TimeBlock]]("wednesday"),
doc.getAs[List[TimeBlock]]("thursday"),
doc.getAs[List[TimeBlock]]("friday"),
doc.getAs[List[TimeBlock]]("saturday"),
doc.getAs[List[TimeBlock]]("sunday")
)
}
}
implicit object PlaceBSONReader extends BSONDocumentReader[Place] {
def read(doc: BSONDocument) :Place = {
val dtString = doc.getAs[String]("created").get
val created = new DateTime(dtString)
val linksBSON = doc.getAs[BSONDocument]("links").get
Place(
doc.getAs[String]("id").get,
doc.getAs[String]("name").get,
doc.getAs[String]("street").get,
doc.getAs[String]("city").get,
doc.getAs[String]("state").get,
doc.getAs[String]("zip").get,
doc.getAs[String]("phone").get,
doc.getAs[List[Double]]("location").get,
doc.getAs[String]("description").get,
doc.getAs[String]("business_type").get,
created,
doc.getAs[Hours]("hours"),
linksBSON.as[Map[String, String]],
doc.getAs[Boolean]("hidden").get
)
}
}
implicit object TimeBlockBSONWriter extends BSONDocumentWriter[TimeBlock] {
def write(block:TimeBlock): BSONDocument = {
BSONDocument(
"opens" -> block.opens.toString(timePattern),
"closes" -> block.closes.toString(timePattern),
"label" -> block.label
)
}
}
implicit object HoursBSONWriter extends BSONDocumentWriter[Hours] {
def write(hours:Hours): BSONDocument = {
BSONDocument(
"monday" -> hours.monday,
"tuesday" -> hours.tuesday,
"wednesday" -> hours.wednesday,
"thursday" -> hours.thursday,
"friday" -> hours.friday,
"saturday" -> hours.saturday,
"sunday" -> hours.sunday
)
}
}
implicit object PlaceBSONWriter extends BSONDocumentWriter[Place] {
def write(place:Place): BSONDocument = {
val links = BSONDocument(place.links.map { tuple =>
tuple._1 -> BSONString(tuple._2)
})
BSONDocument(
"id" -> place.id,
"name" -> place.name,
"street" -> place.street,
"city" -> place.city,
"state" -> place.state,
"zip" -> place.zip,
"phone" -> place.phone,
"location" -> place.location,
"description" -> place.description,
"business_type" -> place.business_type,
"created" -> place.created.toString(),
"hours" -> place.hours,
"links" -> links,
"hidden" -> place.hidden
)
}
}
import scala.concurrent.ExecutionContext.Implicits.global
val driver = new MongoDriver
val connection = driver.connection(List("localhost"))
val placeDoc = BSON.write(placeFixture)
val database = connection("mydatabase")
val collection = database("acollection")
println(collection)
collection.insert(placeDoc)
val places = collection.find(BSONDocument("name" -> "Marks Bar")).
cursor[Place].
collect[List]().map { places =>
for(place <- places){
println(place)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment