Protobuf消息未实现protoreflect.ProtoMessage(ProtoReflect方法具有指针接收器)

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

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

huangapple
  • 本文由 发表于 2023年1月3日 18:29:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/74992083.html
匿名

发表评论

匿名网友

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

确定