@aptre/protobuf-es-lite 0.3.0 → 0.4.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.
package/dist/enum.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { DescEnum } from "./index.js";
1
2
  /**
2
3
  * Reflection information for a protobuf enumeration.
3
4
  */
@@ -33,8 +34,17 @@ export interface EnumValueInfo {
33
34
  */
34
35
  readonly localName: string;
35
36
  }
36
- export declare function normalizeEnumValue(value: EnumValueInfo | Omit<EnumValueInfo, "localName">): EnumValueInfo;
37
37
  /**
38
38
  * Create a new EnumType with the given values.
39
39
  */
40
40
  export declare function createEnumType(typeName: string, values: (EnumValueInfo | Omit<EnumValueInfo, "localName">)[], _opt?: {}): EnumType;
41
+ export declare function enumInfoZeroValue(values: (EnumValueInfo | Omit<EnumValueInfo, "localName">)[]): number;
42
+ export declare function enumDescZeroValue(info: DescEnum): number;
43
+ export declare function enumZeroValue<T extends EnumType>(info: T): number;
44
+ /**
45
+ * Returns the normalized version of the enum value.
46
+ * Null is cast to the default value.
47
+ * String names are cast to the number enum.
48
+ * If string and the value is unknown, throws an error.
49
+ */
50
+ export declare function normalizeEnumValue(info: EnumType, value: string | number | null | undefined): number;
package/dist/enum.js CHANGED
@@ -1,9 +1,3 @@
1
- export function normalizeEnumValue(value) {
2
- if ("localName" in value) {
3
- return value;
4
- }
5
- return { ...value, localName: value.name };
6
- }
7
1
  /**
8
2
  * Create a new EnumType with the given values.
9
3
  */
@@ -16,7 +10,7 @@ _opt) {
16
10
  for (const value of values) {
17
11
  // We do not surface options at this time
18
12
  // const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
19
- const n = normalizeEnumValue(value);
13
+ const n = "localName" in value ? value : { ...value, localName: value.name };
20
14
  normalValues.push(n);
21
15
  names[value.name] = n;
22
16
  numbers[value.no] = n;
@@ -34,3 +28,58 @@ _opt) {
34
28
  },
35
29
  };
36
30
  }
31
+ // enumInfoZeroValue returns the zero value for an enum info.
32
+ export function enumInfoZeroValue(values) {
33
+ if (!values?.length) {
34
+ return 0;
35
+ }
36
+ // In proto3, the first enum value must be zero.
37
+ // In proto2, protobuf-go returns the first value as the default.
38
+ const zeroValue = values[0];
39
+ return zeroValue.no;
40
+ }
41
+ // enumDescZeroValue returns the zero value for an enum description.
42
+ export function enumDescZeroValue(info) {
43
+ // In proto3, the first enum value must be zero.
44
+ // In proto2, protobuf-go returns the first value as the default.
45
+ if (info.values.length < 1) {
46
+ throw new Error("invalid enum: missing at least one value");
47
+ }
48
+ const zeroValue = info.values[0];
49
+ return zeroValue.number;
50
+ }
51
+ // enumZeroValue returns the zero value for an enum type.
52
+ export function enumZeroValue(info) {
53
+ // In proto3, the first enum value must be zero.
54
+ // In proto2, protobuf-go returns the first value as the default.
55
+ if (info.values.length < 1) {
56
+ throw new Error("invalid enum: missing at least one value");
57
+ }
58
+ const zeroValue = info.values[0];
59
+ return zeroValue.no;
60
+ }
61
+ /**
62
+ * Returns the normalized version of the enum value.
63
+ * Null is cast to the default value.
64
+ * String names are cast to the number enum.
65
+ * If string and the value is unknown, throws an error.
66
+ */
67
+ export function normalizeEnumValue(info, value) {
68
+ const zeroValue = enumZeroValue(info);
69
+ if (value == null) {
70
+ return zeroValue;
71
+ }
72
+ if (value === "" || value === zeroValue) {
73
+ return zeroValue;
74
+ }
75
+ if (typeof value === "string") {
76
+ // TODO: strip the type name prefix as well? MyEnum_VALUE
77
+ const val = info.findName(value);
78
+ if (!val) {
79
+ throw new Error(`enum ${info.typeName}: invalid value: "${value}"`);
80
+ }
81
+ return val.no;
82
+ }
83
+ // return the number value
84
+ return value;
85
+ }
@@ -5,8 +5,8 @@ import { ScalarType } from "./scalar.js";
5
5
  * ergonomic for use as a message field.
6
6
  */
7
7
  export interface FieldWrapper<T = any, U = any> {
8
- wrapField(value: U): T;
9
- unwrapField(value: T): U;
8
+ wrapField(value: U | null | undefined): T;
9
+ unwrapField(value: T): U | null | undefined;
10
10
  }
11
11
  /**
12
12
  * Wrap a primitive message field value in its corresponding wrapper
package/dist/field.d.ts CHANGED
@@ -28,7 +28,42 @@ import { protoCamelCase } from "./names.js";
28
28
  * - "default": Only proto2: An explicit default value.
29
29
  * - "delimited": Only proto2: Use the tag-delimited group encoding.
30
30
  */
31
- export type FieldInfo = fiRules<fiScalar> | fiRules<fiEnum> | fiRules<fiMessage> | fiRules<fiMap>;
31
+ export type FieldInfo = ScalarFieldInfo | EnumFieldInfo | MessageFieldInfo | MapFieldInfo;
32
+ /**
33
+ * ScalarFieldInfo represents a scalar field.
34
+ * It includes rules for field presence, repetition, and packing.
35
+ */
36
+ export type ScalarFieldInfo = fiRules<fiScalar>;
37
+ /**
38
+ * EnumFieldInfo represents an enum field.
39
+ * It includes rules for field presence, repetition, and packing.
40
+ */
41
+ export type EnumFieldInfo = fiRules<fiEnum>;
42
+ /**
43
+ * MessageFieldInfo represents a message field.
44
+ * It includes rules for field presence, repetition, and packing.
45
+ */
46
+ export type MessageFieldInfo = fiRules<fiMessage>;
47
+ /**
48
+ * MapFieldInfo represents a map field with a scalar key type and
49
+ * a scalar, enum, or message value type.
50
+ * It includes rules for field presence, repetition, and packing.
51
+ */
52
+ export type MapFieldInfo = fiRules<fiMap>;
53
+ /**
54
+ * MapValueInfo represents the value type of a map field.
55
+ * The value can be a scalar, enum, or message type.
56
+ */
57
+ export type MapValueInfo = {
58
+ readonly kind: "scalar";
59
+ readonly T: ScalarType;
60
+ } | {
61
+ readonly kind: "enum";
62
+ readonly T: EnumType;
63
+ } | {
64
+ readonly kind: "message";
65
+ readonly T: MessageType<any> | (() => MessageType<any>);
66
+ };
32
67
  /**
33
68
  * Provides convenient access to field information of a message type.
34
69
  */
@@ -283,16 +318,7 @@ interface fiMap extends fiShared {
283
318
  /**
284
319
  * Map value type. Can be scalar, enum, or message.
285
320
  */
286
- readonly V: {
287
- readonly kind: "scalar";
288
- readonly T: ScalarType;
289
- } | {
290
- readonly kind: "enum";
291
- readonly T: EnumType;
292
- } | {
293
- readonly kind: "message";
294
- readonly T: MessageType<any> | (() => MessageType<any>);
295
- };
321
+ readonly V: MapValueInfo;
296
322
  /**
297
323
  * Is the field repeated? Never true for maps.
298
324
  */
@@ -27,7 +27,7 @@
27
27
  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
28
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
- import { applyPartialMessage, createMessageType, ScalarType } from "../../index.js";
30
+ import { applyPartialMessage, createMessageType, ScalarType, } from "../../index.js";
31
31
  export const protobufPackage = "google.protobuf";
32
32
  // Any_Wkt contains the well-known-type overrides for Any.
33
33
  const Any_Wkt = {
@@ -42,7 +42,10 @@ const Any_Wkt = {
42
42
  }
43
43
  const message = messageType.fromBinary(msg.value);
44
44
  let json = messageType.toJson(message, options);
45
- if (typeName.startsWith("google.protobuf.") || (json === null || Array.isArray(json) || typeof json !== "object")) {
45
+ if (typeName.startsWith("google.protobuf.") ||
46
+ json === null ||
47
+ Array.isArray(json) ||
48
+ typeof json !== "object") {
46
49
  json = { value: json };
47
50
  }
48
51
  json["@type"] = typeName;
@@ -50,7 +53,9 @@ const Any_Wkt = {
50
53
  },
51
54
  fromJson(json, options) {
52
55
  if (json === null || Array.isArray(json) || typeof json != "object") {
53
- throw new Error(`cannot decode message google.protobuf.Any from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);
56
+ throw new Error(`cannot decode message google.protobuf.Any from JSON: expected object but got ${json === null ? "null"
57
+ : Array.isArray(json) ? "array"
58
+ : typeof json}`);
54
59
  }
55
60
  if (Object.keys(json).length == 0) {
56
61
  return {};
@@ -64,7 +69,8 @@ const Any_Wkt = {
64
69
  throw new Error(`cannot decode message google.protobuf.Any from JSON: ${typeUrl} is not in the type registry`);
65
70
  }
66
71
  let message;
67
- if (typeName.startsWith("google.protobuf.") && Object.prototype.hasOwnProperty.call(json, "value")) {
72
+ if (typeName.startsWith("google.protobuf.") &&
73
+ Object.prototype.hasOwnProperty.call(json, "value")) {
68
74
  message = messageType.fromJson(json["value"], options);
69
75
  }
70
76
  else {
@@ -91,11 +97,16 @@ const Any_Wkt = {
91
97
  unpack(msg, registry) {
92
98
  const typeUrl = msg.typeUrl;
93
99
  const messageType = !!typeUrl && registry.findMessage(typeUrl);
94
- return messageType ? { message: messageType.fromBinary(msg.value), messageType } : undefined;
100
+ return messageType ?
101
+ { message: messageType.fromBinary(msg.value), messageType }
102
+ : undefined;
95
103
  },
96
104
  is(msg, msgType) {
97
105
  const name = msg.typeUrl;
98
- return !!name && (typeof msgType === 'string' ? name === msgType : name === msgType.typeName);
106
+ return (!!name &&
107
+ (typeof msgType === "string" ?
108
+ name === msgType
109
+ : name === msgType.typeName));
99
110
  },
100
111
  };
101
112
  // Any contains the message type declaration for Any.
@@ -40,7 +40,13 @@ export const Method = createMessageType({
40
40
  { no: 3, name: "request_streaming", kind: "scalar", T: ScalarType.BOOL },
41
41
  { no: 4, name: "response_type_url", kind: "scalar", T: ScalarType.STRING },
42
42
  { no: 5, name: "response_streaming", kind: "scalar", T: ScalarType.BOOL },
43
- { no: 6, name: "options", kind: "message", T: () => Option, repeated: true },
43
+ {
44
+ no: 6,
45
+ name: "options",
46
+ kind: "message",
47
+ T: () => Option,
48
+ repeated: true,
49
+ },
44
50
  { no: 7, name: "syntax", kind: "enum", T: Syntax_Enum },
45
51
  ],
46
52
  packedByDefault: true,
@@ -59,8 +65,20 @@ export const Api = createMessageType({
59
65
  typeName: "google.protobuf.Api",
60
66
  fields: [
61
67
  { no: 1, name: "name", kind: "scalar", T: ScalarType.STRING },
62
- { no: 2, name: "methods", kind: "message", T: () => Method, repeated: true },
63
- { no: 3, name: "options", kind: "message", T: () => Option, repeated: true },
68
+ {
69
+ no: 2,
70
+ name: "methods",
71
+ kind: "message",
72
+ T: () => Method,
73
+ repeated: true,
74
+ },
75
+ {
76
+ no: 3,
77
+ name: "options",
78
+ kind: "message",
79
+ T: () => Option,
80
+ repeated: true,
81
+ },
64
82
  { no: 4, name: "version", kind: "scalar", T: ScalarType.STRING },
65
83
  { no: 5, name: "source_context", kind: "message", T: () => SourceContext },
66
84
  { no: 6, name: "mixins", kind: "message", T: () => Mixin, repeated: true },