bufbuild / protobuf-es

Protocol Buffers for ECMAScript. The only JavaScript Protobuf library that is fully-compliant with Protobuf conformance tests.
Apache License 2.0
960 stars 55 forks source link

V2: Add option accessors #828

Closed timostamm closed 1 month ago

timostamm commented 1 month ago

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"