Created
August 6, 2014 16:55
-
-
Save dbalduini/92cc0e9195d61c3031eb to your computer and use it in GitHub Desktop.
Some Default formats for ReactDynamo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.react2.reactdynamo._ | |
import io.react2.reactdynamo.exceptions.ReadsException | |
import org.joda.time.{Period, DateTime} | |
import org.joda.time.format.ISODateTimeFormat | |
trait MyDefaultFormats { | |
val fmt = ISODateTimeFormat.dateTime | |
implicit object DateTimeWrites extends Writes[DateTime] { | |
def writes(dt: DateTime): AttributeValue = new AttributeValue().withS(fmt print dt) | |
} | |
implicit object DateTimeReads extends Reads[DateTime] { | |
def reads(attv: AttributeValue): DateTime = | |
Option(attv.getS).map(fmt.parseDateTime).getOrElse(throw ReadsException(attv.toString)) | |
} | |
implicit object PeriodWrites extends Writes[Period] { | |
def writes(p: Period): AttributeValue = new AttributeValue().withS(p.toString) | |
} | |
implicit object PeriodReads extends Reads[Period] { | |
def reads(attv: AttributeValue): Period = | |
Option(attv.getS).map(Period.parse).getOrElse(throw ReadsException(attv.toString)) | |
} | |
implicit object EnumWrites extends Writes[Enumeration#Value] { | |
def writes(enum: Enumeration#Value): AttributeValue = new AttributeValue().withS(enum.toString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment