title: Unmarshal map[string]DynamoDBAttributeValue into a struct
body: <p>I'm trying to set-up an AWS-lambda using <a href="https://github.com/aws/aws-sdk-go" rel="noreferrer">aws-sdk-go</a> that is triggered whenever a new <code>user</code> is added to a certain <a href="https://aws.amazon.com/dynamodb" rel="noreferrer">dynamodb</a> table.</p>
  
  <p>Everything is working just fine but I can't find a way to unmarshal a map <code>map[string]DynamoDBAttributeValue</code> like:</p>
  
  <pre><code>{
      "name": {
          "S" : "John"
      },
      "residence_address": {
          "M": {
              "address": {
                  "S": "some place"
              }
          }
      }
  }
  </code></pre>
  
  <p>To a given struct, for instance, a <code>User</code> struct. <a href="https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/dynamodbattribute/#example_Unmarshal" rel="noreferrer">Here</a> is shown an example of unsmarhaling a <code>map[string]*dynamodb.AttributeValue</code> into a given interface, but I can't find a way to do the same thing with <code>map[string]DynamoDBAttributeValue</code> even though these types seem to fit the same purposes.</p>
  
  <p><code>map[string]DynamoDBAttributeValue</code> is returned by a <code>events.DynamoDBEvents</code> from package <code>github.com/aws/aws-lambda-go/events</code>. This is my code:</p>
  
  <pre><code>package handler
  
  import (
      "context"
      "github.com/aws/aws-lambda-go/events"
      "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
      "github.com/aws/aws-sdk-go/service/dynamodb"
  )
  
  func HandleDynamoDBRequest(ctx context.Context, e events.DynamoDBEvent) {
  
      for _, record := range e.Records {
  
          if record.EventName == "INSERT" {
  
              // User Struct
              var dynamoUser model.DynamoDBUser
  
              // Of course this can't be done for incompatible types
              _ := dynamodbattribute.UnmarshalMap(record.Change.NewImage, &amp;dynamoUser)
          }
  
      }
  
  }
  </code></pre>
  
  <p>Of course, I can marshal <code>record.Change.NewImage</code> to JSON and unmarshal it back to a given struct, but then, I would have to manually initialize <code>dynamoUser</code> attributes starting from the latter ones.</p>
  
  <p>Or I could even write a function that parses <code>map[string]DynamoDBAttributeValue</code> to <code>map[string]*dynamodb.AttributeValue</code> like:</p>
  
  <pre><code>func getAttributeValueMapFromDynamoDBStreamRecord(e events.DynamoDBStreamRecord) map[string]*dynamodb.AttributeValue {
      image := e.NewImage
      m := make(map[string]*dynamodb.AttributeValue)
      for k, v := range image {
          if v.DataType() == events.DataTypeString {
              s := v.String()
              m[k] = &amp;dynamodb.AttributeValue{
                  S : &amp;s,
              }
          }
          if v.DataType() == events.DataTypeBoolean {
              b := v.Boolean()
              m[k] = &amp;dynamodb.AttributeValue{
                  BOOL : &amp;b,
              }
          }
          // . . .
          if v.DataType() == events.DataTypeMap {
              // ?
          }
      }
      return m
  }
  </code></pre>
  
  <p>And then simply use <code>dynamodbattribute.UnmarshalMap</code>, but on <code>events.DataTypeMap</code> it would be quite a tricky process.</p>
  
  <p><strong>Is there a way through which I can unmarshal a DynamoDB record coming from a <code>events.DynamoDBEvent</code> into a struct with a similar method shown for <code>map[string]*dynamodb.AttributeValue</code>?</strong></p>