-
Notifications
You must be signed in to change notification settings - Fork 15.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generated C# files don't contain numeric constants for FieldOptions,MessageOptions,etc #4591
Comments
Sorry, I'm incredibly busy at the moment - I can't take on any extra work. |
Willing to take a PR for this? |
You can also encourage the maintainers to accept the proto2 in C# proposal which has support for extensions. |
Proto3 has support for options already. In C# it's via the CustomOptions class, which has typed TryGet values that take the numeric field ids of the options. All I'm wanting is for the generator to also build named constants for those numbers, so instead of this
it would be a cleaner and more readible like the C++ generated code, and look more like this
where
Seems like a simple thing to add to the csharp generator that already supports options. Is there some hidden complexity here? |
@jswigart Feel free to send me a PR to add that. It was not added because that's not the right way to support custom options, but we don't see harm adding it though. Note that the generated constant should be named consistently with other existing constants, i.e., use "public const int FooFieldNumber = ...". |
what do you think if I wanted to go a slight step forward, and make the generated identifiers type safe, such that they can only be passed to the CustomOptions that they are matched with? For example, this commit in my fork generates a block in the csharp file that looks like this. this helper identifier is therefore type safe in that you can only pass a FileOptionFieldId to the CustomOptions on a FileDescriptor
So then in your code you could query with a friendly identifier
And you will get a compiler error if you mistakingly try to pass the wrong type as the first argument. Thoughts? If you'd prefer just a bunch of naked integer constants I can do that as well, but I thought it would be useful to make it safer within the option types. |
Nevermind that type safe approach, that would break existing users, so I rolled it back to the simpler version |
@ObsidianMinor: I believe this issue is now deprecated - the options API has changed a few times since this. Do you think we can just close this? |
Yeah that makes sense |
I am generating files for C++ and C# using protoc 3.5.1 and while the C++ files have named constants generated for the various options I am defining in the proto file, there are no such named constants in the C# source file, even though the options appear to still function, if I manually use the numeric values in the CustomOptions.TryGet functions.
Any reason why there are no named constants generated in the C# file to avoid hard coding numbers(and make the code more readable, etc)
The text was updated successfully, but these errors were encountered: