Last active
November 8, 2017 06:19
-
-
Save thsutton/fedee32fbc9e7ce0daa42ece48916c89 to your computer and use it in GitHub Desktop.
Reduce some duplication (by more duplication)
This file contains 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
diff --git a/dynamodb/src/main/scala/io/atlassian/aws/dynamodb/DynamoDB.scala b/dynamodb/src/main/scala/io/atlassian/aws/dynamodb/DynamoDB.scala | |
index e87be02..8331411 100644 | |
--- a/dynamodb/src/main/scala/io/atlassian/aws/dynamodb/DynamoDB.scala | |
+++ b/dynamodb/src/main/scala/io/atlassian/aws/dynamodb/DynamoDB.scala | |
@@ -120,19 +120,7 @@ object DynamoDB { | |
def query[PK, V](ck: Column[PK], cv: Column[V])(q: QueryImpl): DynamoDBAction[Page[PK, V]] = | |
DynamoDBAction.withClient { | |
_.query(q.asQueryRequest) | |
- }.flatMap { res => | |
- DynamoDBAction.attempt { | |
- res.getItems.asScala.toList.traverse[Attempt, V] { | |
- cv.unmarshall.unmarshall | |
- }.map { vs => | |
- Page(vs, | |
- Option(res.getLastEvaluatedKey).flatMap { | |
- lastKey => ck.unmarshall(lastKey.asScala.toMap).toOption | |
- } | |
- ) | |
- } | |
- } | |
- } | |
+ }.flatMap( _.decodeAsPage(ck, cv) ) | |
/** | |
* Scan a table, with pagination. | |
@@ -143,19 +131,7 @@ object DynamoDB { | |
def scan[PK, V](ck: Column[PK], cv: Column[V])(q: ScanImpl): DynamoDBAction[Page[PK, V]] = | |
DynamoDBAction.withClient { | |
_.scan(q.asScanRequest) | |
- }.flatMap { res => | |
- DynamoDBAction.attempt { | |
- res.getItems.asScala.toList.traverse[Attempt, V] { v => | |
- cv.unmarshall.unmarshall(v) | |
- }.map { vs => | |
- Page(vs, | |
- Option(res.getLastEvaluatedKey).flatMap { | |
- lastKey => ck.unmarshall(lastKey.asScala.toMap).toOption | |
- } | |
- ) | |
- } | |
- } | |
- } | |
+ }.flatMap( _.decodeAsPage(ck, cv) ) | |
def tableExists(tableName: String): DynamoDBAction[Boolean] = | |
withClient { client => | |
@@ -259,6 +235,28 @@ object DynamoDB { | |
_.deleteTable(Table.name) | |
} | |
+ /** Describes the bit of the ScanResult and QueryResult classes which we use to read the results. | |
+ */ | |
+ private type ReadOperationResult = { | |
+ def getItems(): java.util.List[java.util.Map[java.lang.String, AttributeValue]] | |
+ def getLastEvaluatedKey(): java.util.Map[java.lang.String, AttributeValue] | |
+ } | |
+ | |
+ private implicit class ResultWithItems[Result <: ReadOperationResult](result: Result) { | |
+ def decodeAsPage[PK, V](ck: Column[PK], cv: Column[V]): DynamoDBAction[Page[PK, V]] = | |
+ DynamoDBAction.attempt { | |
+ result.getItems().asScala.toList.traverse[Attempt, V] { | |
+ cv.unmarshall.unmarshall | |
+ }.map { vs => | |
+ Page(vs, | |
+ Option(result.getLastEvaluatedKey())flatMap { | |
+ lastKey => ck.unmarshall(lastKey.asScala.toMap).toOption | |
+ } | |
+ ) | |
+ } | |
+ } | |
+ } | |
+ | |
sealed trait ReadConsistency | |
object ReadConsistency { | |
case object Strong extends ReadConsistency |
Hmm. Except for the reflection I don't mind this new version.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Deletes 26 lines, adds 26 lines.