1

I have upcoming shows for my band stored with DynamoDB, and I have the following code:

type PartiQLRunner struct {
    DynamoDbClient *dynamodb.DynamoDB
    TableName      string
}

func BuildRunner() *PartiQLRunner {
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    svc := dynamodb.New(sess)

    return &PartiQLRunner{
        DynamoDbClient: svc,
        TableName:      "SHOWS",
    }
}

type Show struct {
    PK      int    `dynamodbav:"PK"`
    DATE    string `dynamodbav:"DATE"`
    ADDRESS string `dynamodbav:"ADDRESS"`
    VENUE   string `dynamodbav:"VENUE"`
}

func (runner PartiQLRunner) GetShows() ([]Show, error) {
    var shows []Show
    response, err := runner.DynamoDbClient.ExecuteStatement(
        &dynamodb.ExecuteStatementInput{
            Statement: aws.String(fmt.Sprintf("SELECT * FROM \"%v\" WHERE PK = 1", runner.TableName)),
        })
    if err != nil {
        log.Printf("Couldn't get info. Here's why: %v\n", err)
    } else {
        err = attributevalue.UnmarshalListOfMaps(response.Items, &shows)
        if err != nil {
            log.Printf("Couldn't unmarshal response. Here's why: %v\n", err)
        }
    }
    return shows, err
}

However, I am getting the following error with the response.Items parameter in UnmarshalListOfMaps():

Cannot use 'response.Items' (type []map[string]*AttributeValue) as the type []map[string]types.AttributeValue

I am still somewhat new to Go syntax, and I'm not sure I'm understanding the mismatch between what is being passed in and what is expected. Any help would be greatly appreciated.

logo_positive
  • 15
  • 1
  • 6
  • Examples [here](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/example_dynamodb_ExecuteStatement_section.html) of PartiQL and the Go SDK. – jarmod Aug 22 '23 at 22:58

1 Answers1

0

It seems that there's a type mismatch between what the UnmarshalListOfMaps function expects and what you're providing. The error message indicates that the function is expecting an argument of type []map[string]types.AttributeValue, but response.Items is of type []map[string]*AttributeValue.

The solution is to convert response.Items to the correct type or use the correct type signature for your slice.

Here's how you can resolve this:

package main

import (
    "fmt"
    "log"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb/attributevalue"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

type Show struct {
    PK      int    `dynamodbav:"PK"`
    DATE    string `dynamodbav:"DATE"`
    ADDRESS string `dynamodbav:"ADDRESS"`
    VENUE   string `dynamodbav:"VENUE"`
}

type PartiQLRunner struct {
    DynamoDbClient *dynamodb.Client
    TableName      string
}

func (runner PartiQLRunner) GetShows() ([]*Show, error) {
    var shows []*Show

    response, err := runner.DynamoDbClient.ExecuteStatement(
        &dynamodb.ExecuteStatementInput{
            Statement: aws.String(fmt.Sprintf("SELECT * FROM \"%v\" WHERE PK = 1", runner.TableName)),
        })
    if err != nil {
        log.Printf("Couldn't get info. Here's why: %v\n", err)
        return shows, err
    }

    err = attributevalue.UnmarshalListOfMaps(response.Items, &shows)
    if err != nil {
        log.Printf("Couldn't unmarshal response. Here's why: %v\n", err)
        return shows, err
    }

    return shows, nil
}

// The main function is just for demonstration and may not be part of your code
func main() {
    // You will need to initialize your DynamoDbClient and TableName
    runner := PartiQLRunner{
        DynamoDbClient: /* initialize your DynamoDB client here */,
        TableName:      "YourTableName",
    }

    shows, err := runner.GetShows()
    if err != nil {
        log.Fatalf("Error retrieving shows: %v", err)
    }

    for _, show := range shows {
        fmt.Printf("Show Date: %s, Venue: %s, Address: %s\n", show.DATE, show.VENUE, show.ADDRESS)
    }
}

Please ensure you have the appropriate DynamoDB client initialization logic (which is commented out in the main function) to establish a connection to DynamoDB.

This provided code now correctly unmarshals the response into a slice of pointers to Show structs.

Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
  • I edited my PartiQLRunner struct to resemble the struct you suggested (I edited my post to include how I initialized it originally). However, I am getting an error: Unresolved type 'Client'. I've made sure my imports match yours as well. – logo_positive Aug 23 '23 at 04:55
  • What is your AWS SDK version – Piyush Patil Aug 23 '23 at 12:25
  • I misread my imports! I was not using v2. I refactored the code, and your solution works now, thank you! – logo_positive Aug 23 '23 at 15:24