Skip to content

Commit

Permalink
add support for named unions in protobuf emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
joerosenberg committed Sep 15, 2024
1 parent 4b942c9 commit cf53a23
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/emitters/protobuf/reference/decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The field index of a Protobuf message must:

#### Target

`ModelProperty`
`ModelProperty | UnionVariant`

#### Parameters

Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/src/core/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
TemplatedType,
Type,
TypeMapper,
Union,
UnknownType,
Value,
VoidType,
Expand Down Expand Up @@ -164,7 +165,7 @@ export function isGlobalNamespace(
* @returns {boolean}
*/
export function isDeclaredInNamespace(
type: Model | Operation | Interface | Namespace | Enum,
type: Model | Operation | Interface | Namespace | Enum | Union,
namespace: Namespace,
options: { recursive?: boolean } = { recursive: true }
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The field index of a Protobuf message must:

##### Target

`ModelProperty`
`ModelProperty | UnionVariant`

##### Parameters

Expand Down
3 changes: 2 additions & 1 deletion packages/protobuf/generated-defs/TypeSpec.Protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Namespace,
Operation,
Type,
UnionVariant,
} from "@typespec/compiler";

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ export type MessageDecorator = (context: DecoratorContext, target: Type) => void
*/
export type FieldDecorator = (
context: DecoratorContext,
target: ModelProperty,
target: ModelProperty | UnionVariant,
index: number
) => void;

Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/lib/proto.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ extern dec message(target: {});
* }
* ```
*/
extern dec field(target: TypeSpec.Reflection.ModelProperty, index: valueof uint32);
extern dec field(target: TypeSpec.Reflection.ModelProperty | TypeSpec.Reflection.UnionVariant, index: valueof uint32);

/**
* Reserve a field index, range, or name. If a field definition collides with a reservation, the emitter will produce
Expand Down
11 changes: 9 additions & 2 deletions packages/protobuf/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ export const TypeSpecProtobufLibrary = createTypeSpecLibrary({
"unsupported-field-type": {
severity: "error",
messages: {
unconvertible: paramMessage`cannot convert a ${"type"} to a protobuf type (only intrinsic types and models are supported)`,
unconvertible: paramMessage`cannot convert a ${"type"} to a protobuf type (only intrinsic types, models and unions are supported)`,
"unknown-intrinsic": paramMessage`no known protobuf scalar for intrinsic type ${"name"}`,
"unknown-scalar": paramMessage`no known protobuf scalar for TypeSpec scalar type ${"name"}`,
"recursive-map": "a protobuf map's 'value' type may not refer to another map",
union: "a message field's type may not be a union",
"union-variant-map": "a protobuf oneof cannot directly include a map",
"union-variant-array": "a protobuf oneof cannot directly include a repeated field"
},
},
"namespace-collision": {
Expand All @@ -124,6 +125,12 @@ export const TypeSpecProtobufLibrary = createTypeSpecLibrary({
"the first variant of an enum must be set to zero to be used in a Protobuf message",
},
},
"unconvertible-union": {
severity: "error",
messages: {
"no-variants": "a union must contain at least one variant to be used in a Protobuf message"
}
},
"nested-array": {
severity: "error",
messages: {
Expand Down
3 changes: 2 additions & 1 deletion packages/protobuf/src/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
resolvePath,
StringLiteral,
Type,
UnionVariant,
} from "@typespec/compiler";

import {
Expand Down Expand Up @@ -148,7 +149,7 @@ export const $message: MessageDecorator = (ctx: DecoratorContext, target: Type)
*/
export const $field: FieldDecorator = (
ctx: DecoratorContext,
target: ModelProperty,
target: ModelProperty | UnionVariant,
fieldIndex: number
) => {
if (!Number.isInteger(fieldIndex) || fieldIndex <= 0) {
Expand Down
Loading

0 comments on commit cf53a23

Please sign in to comment.