Ask Your Question
0

What is the method to make protobuf.js generate string values for enums instead of integers?

asked 2022-09-04 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-06-21 23:00:00 +0000

devzero gravatar image

To make protobuf.js generate string values for enums instead of integers, you can use the string option when defining the enum field in the protobuf schema:

enum MyEnum {
  option allow_alias = true;
  option (protobufjs.enum_value_string) = true; // enable string values

  VALUE_ONE = 0 [(protobufjs.value) = "enum-value-one"]; // set string value
  VALUE_TWO = 1 [(protobufjs.value) = "enum-value-two"]; // set string value
}

Here, the (protobufjs.enum_value_string) = true option enables string values for the enum, and the (protobufjs.value) option allows you to set a custom string value for each enum value.

When you generate code from this schema using protobuf.js, the generated TypeScript/JavaScript code will have string values for the enum instead of integers:

enum MyEnum {
  VALUE_ONE = "enum-value-one",
  VALUE_TWO = "enum-value-two",
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-04 11:00:00 +0000

Seen: 11 times

Last updated: Jun 21 '21