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

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

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

问题

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

我有以下结构体:

type ID struct {
	value string
}

我尝试了两个版本的marshaler:

func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
	return &types.AttributeValueMemberM{
		Value: map[string]types.AttributeValue{
			"value": &types.AttributeValueMemberS{Value: id.value},
		},
	}, nil
}

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

在这两种情况下,我可以通过调试器看到它进入了函数,并且返回的值看起来是正确的。但是在检查数据库后,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

type ID struct {
	value string
}

it tried both versions of the marshaler

func (id *ID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
	return &types.AttributeValueMemberM{
		Value: map[string]types.AttributeValue{
			"value": &types.AttributeValueMemberS{Value: id.value},
		},
	}, nil
}

and

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

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 上的链接无法工作:

这个不会工作:

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

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

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

The problem is the link on the ID

that will not work:

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

but the same thing without link works

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

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:

确定