@aptre/protobuf-es-lite 0.2.4 → 0.2.5

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; } });
@@ -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");
@@ -98,12 +99,12 @@ function generateEnum(f, enumeration) {
98
99
  }
99
100
  f.print("}");
100
101
  f.print();
101
- f.print("// ", enumeration, "_Name maps the enum names to the values.");
102
- f.print(f.exportDecl("const", enumeration.name + "_Name"), " = {");
102
+ f.print("// ", enumeration, "_Enum is the enum type for ", enumeration, ".");
103
+ f.print(f.exportDecl("const", enumeration.name + "_Enum"), " = ", CreateEnumTypeImport, "(", f.string(enumeration.typeName), ", [");
103
104
  for (const value of enumeration.values) {
104
- f.print(" ", (0, ecmascript_1.localName)(value), ": ", enumeration, ".", (0, ecmascript_1.localName)(value), ",");
105
+ f.print(" { no: ", value.number, ', name: "', value.name, '" },');
105
106
  }
106
- f.print("};");
107
+ f.print("]);");
107
108
  f.print();
108
109
  }
109
110
  function generateMessage(schema, f, message) {
@@ -208,7 +209,7 @@ function getFieldInfoLiteral(field) {
208
209
  }
209
210
  break;
210
211
  case "enum":
211
- e.push(`kind: "enum", T: `, field.enum, `, `);
212
+ e.push(`kind: "enum", T: `, field.enum, `_Enum, `);
212
213
  break;
213
214
  }
214
215
  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.5",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"