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

Add utility for unmarshalling dynamodb Query Items. #696

Closed
sanathp opened this issue May 23, 2016 · 8 comments
Closed

Add utility for unmarshalling dynamodb Query Items. #696

sanathp opened this issue May 23, 2016 · 8 comments
Assignees
Labels
feature-request A feature should be added or improved.

Comments

@sanathp
Copy link

sanathp commented May 23, 2016

Query returns items in the below format

Items []map[string]*AttributeValue

I cannot see any UnMarshal function which supports the datatype []map[string]*AttributeValue

Should I UnMarshal each element in a for loop ?? whats the best way to do this?

@jasdel jasdel added the guidance Question that needs advice or information. label May 24, 2016
@jasdel jasdel self-assigned this May 24, 2016
@jasdel
Copy link
Contributor

jasdel commented May 24, 2016

Hi @sanathp thanks for contacting us. You are correct the SDK does not currently provide an utility method to unmarshal a slice of map[string]*dynamodb.AttributeValue. The best way to unmarshal this is to iterate through the returned list calling dynamodbattributevalue.UnmarshalMap() for each element in the slice.

@jasdel
Copy link
Contributor

jasdel commented May 24, 2016

Marking this as a Feature Request to make receiving items from query API operations easier to unmarshal into Go value types. We're also always glad to review PR if you're looking to contribute this feature to the SDK.

@jasdel jasdel added the feature-request A feature should be added or improved. label May 24, 2016
@jasdel jasdel changed the title How to UnMarshal DynamoDb Query output ? Add utility for unmarshalling dynamodb Query Items. May 24, 2016
@jasdel jasdel removed the guidance Question that needs advice or information. label Jun 6, 2016
@twoism
Copy link

twoism commented Jul 28, 2016

I ran into this same issue. It seems strange to not have a native way to unmarshal the return types of both QueryOutput.Items and ScanOutput.Items. This was my solution for now but it would be nice to not have to iterate over the slice in both this code and UnmarshalList.

func UnmarshalListOfMaps(l []map[string]*dynamodb.AttributeValue, out interface{}) error {
    attrs := make([]*dynamodb.AttributeValue, len(l))

    for i, m := range l {
        attrs[i] = &dynamodb.AttributeValue{M: m}
    }

    return dynamodbattribute.UnmarshalList(attrs, out)
}

@twoism
Copy link

twoism commented Jul 29, 2016

Not to harp on this but asking end users to incur N*2 marshaling time for a common return type doesn't seem like the answer. I did some digging and given that the above code is essentially just upcasting each item in []map[string]*dynamodb.AttributeValue into an AttributeValue, couldn't QueryOutput.Items just return a []*dynamodb.AttributeValue where each item has the value type of M? Unless I am wrong, these are essentially the same thing. If so, the marshaler would not need to be updated. It would break the API but I think using AWS types would make things easier. Just a thought and I could be missing something about why the map is necessary, happy to help work on this if needed :)

@jasdel
Copy link
Contributor

jasdel commented Aug 3, 2016

Thanks for the feedback @twoism. I'll forward your feedback along to the DynamoDB team to help improve the API. We wouldn't be able to change the API due to the breaking change without the service doing a major version bump.

To resolve this feature request I think would solve it similar to the example you posted, same for encoding. The SDK already does this to a more limited degree for marshal of list and maps. So updating to unmarshal a list of maps is pretty straightforward, but like you mentioned has more complexity.

I'm not sure yet how the marshaler side of this would work yet though. Not sure its even needed though, but may make sense for parity.

jasdel added a commit to jasdel/aws-sdk-go that referenced this issue Oct 18, 2016
Adds support for unmarshaling a list of maps. This is useful for
unmarshaling the DynamoDB AttributeValue list of maps returned by APIs
like Query and Scan.

Example can be found in example/service/dynamodb/scanItems.

Fix aws#810, aws#696
jasdel added a commit that referenced this issue Oct 18, 2016
Adds support for unmarshalling a list of maps. This is useful for
unmarshalling the DynamoDB AttributeValue list of maps returned by APIs
like Query and Scan.

Example can be found in example/service/dynamodb/scanItems.

Fix #810, #696
@jasdel
Copy link
Contributor

jasdel commented Oct 18, 2016

Thanks for bringing up this issue. Change #897 Fixes this issue by adding a UnmarshalListOfMaps utility to the dynamodbattribute package. This will make using Scan and Query easier since you'll be able to unmarshal Items now without extra logic.

@jasdel jasdel closed this as completed Oct 18, 2016
@twoism
Copy link

twoism commented Oct 18, 2016

This is awesome, thanks a ton!

@timogoosen
Copy link

I know this is an old issue but could someone explain the usage ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

4 participants