cadl/packages/protobuf
Timothee Guerin 0dea649cd7
Update dependencies to resolve vulnerabilities and license issue (#3081)
- Remove unused rollup-plugin-dts dep that had problematic license
- Update dependencies to fix vulnerabilies
2024-03-29 21:22:06 +00:00
..
generated-defs Generate TypeScript decorator signatures from `extern dec` (#2122) 2024-03-19 23:08:59 +00:00
lib Rename template parameters for clarity and consistency (#2726) 2023-12-18 13:44:37 -05:00
src Generate TypeScript decorator signatures from `extern dec` (#2122) 2024-03-19 23:08:59 +00:00
test [protobuf] Add support for expanding template names. (#2861) 2024-02-01 18:23:32 +00:00
.eslintrc.cjs Vitest improvements: vitest-ui, watch mode for deps, debug config (#2791) 2024-01-22 09:56:55 -08:00
.gitignore [gRPC] @typespec/protobuf (#636) 2023-04-25 16:23:51 +00:00
CHANGELOG.json Prepare Publish for January Release (#2816) 2024-01-24 11:14:23 -08:00
CHANGELOG.md Remove old message burried in changelog (#2989) 2024-03-05 16:05:57 -08:00
LICENSE [gRPC] @typespec/protobuf (#636) 2023-04-25 16:23:51 +00:00
README.md Improved ref docs for model (#2951) 2024-02-29 07:21:13 -08:00
package.json Update dependencies to resolve vulnerabilities and license issue (#3081) 2024-03-29 21:22:06 +00:00
tsconfig.config.json Vitest improvements: vitest-ui, watch mode for deps, debug config (#2791) 2024-01-22 09:56:55 -08:00
tsconfig.json Generate TypeScript decorator signatures from `extern dec` (#2122) 2024-03-19 23:08:59 +00:00
vitest.config.ts Vitest improvements: vitest-ui, watch mode for deps, debug config (#2791) 2024-01-22 09:56:55 -08:00

README.md

@typespec/protobuf

TypeSpec library and emitter for Protobuf (gRPC)

Install

npm install @typespec/protobuf

Emitter

Usage

  1. Via the command line
tsp compile . --emit=@typespec/protobuf
  1. Via the config
emit:
  - "@typespec/protobuf"

Emitter options

noEmit

Type: boolean

If set to true, this emitter will not write any files. It will still validate the TypeSpec sources to ensure they are compatible with Protobuf, but the files will simply not be written to the output directory.

omit-unreachable-types

Type: boolean

By default, the emitter will create message declarations for any models in a namespace decorated with @package that have an @field decorator on every property. If this option is set to true, this behavior will be disabled, and only messages that are explicitly decorated with @message or that are reachable from a service operation will be emitted.

Decorators

TypeSpec.Protobuf

@field

Defines the field index of a model property for conversion to a Protobuf message.

The field index of a Protobuf message must:

  • fall between 1 and 229 - 1, inclusive.
  • not fall within the implementation reserved range of 19000 to 19999, inclusive.
  • not fall within any range that was [marked reserved](#
@TypeSpec.Protobuf.field(index: valueof uint32)
Target

ModelProperty

Parameters
Name Type Description
index valueof uint32 The whole-number index of the field.
Examples
model ExampleMessage {
  @field(1)
  test: string;
}

@message

Declares that a model is a Protobuf message.

Messages can be detected automatically if either of the following two conditions are met:

  • The model has a @field annotation on all of its properties.
  • The model is referenced by any service operation.

This decorator will force the emitter to check and emit a model.

@TypeSpec.Protobuf.message
Target

{}

Parameters

None

@package

Declares that a TypeSpec namespace constitutes a Protobuf package. The contents of the namespace will be emitted to a single Protobuf file.

@TypeSpec.Protobuf.package(details?: TypeSpec.Protobuf.PackageDetails)
Target

Namespace

Parameters
Name Type Description
details PackageDetails the optional details of the package

@reserve

Reserve a field index, range, or name. If a field definition collides with a reservation, the emitter will produce an error.

This decorator accepts multiple reservations. Each reservation is one of the following:

  • a string, in which case the reservation refers to a field name.
  • a uint32, in which case the reservation refers to a field index.
  • a tuple [uint32, uint32], in which case the reservation refers to a field range that is inclusive of both ends.

Unlike in Protobuf, where field name and index reservations must be separated, you can mix string and numeric field reservations in a single @reserve call in TypeSpec.

API Compatibility Note

Field reservations prevent users of your Protobuf specification from using the given field names or indices. This can be useful if a field is removed, as it will further prevent adding a new, incompatible field and will prevent users from utilizing the field index at runtime in a way that may break compatibility with users of older specifications.

See Protobuf Language Guide - Reserved Fields for more information.

@TypeSpec.Protobuf.reserve(...reservations: valueof string | [uint32, uint32] | uint32[])
Target

{}

Parameters
Name Type Description
reservations valueof string | [uint32, uint32] | uint32[] a list of field reservations
Examples
// Reserve the fields 8-15 inclusive, 100, and the field name "test" within a model.
@reserve([8, 15], 100, "test")
model Example {
  // ...
}

@service

Declares that a TypeSpec interface constitutes a Protobuf service. The contents of the interface will be converted to a service declaration in the resulting Protobuf file.

@TypeSpec.Protobuf.service
Target

Interface

Parameters

None

@stream

Set the streaming mode of an operation. See StreamMode for more information.

@TypeSpec.Protobuf.stream(mode: TypeSpec.Protobuf.StreamMode)
Target

Operation

Parameters
Name Type Description
mode StreamMode The streaming mode to apply to this operation.
Examples
@stream(StreamMode.Out)
op logs(...LogsRequest): LogEvent;
@stream(StreamMode.Duplex)
op connectToMessageService(...Message): Message;