首页 >后端开发 >Golang >如何使用 AWS sdk 在 Go 中正确解组 PartiQL 查询的结果?

如何使用 AWS sdk 在 Go 中正确解组 PartiQL 查询的结果?

WBOY
WBOY转载
2024-02-12 16:12:12534浏览

如何使用 AWS sdk 在 Go 中正确解组 PartiQL 查询的结果?

问题内容

我的乐队即将演出的节目存储在 DynamoDB 中,并且我有以下代码:

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
}

但是,我在 UnmarshalListOfMaps() 中的 response.Items 参数中收到以下错误:

无法使用“response.Items”(类型[]map[string]*AttributeValue)作为类型[]map[string]types.AttributeValue

我对 Go 语法仍然有些陌生,我不确定我是否理解传入内容与预期内容之间的不匹配。任何帮助将不胜感激。

解决方法

似乎 UnmarshalListOfMaps 函数期望的内容与您提供的内容之间存在类型不匹配。该错误消息表明该函数需要 []map[string]types.AttributeValue 类型的参数,但 response.Items 的类型为 []map[string]*AttributeValue

解决方案是将 response.Items 转换为正确的类型或为您的切片使用正确的类型签名。

解决此问题的方法如下:

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)
    }
}

请确保您具有适当的 DynamoDB 客户端初始化逻辑(在 main 函数中注释掉)来建立与 DynamoDB 的连接。

所提供的代码现在可以正确地将响应解组为指向 Show 结构的指针片段。

以上是如何使用 AWS sdk 在 Go 中正确解组 PartiQL 查询的结果?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除