Skip to content

[gRPC] Support integral enumerations #629

Description

@witemple-msft

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions