how to know whether field has options while converting proto to other language

18 views
Skip to first unread message

LakshmiKanth Gupta

unread,
Dec 5, 2019, 3:26:15 PM12/5/19
to Protocol Buffers
Hi,

Currently I am writing a convertor from proto format to ttcn3 format.

Messages which I have defined has some custom Field options. How can I check whether a field has custom options or not.


syntax="proto3";
import "google/protobuf/descriptor.proto"
extend google.protobuf.FieldOptions{
   bool presence=50001;
}

message Mymessage
{
    int32 foo=1[(presence)=true];
    string bar=2;
}

This is my file. I would like to know when my convertor is invoked using protoc whether a field has options defined or not.

Thanks in advance,

with best regards,
  Gupta

Adam Cozzette

unread,
Dec 5, 2019, 5:43:37 PM12/5/19
to LakshmiKanth Gupta, Protocol Buffers
I assume your converter is set up as a protoc plugin? In that case you can examine the descriptor for each field to see if it has a custom option defined or not. The details depend on the language you are using, though.

--
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/87e492fa-3535-4ee3-b391-f1bb6362f9ad%40googlegroups.com.

LakshmiKanth Gupta

unread,
Dec 5, 2019, 11:01:49 PM12/5/19
to Protocol Buffers
Yes.I am developing protoc plugin using c++.But I didn't find any function to do that. Could somebody help me with that. Also couldn't find the information of the options.

I am using following code:

FieldOptions l_options= m_record->field(i)->options();

From here I couldn't figure out how I can look at these options.




On Thursday, December 5, 2019 at 11:43:37 PM UTC+1, Adam Cozzette wrote:
I assume your converter is set up as a protoc plugin? In that case you can examine the descriptor for each field to see if it has a custom option defined or not. The details depend on the language you are using, though.

On Thu, Dec 5, 2019 at 12:26 PM LakshmiKanth Gupta <klkv...@gmail.com> wrote:
Hi,

Currently I am writing a convertor from proto format to ttcn3 format.

Messages which I have defined has some custom Field options. How can I check whether a field has custom options or not.


syntax="proto3";
import "google/protobuf/descriptor.proto"
extend google.protobuf.FieldOptions{
   bool presence=50001;
}

message Mymessage
{
    int32 foo=1[(presence)=true];
    string bar=2;
}

This is my file. I would like to know when my convertor is invoked using protoc whether a field has options defined or not.

Thanks in advance,

with best regards,
  Gupta

--
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 prot...@googlegroups.com.

LakshmiKanth Gupta

unread,
Dec 6, 2019, 8:33:25 AM12/6/19
to Protocol Buffers
Finally I have figured out whether a field has options or not.

Unfortunately every field in message is associated with FieldOptions. By default, each field has DefaultOptions .So I could make pointer comparision and say whether a field has Custom field options or not.


Final question remains: How do I know the attributes of FieldOptions.

Adam Cozzette

unread,
Dec 6, 2019, 1:26:22 PM12/6/19
to LakshmiKanth Gupta, Protocol Buffers

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/ead37db8-2237-4de5-9cfb-625af0a394d6%40googlegroups.com.

Nadav Samet

unread,
Dec 6, 2019, 1:31:00 PM12/6/19
to Adam Cozzette, LakshmiKanth Gupta, Protocol Buffers
KakshmiKanth, in addition, please take a look at HasExtension and GetExtension: https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#extension to get your extension field from FieldOptions.



--
-Nadav

LakshmiKanth Gupta

unread,
Dec 7, 2019, 12:19:15 PM12/7/19
to Protocol Buffers
Here is way to do it without code generation. 

When we are developing compiler plugin....it is not feasible to use code generated c++.

I think the following solution works:
const Reflection *reflection= message.GetReflection();
std::vector<const FieldDescriptor *> l_fields;
reflection.ListFields( message, &fields );

Once we have FieldDescriptor, then it is usual business
 

--
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 prot...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages