set numerci_range and read numeric_range

45 views
Skip to first unread message

Shahab Nassiri

unread,
Jul 11, 2023, 3:51:18 AM7/11/23
to Protocol Buffers
Hi

I want to set numeric_range on protobuf field and read the defined numeric_range in runtime. I use the file descriptor and field descriptor for this goal.

But i have a problem. I don't want to hard code the name of class variable.

I express one example about this. I define a day field in proto file and set numeric_range on it as following.

message Person{
      int32 day  = 1; [range_min = 5; range_max = 10]
}

now, I want to read the defined numeric_range in runtime.

for this goal, i must determine the variable name as hard code.

is there any way for determining the variable as string?


thank you

Adam Cozzette

unread,
Jul 11, 2023, 12:13:17 PM7/11/23
to Shahab Nassiri, Protocol Buffers
You can reference Person::kDayFieldNumber to get the field number, and then look up the field descriptor with something like Person::descriptor()->FindFieldByNumber(Person::kDayFieldNumber).

--
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/4f45d34e-cee0-44c5-a10c-b3806f72e3acn%40googlegroups.com.

Shahab Nassiri

unread,
Jul 15, 2023, 4:02:27 AM7/15/23
to Protocol Buffers
I want to access to field by the string. Your answer is by the class name. I use this solution at now.

Michael Ngarimu

unread,
Jul 16, 2023, 3:34:12 AM7/16/23
to Protocol Buffers
From any Message you can call GetReflection(). 

From the Reflection call ListFields().

FieldDescriptor has a variety of “name” attributes that are available as strings against which to compare.

FieldDescriptor also has FieldOptions. I have not used FieldOptions for numeric_range but the documentation suggests that FieldOptions supports the options enclosed in []. https://protobuf.dev/reference/cpp/api-docs/google.protobuf.descriptor/#FieldDescriptor.options.details


Reply all
Reply to author
Forward
0 new messages