Home > Mobile >  Protobuf message does not implement protoreflect.ProtoMessage (ProtoReflect method has pointer recei
Protobuf message does not implement protoreflect.ProtoMessage (ProtoReflect method has pointer recei

Time:01-04

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;
}

CodePudding user response:

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