@typespec/protobuf 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/.c8rc.json +3 -0
  2. package/.eslintrc.cjs +7 -0
  3. package/.mocharc.yaml +4 -0
  4. package/.rush/temp/operation/build/state.json +3 -0
  5. package/.rush/temp/operation/test-official/state.json +3 -0
  6. package/.rush/temp/package-deps_build.json +81 -0
  7. package/.rush/temp/package-deps_test-official.json +81 -0
  8. package/.rush/temp/shrinkwrap-deps.json +177 -0
  9. package/CHANGELOG.json +4 -0
  10. package/LICENSE +21 -0
  11. package/README.md +30 -0
  12. package/coverage/cobertura-coverage.xml +2291 -0
  13. package/coverage/coverage-final.json +7 -0
  14. package/coverage/tmp/coverage-6408-1683144315012-0.json +1 -0
  15. package/dist/src/ast.d.ts +199 -0
  16. package/dist/src/ast.d.ts.map +1 -0
  17. package/dist/src/ast.js +60 -0
  18. package/dist/src/ast.js.map +1 -0
  19. package/dist/src/index.d.ts +62 -0
  20. package/dist/src/index.d.ts.map +1 -0
  21. package/dist/src/index.js +5 -0
  22. package/dist/src/index.js.map +1 -0
  23. package/dist/src/lib.d.ts +197 -0
  24. package/dist/src/lib.d.ts.map +1 -0
  25. package/dist/src/lib.js +134 -0
  26. package/dist/src/lib.js.map +1 -0
  27. package/dist/src/proto.d.ts +62 -0
  28. package/dist/src/proto.d.ts.map +1 -0
  29. package/dist/src/proto.js +162 -0
  30. package/dist/src/proto.js.map +1 -0
  31. package/dist/src/transform/index.d.ts +7 -0
  32. package/dist/src/transform/index.d.ts.map +1 -0
  33. package/dist/src/transform/index.js +744 -0
  34. package/dist/src/transform/index.js.map +1 -0
  35. package/dist/src/write.d.ts +12 -0
  36. package/dist/src/write.d.ts.map +1 -0
  37. package/dist/src/write.js +204 -0
  38. package/dist/src/write.js.map +1 -0
  39. package/dist/test/scenarios.test.d.ts +2 -0
  40. package/dist/test/scenarios.test.d.ts.map +1 -0
  41. package/dist/test/scenarios.test.js +165 -0
  42. package/dist/test/scenarios.test.js.map +1 -0
  43. package/lib/proto.tsp +303 -0
  44. package/package.json +49 -0
  45. package/protobuf.build.log +48 -0
  46. package/src/ast.ts +279 -0
  47. package/src/index.ts +7 -0
  48. package/src/lib.ts +161 -0
  49. package/src/proto.ts +217 -0
  50. package/src/transform/index.ts +980 -0
  51. package/src/write.ts +244 -0
  52. package/temp/tsconfig.tsbuildinfo +1 -0
  53. package/test/include/foo/bar.proto +7 -0
  54. package/test/scenarios/addressbook/input/addressbook.tsp +27 -0
  55. package/test/scenarios/addressbook/input/main.tsp +13 -0
  56. package/test/scenarios/addressbook/output/@typespec/protobuf/addressbook.proto +26 -0
  57. package/test/scenarios/addressbook/output/@typespec/protobuf/main.proto +14 -0
  58. package/test/scenarios/anonymous-model/diagnostics.txt +1 -0
  59. package/test/scenarios/anonymous-model/input/main.tsp +24 -0
  60. package/test/scenarios/anonymous-package/input/main.tsp +19 -0
  61. package/test/scenarios/anonymous-package/output/@typespec/protobuf/main.proto +15 -0
  62. package/test/scenarios/array/input/main.tsp +22 -0
  63. package/test/scenarios/array/output/@typespec/protobuf/com/azure/test.proto +18 -0
  64. package/test/scenarios/array-nested/diagnostics.txt +1 -0
  65. package/test/scenarios/array-nested/input/main.tsp +20 -0
  66. package/test/scenarios/cross package references/input/main.tsp +27 -0
  67. package/test/scenarios/cross package references/output/@typespec/protobuf/A.proto +10 -0
  68. package/test/scenarios/cross package references/output/@typespec/protobuf/B.proto +15 -0
  69. package/test/scenarios/derived-scalar/input/main.tsp +24 -0
  70. package/test/scenarios/derived-scalar/output/@typespec/protobuf/com/azure/Test.proto +18 -0
  71. package/test/scenarios/enum/input/main.tsp +33 -0
  72. package/test/scenarios/enum/output/@typespec/protobuf/main.proto +31 -0
  73. package/test/scenarios/enum-nonintegral/diagnostics.txt +4 -0
  74. package/test/scenarios/enum-nonintegral/input/main.tsp +31 -0
  75. package/test/scenarios/enum-nozero/diagnostics.txt +1 -0
  76. package/test/scenarios/enum-nozero/input/main.tsp +25 -0
  77. package/test/scenarios/extern/input/main.tsp +17 -0
  78. package/test/scenarios/extern/output/@typespec/protobuf/main.proto +19 -0
  79. package/test/scenarios/illegal field reservations/diagnostics.txt +2 -0
  80. package/test/scenarios/illegal field reservations/input/main.tsp +16 -0
  81. package/test/scenarios/inferred-message-names/input/main.tsp +16 -0
  82. package/test/scenarios/inferred-message-names/output/@typespec/protobuf/com/azure/test.proto +18 -0
  83. package/test/scenarios/intrinsics/input/main.tsp +17 -0
  84. package/test/scenarios/intrinsics/output/@typespec/protobuf/com/azure/Test.proto +16 -0
  85. package/test/scenarios/map/input/main.tsp +15 -0
  86. package/test/scenarios/map/output/@typespec/protobuf/main.proto +13 -0
  87. package/test/scenarios/model-no-package/diagnostics.txt +2 -0
  88. package/test/scenarios/model-no-package/input/main.tsp +19 -0
  89. package/test/scenarios/name-collision/input/main.tsp +24 -0
  90. package/test/scenarios/name-collision/output/@typespec/protobuf/main.proto +24 -0
  91. package/test/scenarios/options/input/main.tsp +25 -0
  92. package/test/scenarios/options/output/@typespec/protobuf/com/azure/Test.proto +20 -0
  93. package/test/scenarios/options-invalid/diagnostics.txt +1 -0
  94. package/test/scenarios/options-invalid/input/main.tsp +25 -0
  95. package/test/scenarios/reserved field collisions/diagnostics.txt +5 -0
  96. package/test/scenarios/reserved field collisions/input/main.tsp +19 -0
  97. package/test/scenarios/reserved fields/input/main.tsp +16 -0
  98. package/test/scenarios/reserved fields/output/@typespec/protobuf/main.proto +16 -0
  99. package/test/scenarios/simple/input/main.tsp +22 -0
  100. package/test/scenarios/simple/output/@typespec/protobuf/com/azure/Test.proto +18 -0
  101. package/test/scenarios/simple-error/diagnostics.txt +6 -0
  102. package/test/scenarios/simple-error/input/main.tsp +22 -0
  103. package/test/scenarios/simple-no-service/input/main.tsp +22 -0
  104. package/test/scenarios/simple-no-service/output/@typespec/protobuf/com/azure/Test.proto +18 -0
  105. package/test/scenarios/streams/input/main.tsp +30 -0
  106. package/test/scenarios/streams/output/@typespec/protobuf/main.proto +19 -0
  107. package/test/scenarios/type-validation/diagnostics.txt +1 -0
  108. package/test/scenarios/type-validation/input/main.tsp +11 -0
  109. package/test/scenarios/union/diagnostics.txt +1 -0
  110. package/test/scenarios/union/input/main.tsp +33 -0
  111. package/test/scenarios.test.ts +226 -0
  112. package/tsconfig.json +15 -0
@@ -0,0 +1,199 @@
1
+ import { Namespace } from "@typespec/compiler";
2
+ /**
3
+ * This module describes an AST for Protobuf.
4
+ */
5
+ /**
6
+ * A single .proto file.
7
+ */
8
+ export interface ProtoFile {
9
+ /**
10
+ * The package name, if one is known.
11
+ */
12
+ package?: string;
13
+ /**
14
+ * The `option` specifiers to include in the file.
15
+ */
16
+ options: Partial<WellKnownFileOptions>;
17
+ /**
18
+ * Paths imported by this file.
19
+ */
20
+ imports: string[];
21
+ /**
22
+ * The declarations in the file.
23
+ *
24
+ * Only `service` and `message` declarations may exist at the root of the file.
25
+ */
26
+ declarations: Iterable<ProtoTopLevelDeclaration>;
27
+ /**
28
+ * The original namespace node from which this ProtoFile originated.
29
+ */
30
+ source: Namespace;
31
+ }
32
+ /**
33
+ * The built-in options that are defined by Protobuf.
34
+ */
35
+ export interface WellKnownFileOptions {
36
+ java_package: string;
37
+ java_outer_classname: string;
38
+ optimize_for: "SPEED" | "CODE_SIZE" | "LITE_RUNTIME";
39
+ cc_enable_arenas: boolean;
40
+ }
41
+ /**
42
+ * A top-level declaration.
43
+ */
44
+ export type ProtoTopLevelDeclaration = ProtoServiceDeclaration | ProtoMessageDeclaration | ProtoEnumDeclaration;
45
+ /**
46
+ * A declaration. One of `service`, `message`, a field within a message, `one_of`, `enum`, or an `rpc` method.
47
+ */
48
+ export type ProtoDeclaration = ProtoServiceDeclaration | ProtoMessageDeclaration | ProtoFieldDeclaration | ProtoOneOfDeclaration | ProtoEnumDeclaration | ProtoMethodDeclaration;
49
+ /**
50
+ * A Protobuf scalar type.
51
+ */
52
+ export type ScalarName = ScalarIntegralName | "double" | "float" | "bytes" | "string";
53
+ /**
54
+ * A Protobuf integral type.
55
+ */
56
+ export type ScalarIntegralName = ScalarIntegerName | ScalarFixedName | "bool";
57
+ /**
58
+ * A Protobuf variable-length integer type.
59
+ */
60
+ export type ScalarIntegerName = `${"u" | "s" | ""}int${"32" | "64"}`;
61
+ /**
62
+ * A Protobuf fixed-length integer type.
63
+ */
64
+ export type ScalarFixedName = `${"s" | ""}fixed${"32" | "64"}`;
65
+ declare const $scalar: unique symbol;
66
+ declare const $ref: unique symbol;
67
+ declare const $map: unique symbol;
68
+ /**
69
+ * A map type. Map keys can be any integral or string type (any scalar except float, double, and bytes).
70
+ *
71
+ * The value may be any type other than another map.
72
+ */
73
+ export type ProtoMap = [typeof $map, ScalarIntegralName | "string", ProtoRef | ProtoScalar];
74
+ /**
75
+ * A reference to a named message type.
76
+ */
77
+ export type ProtoRef = [typeof $ref, string];
78
+ /**
79
+ * A scalar type.
80
+ */
81
+ export type ProtoScalar = [typeof $scalar, ScalarName];
82
+ /**
83
+ * A Protobuf type.
84
+ */
85
+ export type ProtoType = ProtoScalar | ProtoRef | ProtoMap;
86
+ /**
87
+ * Create a scalar type by name.
88
+ */
89
+ export declare function scalar(t: ScalarName): ProtoScalar;
90
+ /**
91
+ * Create a type reference (symbol) to a named message.
92
+ */
93
+ export declare function ref(t: string): ProtoRef;
94
+ /**
95
+ * Create a map from a key type to a value type.
96
+ */
97
+ export declare function map(k: ScalarIntegralName | "string", v: Exclude<ProtoType, ProtoMap>): ProtoMap;
98
+ /**
99
+ * Creates a type that will throw an internal error if the system attempts to emit it.
100
+ *
101
+ * @param message - optional message that should be printed
102
+ */
103
+ export declare function unreachable(message?: string): never;
104
+ /**
105
+ * A "pattern" object with variants for each Protobuf type.
106
+ */
107
+ export interface ProtoTypeMatchPattern<T> {
108
+ scalar: (s: ScalarName) => T;
109
+ ref: (r: string) => T;
110
+ map: (k: ScalarIntegralName | "string", v: Exclude<ProtoType, ProtoMap>) => T;
111
+ }
112
+ /**
113
+ * A helper function that matches and delegates a Protobuf type to a handler per type.
114
+ *
115
+ * @param type - the Protobuf type to match and delegate
116
+ * @param pattern - the matching pattern of delegates to apply
117
+ * @returns
118
+ */
119
+ export declare function matchType<Result>(type: ProtoType, pattern: ProtoTypeMatchPattern<Result>): Result;
120
+ /**
121
+ * A `service` declaration.
122
+ */
123
+ export interface ProtoServiceDeclaration {
124
+ kind: "service";
125
+ name: string;
126
+ operations: ProtoMethodDeclaration[];
127
+ }
128
+ /**
129
+ * An operation's streaming mode.
130
+ */
131
+ export declare const enum StreamingMode {
132
+ Duplex = 3,
133
+ In = 2,
134
+ Out = 1,
135
+ None = 0
136
+ }
137
+ /**
138
+ * An `rfc` method declaration.
139
+ */
140
+ export interface ProtoMethodDeclaration {
141
+ kind: "method";
142
+ stream: StreamingMode;
143
+ name: string;
144
+ input: ProtoRef;
145
+ returns: ProtoRef;
146
+ }
147
+ /**
148
+ * A declaration that can fit within the body of a message declaration.
149
+ */
150
+ export type ProtoMessageBodyDeclaration = ProtoFieldDeclaration | ProtoMessageDeclaration | ProtoOneOfDeclaration | ProtoEnumDeclaration;
151
+ /**
152
+ * A `message` declaration.
153
+ */
154
+ export interface ProtoMessageDeclaration {
155
+ kind: "message";
156
+ name: string;
157
+ declarations: Array<ProtoMessageBodyDeclaration>;
158
+ reservations?: Array<string | number | [number, number]>;
159
+ }
160
+ /**
161
+ * A field declaration within a message.
162
+ */
163
+ export interface ProtoFieldDeclaration {
164
+ kind: "field";
165
+ name: string;
166
+ /**
167
+ * Whether or not the field is repeated (i.e. an array).
168
+ */
169
+ repeated?: boolean;
170
+ options?: Partial<DefaultFieldOptions>;
171
+ type: ProtoType;
172
+ index: number;
173
+ }
174
+ /**
175
+ * The options for fields defined by the Protobuf specification.
176
+ */
177
+ export interface DefaultFieldOptions {
178
+ packed: true;
179
+ deprecated: true;
180
+ }
181
+ /**
182
+ * A `one_of` declaration.
183
+ */
184
+ export interface ProtoOneOfDeclaration {
185
+ kind: "oneof";
186
+ name: string;
187
+ declarations: ProtoFieldDeclaration[];
188
+ }
189
+ /**
190
+ * An `enum` declaration.
191
+ */
192
+ export interface ProtoEnumDeclaration {
193
+ kind: "enum";
194
+ name: string;
195
+ allowAlias?: boolean;
196
+ variants: [string, number][];
197
+ }
198
+ export {};
199
+ //# sourceMappingURL=ast.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../src/ast.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C;;GAEG;AAEH;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAEvC;;OAEG;IACH,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB;;;;OAIG;IACH,YAAY,EAAE,QAAQ,CAAC,wBAAwB,CAAC,CAAC;IAEjD;;OAEG;IACH,MAAM,EAAE,SAAS,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,MAAM,CAAC;IAE7B,YAAY,EAAE,OAAO,GAAG,WAAW,GAAG,cAAc,CAAC;IAErD,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAChC,uBAAuB,GACvB,uBAAuB,GACvB,oBAAoB,CAAC;AAEzB;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB,uBAAuB,GACvB,uBAAuB,GACvB,qBAAqB,GACrB,qBAAqB,GACrB,oBAAoB,GACpB,sBAAsB,CAAC;AAE3B;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,kBAAkB,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEtF;;GAEG;AACH,MA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AO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAC;IACvC,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,IAAI,CAAC;IACb,UAAU,EAAE,IAAI,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,qBAAqB,EAAE,CAAC;CACvC;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;CAC9B"}
@@ -0,0 +1,60 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Symbols for type destructuring
3
+ const $scalar = Symbol("$scalar");
4
+ const $ref = Symbol("$ref");
5
+ const $map = Symbol("$map");
6
+ /**
7
+ * Create a scalar type by name.
8
+ */
9
+ export function scalar(t) {
10
+ return [$scalar, t];
11
+ }
12
+ /**
13
+ * Create a type reference (symbol) to a named message.
14
+ */
15
+ export function ref(t) {
16
+ return [$ref, t];
17
+ }
18
+ /**
19
+ * Create a map from a key type to a value type.
20
+ */
21
+ export function map(k, v) {
22
+ return [$map, k, v];
23
+ }
24
+ /* c8 ignore start */
25
+ // Unreachable, by definition, should not be covered :)
26
+ /**
27
+ * Creates a type that will throw an internal error if the system attempts to emit it.
28
+ *
29
+ * @param message - optional message that should be printed
30
+ */
31
+ export function unreachable(message = "tried to emit unreachable type") {
32
+ // This little "array-like" object will throw an internal error as soon as the "tag" is inspected.
33
+ return Object.freeze({
34
+ get [0]() {
35
+ throw new Error("Internal Error: " + message);
36
+ },
37
+ });
38
+ }
39
+ /**
40
+ * A helper function that matches and delegates a Protobuf type to a handler per type.
41
+ *
42
+ * @param type - the Protobuf type to match and delegate
43
+ * @param pattern - the matching pattern of delegates to apply
44
+ * @returns
45
+ */
46
+ export function matchType(type, pattern) {
47
+ switch (type[0]) {
48
+ case $ref:
49
+ return pattern.ref(type[1]);
50
+ case $scalar:
51
+ return pattern.scalar(type[1]);
52
+ case $map:
53
+ return pattern.map(type[1], type[2]);
54
+ /* c8 ignore next 5 */
55
+ default:
56
+ const __exhaust = type[0];
57
+ throw new Error(`Internal Error: unreachable matchType variant ${__exhaust}`);
58
+ }
59
+ }
60
+ //# sourceMappingURL=ast.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast.js","sourceRoot":"","sources":["../../src/ast.ts"],"names":[],"mappings":"AAAA,uCAAuC;AA0FvC,iCAAiC;AACjC,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAClC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAwB5B;;GAEG;AACH,MAAM,UAAU,MAAM,CAAC,CAAa;IAClC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;AACtB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG,CAAC,CAAS;IAC3B,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;AACnB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG,CAAC,CAAgC,EAAE,CAA+B;IACnF,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AACtB,CAAC;AAED,qBAAqB;AAErB,uDAAuD;AAEvD;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,UAAkB,gCAAgC;IAC5E,kGAAkG;IAClG,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,IAAI,CAAC,CAAC,CAAC;YACL,MAAM,IAAI,KAAK,CAAC,kBAAkB,GAAG,OAAO,CAAC,CAAC;QAChD,CAAC;KACF,CAAU,CAAC;AACd,CAAC;AAaD;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAS,IAAe,EAAE,OAAsC;IACvF,QAAQ,IAAI,CAAC,CAAC,CAAC,EAAE;QACf,KAAK,IAAI;YACP,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9B,KAAK,OAAO;YACV,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACjC,KAAK,IAAI;YACP,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAiC,CAAC,CAAC;QACvE,sBAAsB;QACtB;YACE,MAAM,SAAS,GAAU,IAAI,CAAC,CAAC,CAAC,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,iDAAiD,SAAS,EAAE,CAAC,CAAC;KACjF;AACH,CAAC"}
@@ -0,0 +1,62 @@
1
+ export declare const namespace = "TypeSpec.Protobuf";
2
+ export * from "./proto.js";
3
+ export declare const $lib: import("@typespec/compiler").TypeSpecLibrary<{
4
+ "field-index": {
5
+ missing: import("@typespec/compiler").CallableMessage<[string]>;
6
+ invalid: import("@typespec/compiler").CallableMessage<[string]>;
7
+ "out-of-bounds": import("@typespec/compiler").CallableMessage<[string, string]>;
8
+ reserved: import("@typespec/compiler").CallableMessage<[string]>;
9
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
10
+ "user-reserved-range": import("@typespec/compiler").CallableMessage<[string]>;
11
+ };
12
+ "field-name": {
13
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
14
+ };
15
+ "root-operation": {
16
+ default: string;
17
+ };
18
+ "unsupported-intrinsic": {
19
+ default: import("@typespec/compiler").CallableMessage<[string]>;
20
+ };
21
+ "unsupported-return-type": {
22
+ default: string;
23
+ };
24
+ "unsupported-input-type": {
25
+ "wrong-number": string;
26
+ "wrong-type": string;
27
+ unconvertible: string;
28
+ };
29
+ "unsupported-field-type": {
30
+ unconvertible: import("@typespec/compiler").CallableMessage<[string]>;
31
+ "unknown-intrinsic": import("@typespec/compiler").CallableMessage<[string]>;
32
+ "unknown-scalar": import("@typespec/compiler").CallableMessage<[string]>;
33
+ "recursive-map": string;
34
+ union: string;
35
+ };
36
+ "namespace-collision": {
37
+ default: import("@typespec/compiler").CallableMessage<[string]>;
38
+ };
39
+ "unconvertible-enum": {
40
+ default: string;
41
+ "no-zero-first": string;
42
+ };
43
+ "nested-array": {
44
+ default: string;
45
+ };
46
+ "invalid-package-name": {
47
+ default: import("@typespec/compiler").CallableMessage<[string]>;
48
+ };
49
+ "illegal-reservation": {
50
+ default: string;
51
+ };
52
+ "model-not-in-package": {
53
+ default: import("@typespec/compiler").CallableMessage<[string]>;
54
+ };
55
+ "anonymous-model": {
56
+ default: string;
57
+ };
58
+ package: {
59
+ "disallowed-option-type": import("@typespec/compiler").CallableMessage<[string, string]>;
60
+ };
61
+ }, import("./lib.js").ProtobufEmitterOptions>;
62
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,sBAAsB,CAAC;AAE7C,cAAc,YAAY,CAAC;AAE3B,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6CAA0B,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { TypeSpecProtobufLibrary } from "./lib.js";
2
+ export const namespace = "TypeSpec.Protobuf";
3
+ export * from "./proto.js";
4
+ export const $lib = TypeSpecProtobufLibrary;
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAEnD,MAAM,CAAC,MAAM,SAAS,GAAG,mBAAmB,CAAC;AAE7C,cAAc,YAAY,CAAC;AAE3B,MAAM,CAAC,MAAM,IAAI,GAAG,uBAAuB,CAAC"}
@@ -0,0 +1,197 @@
1
+ /**
2
+ * Options that the Protobuf emitter accepts.
3
+ */
4
+ export interface ProtobufEmitterOptions {
5
+ /**
6
+ * Don't emit anything.
7
+ */
8
+ noEmit?: boolean;
9
+ }
10
+ export declare const TypeSpecProtobufLibrary: import("@typespec/compiler").TypeSpecLibrary<{
11
+ "field-index": {
12
+ missing: import("@typespec/compiler").CallableMessage<[string]>;
13
+ invalid: import("@typespec/compiler").CallableMessage<[string]>;
14
+ "out-of-bounds": import("@typespec/compiler").CallableMessage<[string, string]>;
15
+ reserved: import("@typespec/compiler").CallableMessage<[string]>;
16
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
17
+ "user-reserved-range": import("@typespec/compiler").CallableMessage<[string]>;
18
+ };
19
+ "field-name": {
20
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
21
+ };
22
+ "root-operation": {
23
+ default: string;
24
+ };
25
+ "unsupported-intrinsic": {
26
+ default: import("@typespec/compiler").CallableMessage<[string]>;
27
+ };
28
+ "unsupported-return-type": {
29
+ default: string;
30
+ };
31
+ "unsupported-input-type": {
32
+ "wrong-number": string;
33
+ "wrong-type": string;
34
+ unconvertible: string;
35
+ };
36
+ "unsupported-field-type": {
37
+ unconvertible: import("@typespec/compiler").CallableMessage<[string]>;
38
+ "unknown-intrinsic": import("@typespec/compiler").CallableMessage<[string]>;
39
+ "unknown-scalar": import("@typespec/compiler").CallableMessage<[string]>;
40
+ "recursive-map": string;
41
+ union: string;
42
+ };
43
+ "namespace-collision": {
44
+ default: import("@typespec/compiler").CallableMessage<[string]>;
45
+ };
46
+ "unconvertible-enum": {
47
+ default: string;
48
+ "no-zero-first": string;
49
+ };
50
+ "nested-array": {
51
+ default: string;
52
+ };
53
+ "invalid-package-name": {
54
+ default: import("@typespec/compiler").CallableMessage<[string]>;
55
+ };
56
+ "illegal-reservation": {
57
+ default: string;
58
+ };
59
+ "model-not-in-package": {
60
+ default: import("@typespec/compiler").CallableMessage<[string]>;
61
+ };
62
+ "anonymous-model": {
63
+ default: string;
64
+ };
65
+ package: {
66
+ "disallowed-option-type": import("@typespec/compiler").CallableMessage<[string, string]>;
67
+ };
68
+ }, ProtobufEmitterOptions>;
69
+ export declare const reportDiagnostic: <C extends "field-index" | "field-name" | "root-operation" | "unsupported-intrinsic" | "unsupported-return-type" | "unsupported-input-type" | "unsupported-field-type" | "namespace-collision" | "unconvertible-enum" | "nested-array" | "invalid-package-name" | "illegal-reservation" | "model-not-in-package" | "anonymous-model" | "package", M extends keyof {
70
+ "field-index": {
71
+ missing: import("@typespec/compiler").CallableMessage<[string]>;
72
+ invalid: import("@typespec/compiler").CallableMessage<[string]>;
73
+ "out-of-bounds": import("@typespec/compiler").CallableMessage<[string, string]>;
74
+ reserved: import("@typespec/compiler").CallableMessage<[string]>;
75
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
76
+ "user-reserved-range": import("@typespec/compiler").CallableMessage<[string]>;
77
+ };
78
+ "field-name": {
79
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
80
+ };
81
+ "root-operation": {
82
+ default: string;
83
+ };
84
+ "unsupported-intrinsic": {
85
+ default: import("@typespec/compiler").CallableMessage<[string]>;
86
+ };
87
+ "unsupported-return-type": {
88
+ default: string;
89
+ };
90
+ "unsupported-input-type": {
91
+ "wrong-number": string;
92
+ "wrong-type": string;
93
+ unconvertible: string;
94
+ };
95
+ "unsupported-field-type": {
96
+ unconvertible: import("@typespec/compiler").CallableMessage<[string]>;
97
+ "unknown-intrinsic": import("@typespec/compiler").CallableMessage<[string]>;
98
+ "unknown-scalar": import("@typespec/compiler").CallableMessage<[string]>;
99
+ "recursive-map": string;
100
+ union: string;
101
+ };
102
+ "namespace-collision": {
103
+ default: import("@typespec/compiler").CallableMessage<[string]>;
104
+ };
105
+ "unconvertible-enum": {
106
+ default: string;
107
+ "no-zero-first": string;
108
+ };
109
+ "nested-array": {
110
+ default: string;
111
+ };
112
+ "invalid-package-name": {
113
+ default: import("@typespec/compiler").CallableMessage<[string]>;
114
+ };
115
+ "illegal-reservation": {
116
+ default: string;
117
+ };
118
+ "model-not-in-package": {
119
+ default: import("@typespec/compiler").CallableMessage<[string]>;
120
+ };
121
+ "anonymous-model": {
122
+ default: string;
123
+ };
124
+ package: {
125
+ "disallowed-option-type": import("@typespec/compiler").CallableMessage<[string, string]>;
126
+ };
127
+ }[C]>(program: import("@typespec/compiler").Program, diag: import("@typespec/compiler").DiagnosticReport<{
128
+ "field-index": {
129
+ missing: import("@typespec/compiler").CallableMessage<[string]>;
130
+ invalid: import("@typespec/compiler").CallableMessage<[string]>;
131
+ "out-of-bounds": import("@typespec/compiler").CallableMessage<[string, string]>;
132
+ reserved: import("@typespec/compiler").CallableMessage<[string]>;
133
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
134
+ "user-reserved-range": import("@typespec/compiler").CallableMessage<[string]>;
135
+ };
136
+ "field-name": {
137
+ "user-reserved": import("@typespec/compiler").CallableMessage<[string]>;
138
+ };
139
+ "root-operation": {
140
+ default: string;
141
+ };
142
+ "unsupported-intrinsic": {
143
+ default: import("@typespec/compiler").CallableMessage<[string]>;
144
+ };
145
+ "unsupported-return-type": {
146
+ default: string;
147
+ };
148
+ "unsupported-input-type": {
149
+ "wrong-number": string;
150
+ "wrong-type": string;
151
+ unconvertible: string;
152
+ };
153
+ "unsupported-field-type": {
154
+ unconvertible: import("@typespec/compiler").CallableMessage<[string]>;
155
+ "unknown-intrinsic": import("@typespec/compiler").CallableMessage<[string]>;
156
+ "unknown-scalar": import("@typespec/compiler").CallableMessage<[string]>;
157
+ "recursive-map": string;
158
+ union: string;
159
+ };
160
+ "namespace-collision": {
161
+ default: import("@typespec/compiler").CallableMessage<[string]>;
162
+ };
163
+ "unconvertible-enum": {
164
+ default: string;
165
+ "no-zero-first": string;
166
+ };
167
+ "nested-array": {
168
+ default: string;
169
+ };
170
+ "invalid-package-name": {
171
+ default: import("@typespec/compiler").CallableMessage<[string]>;
172
+ };
173
+ "illegal-reservation": {
174
+ default: string;
175
+ };
176
+ "model-not-in-package": {
177
+ default: import("@typespec/compiler").CallableMessage<[string]>;
178
+ };
179
+ "anonymous-model": {
180
+ default: string;
181
+ };
182
+ package: {
183
+ "disallowed-option-type": import("@typespec/compiler").CallableMessage<[string, string]>;
184
+ };
185
+ }, C, M>) => void;
186
+ export type TypeSpecProtobufLibrary = typeof TypeSpecProtobufLibrary;
187
+ export declare const state: {
188
+ message: symbol;
189
+ stream: symbol;
190
+ service: symbol;
191
+ package: symbol;
192
+ fieldIndex: symbol;
193
+ externRef: symbol;
194
+ reserve: symbol;
195
+ _map: symbol;
196
+ };
197
+ //# sourceMappingURL=lib.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../../src/lib.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAaD,eAAO,MAAM,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0BAiHlC,CAAC;AAEH,eAAO,MAAQ,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAA4B,CAAC;AAE5D,MAAM,MAAM,uBAAuB,GAAG,OAAO,uBAAuB,CAAC;AAarE,eAAO,MAAM,KAAK;;;;;;;;;CAIjB,CAAC"}
@@ -0,0 +1,134 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ import { createTypeSpecLibrary, paramMessage } from "@typespec/compiler";
4
+ const EmitterOptionsSchema = {
5
+ type: "object",
6
+ additionalProperties: false,
7
+ properties: {
8
+ noEmit: { type: "boolean", nullable: true },
9
+ },
10
+ required: [],
11
+ };
12
+ const PACKAGE_NAME = "@typespec/protobuf";
13
+ export const TypeSpecProtobufLibrary = createTypeSpecLibrary({
14
+ name: PACKAGE_NAME,
15
+ requireImports: [PACKAGE_NAME],
16
+ diagnostics: {
17
+ "field-index": {
18
+ severity: "error",
19
+ messages: {
20
+ missing: paramMessage `field ${"name"} does not have a field index, but one is required (try using the '@field' decorator)`,
21
+ invalid: paramMessage `field index ${"index"} is invalid (must be an integer greater than zero)`,
22
+ "out-of-bounds": paramMessage `field index ${"index"} is out of bounds (must be less than ${"max"})`,
23
+ reserved: paramMessage `field index ${"index"} falls within the implementation-reserved range of 19000-19999 inclusive`,
24
+ "user-reserved": paramMessage `field index ${"index"} was reserved by a call to @reserve on this model`,
25
+ "user-reserved-range": paramMessage `field index ${"index"} falls within a range reserved by a call to @reserve on this model`,
26
+ },
27
+ },
28
+ "field-name": {
29
+ severity: "error",
30
+ messages: {
31
+ "user-reserved": paramMessage `field name '${"name"}' was reserved by a call to @reserve on this model`,
32
+ },
33
+ },
34
+ "root-operation": {
35
+ severity: "error",
36
+ messages: {
37
+ default: "operations in the root namespace are not supported (no associated Protobuf service)",
38
+ },
39
+ },
40
+ "unsupported-intrinsic": {
41
+ severity: "error",
42
+ messages: {
43
+ default: paramMessage `intrinsic type ${"name"} is not supported in Protobuf`,
44
+ },
45
+ },
46
+ "unsupported-return-type": {
47
+ severity: "error",
48
+ messages: {
49
+ default: "Protobuf methods must return a named Model",
50
+ },
51
+ },
52
+ "unsupported-input-type": {
53
+ severity: "error",
54
+ messages: {
55
+ "wrong-number": "Protobuf methods must accept exactly one Model input (an empty model will do)",
56
+ "wrong-type": "Protobuf methods may only accept a named Model as an input",
57
+ unconvertible: "input parameters cannot be converted to a Protobuf message",
58
+ },
59
+ },
60
+ "unsupported-field-type": {
61
+ severity: "error",
62
+ messages: {
63
+ unconvertible: paramMessage `cannot convert a ${"type"} to a protobuf type (only intrinsic types and models are supported)`,
64
+ "unknown-intrinsic": paramMessage `no known protobuf scalar for intrinsic type ${"name"}`,
65
+ "unknown-scalar": paramMessage `no known protobuf scalar for TypeSpec scalar type ${"name"}`,
66
+ "recursive-map": "a protobuf map's 'value' type may not refer to another map",
67
+ union: "a message field's type may not be a union",
68
+ },
69
+ },
70
+ "namespace-collision": {
71
+ severity: "error",
72
+ messages: {
73
+ default: paramMessage `the package name ${"name"} has already been used`,
74
+ },
75
+ },
76
+ "unconvertible-enum": {
77
+ severity: "error",
78
+ messages: {
79
+ default: "enums must explicitly assign exactly one integer to each member to be used in a Protobuf message",
80
+ "no-zero-first": "the first variant of an enum must be set to zero to be used in a Protobuf message",
81
+ },
82
+ },
83
+ "nested-array": {
84
+ severity: "error",
85
+ messages: {
86
+ default: "nested arrays are not supported by the Protobuf emitter",
87
+ },
88
+ },
89
+ "invalid-package-name": {
90
+ severity: "error",
91
+ messages: {
92
+ default: paramMessage `${"name"} is not a valid package name (must consist of letters and numbers separated by ".")`,
93
+ },
94
+ },
95
+ "illegal-reservation": {
96
+ severity: "error",
97
+ messages: {
98
+ default: "reservation value must be a string literal, uint32 literal, or a tuple of two uint32 literals denoting a range",
99
+ },
100
+ },
101
+ "model-not-in-package": {
102
+ severity: "error",
103
+ messages: {
104
+ default: paramMessage `model ${"name"} is not in a namespace that uses the '@Protobuf.package' decorator`,
105
+ },
106
+ },
107
+ "anonymous-model": {
108
+ severity: "error",
109
+ messages: {
110
+ default: "anonymous models cannot be used in Protobuf messages",
111
+ },
112
+ },
113
+ package: {
114
+ severity: "error",
115
+ messages: {
116
+ "disallowed-option-type": paramMessage `option '${"name"}' with type '${"type"}' is not allowed in a package declaration (only string, boolean, and numeric types are allowed)`,
117
+ },
118
+ },
119
+ },
120
+ emitter: { options: EmitterOptionsSchema },
121
+ });
122
+ export const { reportDiagnostic } = TypeSpecProtobufLibrary;
123
+ const keys = [
124
+ "fieldIndex",
125
+ "package",
126
+ "service",
127
+ "externRef",
128
+ "stream",
129
+ "reserve",
130
+ "message",
131
+ "_map",
132
+ ];
133
+ export const state = Object.fromEntries(keys.map((k) => [k, TypeSpecProtobufLibrary.createStateSymbol(k)]));
134
+ //# sourceMappingURL=lib.js.map