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

V2: Add option accessors #828

Merged
merged 1 commit into from
May 2, 2024
Merged

V2: Add option accessors #828

merged 1 commit into from
May 2, 2024

Conversation

timostamm
Copy link
Member

This adds accessor functions for options, similar to the existing accessors for extensions.

Options are extensions to the google.protobuf.*Options messages defined in
google/protobuf/descriptor.proto. The accessors provide just a bit more convenience for working with generated descriptors, or descriptors provided by @bufbuild/protoplugin.

For example, let's define a message option and set it on a message:

syntax = "proto3";

import "google/protobuf/descriptor.proto";

MessageWithOption {
  option (my_option) = "hello";
}

extend google.protobuf.MessageOptions {
  optional string my_option = 1001;
}

To read the option, use getOption:

import { getOption } from "@bufbuild/protobuf";
import { MessageWithOptionDesc, my_option} from "./gen/example_pb.js";

getOption(MessageWithOptionDesc, my_option); // "hello"

Comment on lines +46 to +47
if (hasOption(service, default_host)) {
const defaultHost = getOption(service, default_host);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is how it looks in practice.

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

Successfully merging this pull request may close these issues.

2 participants