@aptre/protobuf-es-lite 0.2.4 → 0.2.6

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.
package/dist/enum.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { EnumType, EnumValueInfo } from "@bufbuild/protobuf";
2
+ export declare function normalizeEnumValue(value: EnumValueInfo | Omit<EnumValueInfo, "localName">): EnumValueInfo;
3
+ /**
4
+ * Create a new EnumType with the given values.
5
+ */
6
+ export declare function createEnumType(typeName: string, values: (EnumValueInfo | Omit<EnumValueInfo, "localName">)[], _opt?: {}): EnumType;
package/dist/enum.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEnumType = exports.normalizeEnumValue = void 0;
4
+ function normalizeEnumValue(value) {
5
+ if ("localName" in value) {
6
+ return value;
7
+ }
8
+ return { ...value, localName: value.name };
9
+ }
10
+ exports.normalizeEnumValue = normalizeEnumValue;
11
+ /**
12
+ * Create a new EnumType with the given values.
13
+ */
14
+ function createEnumType(typeName, values,
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
+ _opt) {
17
+ const names = Object.create(null);
18
+ const numbers = Object.create(null);
19
+ const normalValues = [];
20
+ for (const value of values) {
21
+ // We do not surface options at this time
22
+ // const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
23
+ const n = normalizeEnumValue(value);
24
+ normalValues.push(n);
25
+ names[value.name] = n;
26
+ numbers[value.no] = n;
27
+ }
28
+ return {
29
+ typeName,
30
+ values: normalValues,
31
+ // We do not surface options at this time
32
+ // options: opt?.options ?? Object.create(null),
33
+ findName(name) {
34
+ return names[name];
35
+ },
36
+ findNumber(no) {
37
+ return numbers[no];
38
+ },
39
+ };
40
+ }
41
+ exports.createEnumType = createEnumType;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ export { EnumType, EnumValue, EnumValueInfo, ScalarType, WireType, LongType, } from "@bufbuild/protobuf";
1
2
  export { Message, CompleteMessage, AnyMessage, MessageType, Field, CompleteField, compareMessages, createMessageType, } from "./message.js";
2
3
  export { isCompleteMessage, isCompleteField } from "./is-message.js";
3
4
  export { newFieldList, FieldList, PartialFieldInfo, FieldInfo, OneofInfo, fieldJsonName, localFieldName, localOneofName, } from "./field.js";
4
5
  export { scalarEquals, scalarZeroValue, isScalarZeroValue } from "./scalar.js";
6
+ export { createEnumType, normalizeEnumValue } from "./enum.js";
package/dist/index.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.isCompleteField = exports.isCompleteMessage = exports.createMessageType = exports.compareMessages = void 0;
3
+ exports.normalizeEnumValue = exports.createEnumType = exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.isCompleteField = exports.isCompleteMessage = exports.createMessageType = exports.compareMessages = exports.LongType = exports.WireType = exports.ScalarType = exports.EnumValue = void 0;
4
+ var protobuf_1 = require("@bufbuild/protobuf");
5
+ Object.defineProperty(exports, "EnumValue", { enumerable: true, get: function () { return protobuf_1.EnumValue; } });
6
+ Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function () { return protobuf_1.ScalarType; } });
7
+ Object.defineProperty(exports, "WireType", { enumerable: true, get: function () { return protobuf_1.WireType; } });
8
+ Object.defineProperty(exports, "LongType", { enumerable: true, get: function () { return protobuf_1.LongType; } });
4
9
  var message_js_1 = require("./message.js");
5
10
  Object.defineProperty(exports, "compareMessages", { enumerable: true, get: function () { return message_js_1.compareMessages; } });
6
11
  Object.defineProperty(exports, "createMessageType", { enumerable: true, get: function () { return message_js_1.createMessageType; } });
@@ -17,3 +22,6 @@ var scalar_js_1 = require("./scalar.js");
17
22
  Object.defineProperty(exports, "scalarEquals", { enumerable: true, get: function () { return scalar_js_1.scalarEquals; } });
18
23
  Object.defineProperty(exports, "scalarZeroValue", { enumerable: true, get: function () { return scalar_js_1.scalarZeroValue; } });
19
24
  Object.defineProperty(exports, "isScalarZeroValue", { enumerable: true, get: function () { return scalar_js_1.isScalarZeroValue; } });
25
+ var enum_js_1 = require("./enum.js");
26
+ Object.defineProperty(exports, "createEnumType", { enumerable: true, get: function () { return enum_js_1.createEnumType; } });
27
+ Object.defineProperty(exports, "normalizeEnumValue", { enumerable: true, get: function () { return enum_js_1.normalizeEnumValue; } });
@@ -1,5 +1,7 @@
1
- import type { DescExtension, DescField } from "@bufbuild/protobuf";
2
- import type { GeneratedFile, Printable, Schema } from "@bufbuild/protoplugin/ecmascript";
1
+ import type { DescEnum, DescExtension, DescField, DescFile, DescMessage } from "@bufbuild/protobuf";
2
+ import type { GeneratedFile, ImportSymbol, Printable, Schema } from "@bufbuild/protoplugin/ecmascript";
3
3
  export declare function generateTs(schema: Schema): void;
4
+ export declare function makeImportPath(file: DescFile): string;
4
5
  export declare function generateFieldInfo(f: GeneratedFile, field: DescField | DescExtension): void;
6
+ export declare const createTypeImport: (desc: DescMessage | DescEnum | DescExtension) => ImportSymbol;
5
7
  export declare function getFieldInfoLiteral(field: DescField | DescExtension): Printable;
@@ -14,7 +14,7 @@
14
14
  // See the License for the specific language governing permissions and
15
15
  // limitations under the License.
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.getFieldInfoLiteral = exports.generateFieldInfo = exports.generateTs = void 0;
17
+ exports.getFieldInfoLiteral = exports.createTypeImport = exports.generateFieldInfo = exports.makeImportPath = exports.generateTs = void 0;
18
18
  const protobuf_1 = require("@bufbuild/protobuf");
19
19
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
20
20
  const editions_js_1 = require("./editions.js");
@@ -24,6 +24,7 @@ const MessageImport = (0, ecmascript_1.createImportSymbol)("Message", libPkg);
24
24
  const MessageTypeImport = (0, ecmascript_1.createImportSymbol)("MessageType", libPkg);
25
25
  const CreateMessageTypeImport = (0, ecmascript_1.createImportSymbol)("createMessageType", libPkg);
26
26
  const PartialFieldInfo = (0, ecmascript_1.createImportSymbol)("PartialFieldInfo", libPkg);
27
+ const CreateEnumTypeImport = (0, ecmascript_1.createImportSymbol)("createEnumType", libPkg);
27
28
  function generateTs(schema) {
28
29
  for (const file of schema.files) {
29
30
  const f = schema.generateFile(file.name + "_pb.ts");
@@ -39,6 +40,9 @@ function generateTs(schema) {
39
40
  if (message.file !== file) {
40
41
  return;
41
42
  }
43
+ for (const nestedEnum of message.nestedEnums) {
44
+ generateEnum(f, nestedEnum);
45
+ }
42
46
  messageTypes.push(message);
43
47
  const deps = new Set();
44
48
  for (const field of message.fields) {
@@ -98,12 +102,12 @@ function generateEnum(f, enumeration) {
98
102
  }
99
103
  f.print("}");
100
104
  f.print();
101
- f.print("// ", enumeration, "_Name maps the enum names to the values.");
102
- f.print(f.exportDecl("const", enumeration.name + "_Name"), " = {");
105
+ f.print("// ", enumeration, "_Enum is the enum type for ", enumeration, ".");
106
+ f.print(f.exportDecl("const", enumeration.name + "_Enum"), " = ", CreateEnumTypeImport, "(", f.string(enumeration.typeName), ", [");
103
107
  for (const value of enumeration.values) {
104
- f.print(" ", (0, ecmascript_1.localName)(value), ": ", enumeration, ".", (0, ecmascript_1.localName)(value), ",");
108
+ f.print(" { no: ", value.number, ', name: "', value.name, '" },');
105
109
  }
106
- f.print("};");
110
+ f.print("]);");
107
111
  f.print();
108
112
  }
109
113
  function generateMessage(schema, f, message) {
@@ -132,12 +136,6 @@ function generateMessage(schema, f, message) {
132
136
  f.print(" },");
133
137
  f.print(");");
134
138
  f.print();
135
- for (const nestedEnum of message.nestedEnums) {
136
- generateEnum(f, nestedEnum);
137
- }
138
- for (const nestedMessage of message.nestedMessages) {
139
- generateMessage(schema, f, nestedMessage);
140
- }
141
139
  }
142
140
  function generateField(f, field) {
143
141
  if (field.oneof) {
@@ -167,10 +165,23 @@ function generateOneof(f, oneof) {
167
165
  .flat();
168
166
  f.print(" ", oneof.name, "?: {\n value?: undefined,\n case: undefined\n }", oneOfCases, ";");
169
167
  }
168
+ function makeImportPath(file) {
169
+ return "./" + file.name + "_pb.js";
170
+ }
171
+ exports.makeImportPath = makeImportPath;
170
172
  function generateFieldInfo(f, field) {
171
173
  f.print(" ", getFieldInfoLiteral(field), ",");
172
174
  }
173
175
  exports.generateFieldInfo = generateFieldInfo;
176
+ const createTypeImport = (desc) => {
177
+ var name = (0, ecmascript_1.localName)(desc);
178
+ if (desc.kind === "enum") {
179
+ name += "_Enum";
180
+ }
181
+ const from = makeImportPath(desc.file);
182
+ return (0, ecmascript_1.createImportSymbol)(name, from);
183
+ };
184
+ exports.createTypeImport = createTypeImport;
174
185
  function getFieldInfoLiteral(field) {
175
186
  const e = [];
176
187
  e.push("{ no: ", field.number, `, `);
@@ -208,7 +219,7 @@ function getFieldInfoLiteral(field) {
208
219
  }
209
220
  break;
210
221
  case "enum":
211
- e.push(`kind: "enum", T: `, field.enum, `, `);
222
+ e.push(`kind: "enum", T: `, (0, exports.createTypeImport)(field.enum), `, `);
212
223
  break;
213
224
  }
214
225
  if (field.repeated) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aptre/protobuf-es-lite",
3
3
  "description": "Lightweight Protobuf codegen for TypeScript and JavaScript.",
4
- "version": "0.2.4",
4
+ "version": "0.2.6",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"