Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a Try[JsObject] for fromDynamoJson (reads) #1

Merged
merged 1 commit into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.lifeway.play.dynamo

import play.api.libs.json._

import scala.collection.GenTraversable
import scala.util.Try

object DynamoJsonConverters {

Expand Down Expand Up @@ -47,8 +49,12 @@ object DynamoJsonConverters {
* Given a JsObject that was stored in DynamoDB, this will turn that Dynamo json into a standard JsObject will all of
* the dynamo types removed. Typically, you would use this to transform the Dynamo response back to a JsValue type
* that you could then pass to Play's standard Json writes method.
*
* Note that this returns a Try[JsObject], because if a non dynamoDB json object is passed to it (which we can't
* prevent), or if a currently unsupported DynamoDB type was in the object (i.e. Binary or BinarySet), then we
* would throw a match exception.
*/
def fromDynamoJson: JsObject = {
def fromDynamoJson: Try[JsObject] = {
def objectConversion(i: JsObject): JsObject =
JsObject(i.fields.flatMap {
case (k, JsObject(wrappedObj)) =>
Expand Down Expand Up @@ -81,7 +87,7 @@ object DynamoJsonConverters {
}
})

objectConversion(input)
Try(objectConversion(input))
}
}
}
21 changes: 19 additions & 2 deletions src/test/scala/com/lifeway/play/dynamo/JsonToFromDynamoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.lifeway.play.dynamo
import org.scalatest.{MustMatchers, WordSpec}
import play.api.libs.json.{JsObject, Json}

import scala.util.Try

class JsonToFromDynamoSpec extends WordSpec with MustMatchers {

case class NestedObject(title: String, age: Byte)
Expand Down Expand Up @@ -139,9 +141,24 @@ class JsonToFromDynamoSpec extends WordSpec with MustMatchers {
"read from DynamoDB Json through direct Json Converters to standard case class readers" in {
import DynamoJsonConverters.Converters

println(sampleJson.as[JsObject].fromDynamoJson)
val result: Try[TestSample] = sampleJson.as[JsObject].fromDynamoJson.map(_.as[TestSample])
result.isSuccess mustEqual true
result.get mustEqual sampleVal
}

"read from an invalid DynamoDB JSON through direct Json Converters should return an failed Try in the event of non-dynamo Json" in {
import DynamoJsonConverters.Converters

val json = Json.parse(
"""
|{
| "key": "This is normal Json",
| "otherThing": true
|}
""".stripMargin)

sampleJson.as[JsObject].fromDynamoJson.as[TestSample] mustEqual sampleVal
val result: Try[TestSample] = json.as[JsObject].fromDynamoJson.map(_.as[TestSample])
result.isSuccess mustEqual false
}

"write to DynamoDB Json through direct Json converters that occur after the standard case class Json writers" in {
Expand Down