aws-sdk-go-v2的attributevalue.Marshaler接口不起作用。

huangapple go评论135阅读模式
英文:

aws-sdk-go-v2 attributevalue.Marshaler interface not working

问题

我尝试实现aws-sdk-go-v2的attributevalue.Marshaler接口,但没有效果。始终在数据库中存储一个空对象。

我有以下结构体:

  1. type ID struct {
  2. value string
  3. }

我尝试了两个版本的marshaler:

  1. func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberM{
  3. Value: map[string]types.AttributeValue{
  4. "value": &types.AttributeValueMemberS{Value: id.value},
  5. },
  6. }, nil
  7. }

  1. func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberS{Value: id.value}, nil
  3. }

在这两种情况下,我可以通过调试器看到它进入了函数,并且返回的值看起来是正确的。但是在检查数据库后,id被存储为一个没有属性的对象。

英文:

I try to implement the aws-sdk-go-v2 attributevalue.Marshaler interface but it has no effect. An empty object is always stored on the db.

I have the following struct

  1. type ID struct {
  2. value string
  3. }

it tried both versions of the marshaler

  1. func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberM{
  3. Value: map[string]types.AttributeValue{
  4. "value": &types.AttributeValueMemberS{Value: id.value},
  5. },
  6. }, nil
  7. }

and

  1. func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberS{Value: id.value}, nil
  3. }

in both cases I can see with the debugger that it enters on the function and looks likes the returned value is correct. But after that when I check on the db the id is stored as an object without properties.

答案1

得分: 1

问题是 ID 上的链接无法工作:

这个不会工作:

  1. func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberS{Value: id.value}, nil
  3. }

但是去掉链接的相同代码可以工作:

  1. func (id ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberS{Value: id.value}, nil
  3. }
英文:

The problem is the link on the ID

that will not work:

  1. func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberS{Value: id.value}, nil
  3. }

but the same thing without link works

  1. func (id ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
  2. return &types.AttributeValueMemberS{Value: id.value}, nil
  3. }

huangapple
  • 本文由 发表于 2023年4月13日 18:40:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定