Get custom file option Value from descriptor file (Java)

55 views
Skip to first unread message

Moustafa Nassar

unread,
Jul 22, 2020, 4:41:33 PM7/22/20
to Protocol Buffers
The file I have is:

test.proto

syntax = "proto3"
import "google/protobuf/descriptor.proto"
extend google.protobuf.FileOptions{
    string myOption = 50000;
}
option (myOption) = "foo";

I create a descriptor file by using: protoc --include_imports --descriptor_set_out="test.desc" test.proto

I then get the FileDescriptor from test.desc and do System.out.println(FileDescriptor.getOptions()). It prints out 50000: "foo" but I don't know how to get that in a string or something that I can work with.

Nadav Samet

unread,
Jul 22, 2020, 4:54:56 PM7/22/20
to Moustafa Nassar, Protocol Buffers
Hi Moustafa,

To parse the FileDescriptor:
1. create an ExtensionRegistry using ExtensionRegistry.newInstance, 
2. Use YourProtoFile.registerAllExtensions(registry)  (look for that generated method)
3. Parse using parseFrom(data, registry)
4. Access using result.getExtension(TestProto.myOption)  // look for the exact location in the generated code.



--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/04cb2072-ce76-450e-a9fc-601de703b683n%40googlegroups.com.


--
-Nadav

Moustafa Nassar

unread,
Jul 22, 2020, 6:21:44 PM7/22/20
to Protocol Buffers
So the issue I am having is that I am parsing the descriptor files at runtime, I don't have access to the proto class therefore I can't call YourProtoFile.registerAllExtensions. Here is how I am reading the descriptor file and getting the FileDescriptor:

// Open test.desc and get the FileDescriptorSet
FileInputStream fin = new FileInputStream("/path/to/test.desc");
FileDescriptorSet set = FileDescriptorSet.parseFrom(fin);

// This loop handles my imports
List<FileDescriptor> fileDescriptors = new ArrayList<>();
for(int i=0; i<set.getFileCount;i++){
    fileDescriptors.add(FileDescriptor.buildFrom(set.getFile(i), new FileDescriptor[] {})));
}

// Get the FileDescriptor
FileDescriptor descriptor = FileDescriptor.buildFrom(set.getFile(set.getFileCount()-1), fileDescriptors.toArray(new FileDescriptor[0]));

Nadav Samet

unread,
Jul 22, 2020, 6:29:24 PM7/22/20
to Moustafa Nassar, Protocol Buffers
In that case, I believe you should be able to find the data in the UnknownFieldSet:
fileDescriptor.getOptions().getUnknownFields().getField(50000).getLengthDelimitedList()

That should give you a list with your string represented as a ByteString (UTF-8 encoded).




--
-Nadav

Moustafa Nassar

unread,
Jul 22, 2020, 6:59:33 PM7/22/20
to Protocol Buffers
Thank you this should work for my purposes, I think the first approach you presented to me is preferred if possible. There seems to be a solution here:  https://groups.google.com/g/protobuf/c/zKYLsr9xE90  I am not skilled enough in protobuf to understand what the solution was though... 
Reply all
Reply to author
Forward
0 new messages