gpt4 book ai didi

amazon-web-services - 做 dynamoattribute.UnmarshalMap 时关系总是为零

转载 作者:行者123 更新时间:2023-12-01 21:14:28 25 4
gpt4 key购买 nike

我有两个结构。 sample 和测试。 “样本”具有“测试”类型的关系。当我尝试做'dynamoattribute.UnmarshalMap'时,关系总是为零。您能否建议如何填充关系(在这种情况下为“测试”)?

package main

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

type Sample struct {
SampleId string `jsonapi:"attr,sampleId,omitempty" dynamodbav:"sample_id"`
Test *Test `jsonapi:"relation,test"`
}

type Test struct {
TestId string `jsonapi:"attr,testId,omitempty" dynamodbav:"test_id"`
}

func main() {
var m map[string]*dynamodb.AttributeValue
m = make(map[string]*dynamodb.AttributeValue)

m["sample_id"] = &dynamodb.AttributeValue{
S: aws.String("sample1"),
}

m["test_id"] = &dynamodb.AttributeValue{
S: aws.String("test"),
}

sam := Sample{}

err := dynamodbattribute.UnmarshalMap(m, &sam)
if err != nil {
fmt.Println(err)
}

fmt.Println(sam)
}

enter image description here

最佳答案

package main

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

type Sample struct {
SampleId string `jsonapi:"attr,sampleId,omitempty" dynamodbav:"sample_id"`
Test *Test `jsonapi:"relation,test"`
}

type Test struct {
TestId string `jsonapi:"primary,testId" dynamodbav:"test_id"`
}


func main() {
var m map[string]*dynamodb.AttributeValue
m = make(map[string]*dynamodb.AttributeValue)

m["sample_id"] = &dynamodb.AttributeValue{
S: aws.String("sample1"),
}

var mTest map[string]*dynamodb.AttributeValue
mTest = make(map[string]*dynamodb.AttributeValue)

mTest["test_id"] = &dynamodb.AttributeValue{
S: aws.String("test1"),
}

m["test"] = &dynamodb.AttributeValue{
M: mTest,
}

sam := Sample{}

err := dynamodbattribute.UnmarshalMap(m, &sam)
if err != nil {
fmt.Println(err)
}

fmt.Println(sam)
}

关于amazon-web-services - 做 dynamoattribute.UnmarshalMap 时关系总是为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60282995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com