Skip to content

[gRPC] Support integral enumerations #629

Closed

Description

Protobuf supports integral enums using the enum keyword. We should be able to map Cadl enum to Proto3 enum:

@service interface WidgetService {
  paint(@field(1) id: int32, @field(2) color: WidgetColor): void;
}

enum WidgetColor {
  RED: 0,
  BLUE: 1
}

This should yield the following service:

enum WidgetColor {
  RED = 0;
  BLUE = 1;
}

message PaintRequest {
  int32 id = 1;
  WidgetColor color = 2;
}

service WidgetService {
  rpc paint(PaintRequest) returns Empty;
}

There are some considerations:

  • The values must be int32 values, and there must be a zero value (which is implied to be the default value), so we would likely require Cadl enums to explicitly specify integral values to use them in gRPC emit.
    • We could also provide a template or decorator to "gRPC-ify" an enum.
  • The zero value is required to be the first value in the enum for proto2 compatibility reasons.
  • Protobuf enums support an option called allow_alias (off by default) that will permit multiple keys of the enum to refer to the same integral value.

Activity

  1. witemple-msft commented on Jun 21, 2022

    @witemple-msft
    MemberAuthor

    gRPC priority: 2
    Estimate: 3
    It should be relatively straightforward to do a basic mapping between CADL enum and Protobuf, but the devil is in the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions