英文:
Protobuf message does not implement protoreflect.ProtoMessage (ProtoReflect method has pointer receiver)
问题
我有一个导入了"google/protobuf/any.proto"
的Protobuf消息:
message MintRecord {
...
google.protobuf.Any data = 11;
...
}
我试图在data
字段中使用anypb序列化另一个protobuf:
data, err := anypb.New(protobuf.LootcratePrize{
Items: &protobuf.Inventory{Items: items},
Roll: fmt.Sprintf("%f", roll),
})
if err != nil {
log.Println("[Lootbox] Err: error marshalling lootcrate prize data into mintRec", err)
} else {
mintRecordProto.Data = data
}
在编译时,我得到以下错误:
cannot use protobuf.LootcratePrize{...} (value of type protobuf.LootcratePrize) as type protoreflect.ProtoMessage in argument to anypb.New:
protobuf.LootcratePrize does not implement protoreflect.ProtoMessage (ProtoReflect method has pointer receiver)
根据文档,我在这里没有做任何特殊的操作。我该如何解决这个问题?
这是我试图序列化并存储在data
字段中的protobuf:
Lootcrate.proto:
syntax = "proto3";
package protobuf;
option go_package = "protobuf/";
import "protobuf/inventory.proto";
import "protobuf/flowerdbservice.proto";
message LootcratePrize {
Inventory items = 1;
repeated NFT flowers = 2;
string roll = 3;
}
英文:
I have a Protobuf message that imports "google/protobuf/any.proto"
:
message MintRecord {
...
google.protobuf.Any data = 11;
...
}
And I am trying to serialize another protobuf inside of the data
field using anypb:
data, err := anypb.New(protobuf.LootcratePrize{
Items: &protobuf.Inventory{Items: items},
Roll: fmt.Sprintf("%f", roll),
})
if err != nil {
log.Println("[Lootbox] Err: error marshalling lootcrate prize data into mintRec", err)
} else {
mintRecordProto.Data = data
}
Upon compilation I get the following error:
cannot use protobuf.LootcratePrize{…} (value of type protobuf.LootcratePrize) as type protoreflect.ProtoMessage in argument to anypb.New:
protobuf.LootcratePrize does not implement protoreflect.ProtoMessage (ProtoReflect method has pointer receiver)
According to the docs I am doing nothing out of the ordinary here. How do I resolve this issue?
This is the protobuf I am attempting to serialize and store inside of the data
field:
Lootcrate.proto:
syntax = "proto3";
package protobuf;
option go_package = "protobuf/";
import "protobuf/inventory.proto";
import "protobuf/flowerdbservice.proto";
message LootcratePrize {
Inventory items = 1;
repeated NFT flowers = 2;
string roll = 3;
}
答案1
得分: 3
Sarath Sadasivan Pillai 是正确的。
将你的代码改为:
data, err := anypb.New(&protobuf.LootcratePrize{
Items: &protobuf.Inventory{Items: items},
Roll: fmt.Sprintf("%f", roll),
})
英文:
Sarath Sadasivan Pillai is correct.
Change your code to :
data, err := anypb.New(&protobuf.LootcratePrize{
Items: &protobuf.Inventory{Items: items},
Roll: fmt.Sprintf("%f", roll),
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论