Skip to content
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

C# .NET GetOption doesn't have a way to test a descriptor for options #7240

Closed
thesoftwarejedi opened this issue Feb 21, 2020 · 3 comments
Closed

Comments

@thesoftwarejedi
Copy link

thesoftwarejedi commented Feb 21, 2020

Regarding the recent C# Custom Options

CustomOptions on Descriptors is now Obsolete. The replacement GetOption throws an exception if the descriptor has no options. Is there any way to test for this now?

The only workaround I can find is simply using the old way of doing a TryGet, which is an Obsolete way of doing it. CustomOptions doesn't even provide a way to determine if it is

//if I have to do this, I might as well just get the message when I check, but this is for example purposes
if (messageDescriptor?.CustomOptions.TryGetBytes(SomeExtensions.SomeOption.FieldNumber, out var nothing) ?? false)
{
    //this line will throw if the message has no `CustomOptions`
    SomeOption o = messageDescriptor?.GetOption<SomeOption>(SomeExtensions.SomeOption);
    return o;
}
return null;

I'm trying to test messages dynamically for options that they may not have.

CC: @ObsidianMinor

@thesoftwarejedi thesoftwarejedi changed the title CustomOptions no longer has a way to test a descriptor for options C# .NET CustomOptions no longer has a way to test a descriptor for options Feb 21, 2020
@ObsidianMinor
Copy link
Contributor

I have a PR open that adds null checks and discusses a "TryGetOption" API that doesn't return the default value here: #7130

@thesoftwarejedi
Copy link
Author

Perfect. I left a comment there but that looks like exactly what I would want. Thanks!

@jtattermusch
Copy link
Contributor

This is dupe of #7127.

@jtattermusch jtattermusch changed the title C# .NET CustomOptions no longer has a way to test a descriptor for options C# .NET GetOption no longer has a way to test a descriptor for options Apr 22, 2020
@jtattermusch jtattermusch changed the title C# .NET GetOption no longer has a way to test a descriptor for options C# .NET GetOption doesn't have a way to test a descriptor for options Apr 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants