Skip to content

V2: Add option accessors#828

Merged
timostamm merged 1 commit intov2from
tstamm/option-accessors
May 2, 2024
Merged

V2: Add option accessors#828
timostamm merged 1 commit intov2from
tstamm/option-accessors

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