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

Consider providing a convenient accessor for custom options from Desc* types #701

Closed
Jayatubi opened this issue Feb 2, 2024 · 3 comments

Comments

@Jayatubi
Copy link

Jayatubi commented Feb 2, 2024

It looks like getExtension could only be used on Message instance but not Descriptors as findCustomScalarOption used to do.

What if I want to get the options without creating a new instance but only with the Descriptors, such as get options from an enum?

It would be better if these extension related interfaces could be used with Types, aka Descriptors, instead of instances.

@timostamm
Copy link
Member

It is correct that getExtension can only be used on Message instances. A custom option extends one of the option messages defined in descriptor.proto.

For example, a Protobuf enum is described by the message google.protobuf.EnumDescriptorProto, and it has the following field:

message EnumDescriptorProto {
  // ...
  optional EnumOptions options = 3;
  // ...
} 

If you define an enum option, you extend google.protobuf.EnumOptions, and the Protobuf compiler will set the value as an extension on this message field.

You can retrieve the option value again from this message field. Here is how you would do it for an enum:

declare const descEnum: DescEnum;
if (descEnum.proto.options && hasExtension(descEnum.proto.options, my_option)) {
  const myOption = getExtension(descEnum.proto.options, my_option);
  // ...

We have a full tutorial here, which also links to a runnable example.

getExtension is not a direct replacement for findCustomScalarOption. findCustomScalarOption accepts DescEnum as an argument, and looks up proto.options for you, which can be convenient. It would be nice if we had a direct replacement for findCustom*Option that uses extensions under the hood. Let's use this issue to track this addition.

@timostamm timostamm changed the title getExtension doesn't work with enums. Consider providing a convenient accessor for custom options from Desc* types Feb 2, 2024
@Jayatubi
Copy link
Author

Jayatubi commented Feb 3, 2024

I use protobuf-es not only with the generated types but also with DescriptorSet, from protoc -o. I used to use findCustom*Option with hard coded option number since the previous version of protoc-gen-es skipped all the extensions which were defined in proto files. such as:

const descriptorSet = createDescriptorSet(/* read this file... */);

for (const [name, descriptor] of descriptorSet.messages) {
  if (findCustomScalarOption(descriptor, /* magic number */ 1234567, ScalarType.BOOL)) {
    // Do something with the message which has a specified option
  }
}

It would be better that to keep findCustom*Option not to be deprecated and then replace the hard coded number by generated code from proto file by the new version protoc-gen-es.

@timostamm
Copy link
Member

This is implemented for V2 in #828

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

2 participants