microsoft / microsoft/typespec

[protobuf] Support unions.

Open
#1,854 0 comments 2 reactions 1 assignee Claimed by @witemple-msft View on GitHub
emitter:protobuf feature triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

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:

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

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

We would then emit the following:

```proto3
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.