Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for named unions in protobuf emitter #4450

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 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,14 @@ 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: {
"anonymous": "anonymous unions cannot be used in Protobuf messages",
"anonymous-variant": "union variants without identifiers cannot be used in Protobuf 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