Is it possible to parse protobuf binary using descriptorpb.DescriptorProto

Viewed 81

The use case is parsing idls in service A and parsing protobuf messages to json in service B.

  1. Using jhump.protoreflect to parse idl and get desc.MessageDescriptor, then use protojson to marshal descriptorpb.DescriptorProto

    h, _ := protojson.Marshal(md.Descriptor.AsDescriptorProto())

  2. Using protojson to unmarshal h and get DescriptorProto

    var z descriptorpb.ç if err := protojson.Unmarshal(h, &z); err != nil { fmt.Println(err.Error()) }

  3. How to parse message by using DescriptorProto?

If I'm doing the whole work in the same service, I'm able to parse the message by using MessageDescriptor

dm := dynamic.NewMessage(md)
dm.Unmarshal(body)
dm.MarshalJSONPB(&jsonpb.Marshaler{OrigName: true, EnumsAsInts: true})

but the issue is parsing message is done in a different service, so I'm looking to keep some "message descriptor" in database and can be used by service B.

Any suggestion?

0 Answers
Related