6

I am currently working on Google Protocol Buffers and need to generate dynamic messages. I already have my .proto files defined as shown below.

message TSInbound
{
  string id = 1;
  map<string,string> state = 2;
  map<string,string> reading =3;
}

AFAIK,I can use File Descriptor Set to create dynamic messsages. But, that would involve using the compiler to generate the desc files. I would like to generate Descriptor without compiling the .proto files. Is there a way for dynamic creation of message using custom defined .proto files and not using protoc ?

Ankita
  • 2,798
  • 4
  • 18
  • 25

2 Answers2

7

I think you are asking: "Is there a way to dynamically parse text .proto files at runtime to get descriptors."

The .proto parser is written in C++. It is available as the library libprotoc.so. You could, in theory, write a JNI wrapper around this to do parsing at runtime. However, there is no (official) pure-Java parser for .proto files.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
0

I just found a solution from confluent, more precisely 'io.confluent:kafka-protobuf-serializer', which enables this in a very easy way

    ProtobufSchema schema = new ProtobufSchema(
            Files.readString(
                    Path.of("/anyPath/test.proto")
            )
    );
    Descriptor descriptor = schema.toDescriptor();
    Message deserializedMessage = DynamicMessage.parseFrom(descriptor, messageByteArr);
Dominik21
  • 178
  • 1
  • 8