AWScala icon indicating copy to clipboard operation
AWScala copied to clipboard

dynamodbv2.Item doesn't support getting attributes by key

Open mcamou opened this issue 11 years ago • 1 comments

All DynamoDB get operations (get, query, scan, etc...) return Item objects. While the AWS API returns a Map, the Item object turns it into a simple Seq(name, AttributeValue). That means that to get the value of a specific attribute (which I assume is a common case you have to either turn it back into a Map or iterate. It would be much more useful to retain the attribute names and be able to do, for example, item("attribute").

mcamou avatar Feb 24 '14 17:02 mcamou

Here's my workaround for that:

import awscala._, dynamodbv2._

class EnhancedDynamoItem(item: Item) {

  def attributesMap: Map[String, AttributeValue] =
    item.attributes
      .foldLeft(Map.empty[String, AttributeValue])((m, a) =>
        m ++ Map(a.name -> a.value))
}

object ToEnhancedDynamoOps {

  implicit def toEnhancedItem(item: Item): EnhancedDynamoItem =
    new EnhancedDynamoItem(item)

}

beezee avatar May 15 '15 17:05 beezee