question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

enums as string value

See original GitHub issue

Hello,

ts-proto offers the options --ts_proto_opt=stringEnums=true which allows for generated enum types to be string-based instead of int-based.

This is useful if you want “only types” and are using a gRPC REST Gateway configured to serialize enums as strings.

Are you willing to support this? It will be very useful in a twirp and gPRC gateway setting

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
timostammcommented, Jul 12, 2021

Give it a try, this already works 😁

The canonical mapping for a protobuf enum is string, and parsers must accept both enum names and integer values.

The canonical mapping deals with edge cases like numeric map keys, infinite numbers, etc. The documentation is sparse, but the conformance tests are not (protobuf-ts passes them). You can be pretty sure that you’ll have perfect JSON interoperability.

0reactions
fenoscommented, Jul 17, 2021

@timostamm I gave more and more thoughts around this.

I think you are 100% right here, it all boils down to how enums works in protobuf.

For now I’ll accept the fact that they are annoying 😄 as a workaround I’ve nested my enum to prevent collisions:


message Workflow {
  enum Status {
    UNKNOWN = 0;
    PENDING = 1;
    IN_PROGRESS = 2;
    FINISHED = 3;
    ERRORED = 4;
  }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Enum values as String literals - java - Stack Overflow
Option One: use the built-in name() on an enum. This is perfectly fine if you don't need any special naming format. String name...
Read more >
String Enums in C#: Everything You Need to Know
An enumeration type is a value type that has a set of named constants. To declare an enumeration type, use the enum keyword...
Read more >
Convert Enum To String In C# - C# Corner
We can convert an enum to string by calling the ToString() method of an Enum. The following code example shows how to convert...
Read more >
Handbook - Enums - TypeScript
String enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member...
Read more >
Enum.ToString Method (System) - Microsoft Learn
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found