Skip to content

[protobuf] Support unions. #1854

@witemple-msft

Description

@witemple-msft

Unions are currently not supported in the protobuf emitter. There are a few challenges associated with converting unions to protobuf oneof declarations:

  • oneof declarations are inline fields of a message.
  • oneof fields share a field index space with the message that they are declared within.
  • oneof declarations must be named and must have a name for each variant of the oneof.

Possible Solution 1: Require named unions

We could aggressively limit support for oneof declarations to named union declarations in TypeSpec, and only support the following form:

union Foo {
    @field(1) a: int32;
    @field(2) b: string;
}

@message model Bar {
    @field(1) value: Foo
}

We would then emit the following:

message Foo {
  oneof value {
    int32 a = 1;
    string b = 2;
  }
}

message Bar {
  Foo value = 1;
}

This seems like one of the only approaches that preserves (1) the field indices declared within the union in their own field index space, (2) the name of the field within the message Bar and (3) the field names of the union.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions