protoc-gen-star icon indicating copy to clipboard operation
protoc-gen-star copied to clipboard

cannot access multiple field options of a field

Open Shivam010 opened this issue 4 years ago • 4 comments

Cannot access all the field options tagged in the field in proto, when the options are defined in oneOf type See: https://github.com/Shivam010/protoc-gen-sample

When using this:

message Check {
    string field_1 = 1 [(main.rule1).type.one = "rule1.type.one", (main.rule1).type.two = "rule1.type.two"];
    string field_2 = 2 [(main.rule2).type.one = "rule2.type.one", (main.rule2).type.two = "rule2.type.two"];
    string field_3 = 3 [(main.rule2).type.two = "rule2.type.two", (main.rule2).type.one = "rule2.type.one"];
}

where rule1 and rule2 are 2 field options:

message Rule1 {
    String type = 1;
}
message Rule2 {
    oneof one_of {
        String type = 1;
    }
}
message String {
    string one = 1;
    string two = 2;
}

the retrieved options are for fields are:

field_1 one:"rule1.type.one" two:"rule1.type.two" 
field_2 two:"rule2.type.two" 
field_3 one:"rule2.type.one"

It turns out that in the case of oneOf a new message option is created each time. And hence, only second option value is received.

Shivam010 avatar Aug 03 '19 19:08 Shivam010

Hi, just curious if this was ever looked at or resolved?

adriangb avatar Aug 05 '20 14:08 adriangb

I don't think so. I resolved the issue in my project by using a different approach. I had pushed it in my sample repo.

message Check {
    string field_1 = 1 [(main.rule1).type.one = "rule1.type.one", (main.rule1).type.two = "rule1.type.two"];
    // issue
    string field_2 = 2 [(main.rule2).type.one = "rule2.type.one", (main.rule2).type.two = "rule2.type.two"];
    string field_3 = 3 [(main.rule2).type.two = "rule2.type.two", (main.rule2).type.one = "rule2.type.one"];
    // solution
    string field_4 = 4 [(main.rule2).type = {one: "rule2.type.one" two: "rule2.type.two"}];
    string field_5 = 5 [(main.rule2).type = {two: "rule2.type.two" one: "rule2.type.one"}];
}

Follow https://github.com/Shivam010/protoc-gen-sample/blob/master/check.proto#L7-L15

Shivam010 avatar Aug 05 '20 14:08 Shivam010

@Shivam010 Dude I only want to thank you because your sample repo helps me understand how to get options from messages.

theruziev avatar Aug 06 '23 07:08 theruziev

Happy that it was helpful for you! 🤗

Shivam010 avatar Aug 06 '23 09:08 Shivam010