@bufbuild/protobuf 1.4.2 → 1.5.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.
@@ -1,5 +1,4 @@
1
- import type { FieldDescriptorProto } from "./google/protobuf/descriptor_pb.js";
2
- import { FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
1
+ import { FeatureSetDefaults, FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
3
2
  import type { DescriptorSet } from "./descriptor-set.js";
4
3
  /**
5
4
  * Create a DescriptorSet, a convenient interface for working with a set of
@@ -9,8 +8,23 @@ import type { DescriptorSet } from "./descriptor-set.js";
9
8
  * before any file that imports it. Protocol buffer compilers always produce
10
9
  * files in topological order.
11
10
  */
12
- export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array): DescriptorSet;
11
+ export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array, options?: CreateDescriptorSetOptions): DescriptorSet;
13
12
  /**
14
- * Get the default `packed` state of a repeated field.
13
+ * Options to createDescriptorSet()
15
14
  */
16
- export declare function isPackedFieldByDefault(proto: FieldDescriptorProto, syntax: "proto2" | "proto3"): boolean;
15
+ interface CreateDescriptorSetOptions {
16
+ /**
17
+ * Editions support language-specific features with extensions to
18
+ * google.protobuf.FeatureSet. They can define defaults, and specify on
19
+ * which targets the features can be set.
20
+ *
21
+ * To create a DescriptorSet that provides your language-specific features,
22
+ * you have to provide a google.protobuf.FeatureSetDefaults message in this
23
+ * option.
24
+ *
25
+ * The defaults can be generated with `protoc` - see the flag
26
+ * `--experimental_edition_defaults_out`.
27
+ */
28
+ featureSetDefaults?: FeatureSetDefaults;
29
+ }
30
+ export {};
@@ -13,13 +13,14 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.isPackedFieldByDefault = exports.createDescriptorSet = void 0;
16
+ exports.createDescriptorSet = void 0;
17
17
  const descriptor_pb_js_1 = require("./google/protobuf/descriptor_pb.js");
18
18
  const assert_js_1 = require("./private/assert.js");
19
19
  const field_js_1 = require("./field.js");
20
20
  const service_type_js_1 = require("./service-type.js");
21
21
  const names_js_1 = require("./private/names.js");
22
- const proto_int64_js_1 = require("./proto-int64.js");
22
+ const text_format_js_1 = require("./private/text-format.js");
23
+ const feature_set_js_1 = require("./private/feature-set.js");
23
24
  /**
24
25
  * Create a DescriptorSet, a convenient interface for working with a set of
25
26
  * google.protobuf.FileDescriptorProto.
@@ -28,13 +29,15 @@ const proto_int64_js_1 = require("./proto-int64.js");
28
29
  * before any file that imports it. Protocol buffer compilers always produce
29
30
  * files in topological order.
30
31
  */
31
- function createDescriptorSet(input) {
32
+ function createDescriptorSet(input, options) {
33
+ var _a;
32
34
  const cart = {
33
35
  enums: new Map(),
34
36
  messages: new Map(),
35
37
  services: new Map(),
36
38
  extensions: new Map(),
37
39
  mapEntries: new Map(),
40
+ resolveFeatures: (0, feature_set_js_1.createFeatureResolver)((_a = options === null || options === void 0 ? void 0 : options.featureSetDefaults) !== null && _a !== void 0 ? _a : feature_set_js_1.featureSetDefaults),
38
41
  };
39
42
  const fileDescriptors = input instanceof descriptor_pb_js_1.FileDescriptorSet
40
43
  ? input.file
@@ -49,20 +52,9 @@ exports.createDescriptorSet = createDescriptorSet;
49
52
  * Create a descriptor for a file.
50
53
  */
51
54
  function newFile(proto, cart) {
52
- var _a, _b, _c;
55
+ var _a, _b;
53
56
  (0, assert_js_1.assert)(proto.name, `invalid FileDescriptorProto: missing name`);
54
- (0, assert_js_1.assert)(proto.syntax === undefined || proto.syntax === "proto3", `invalid FileDescriptorProto: unsupported syntax: ${(_a = proto.syntax) !== null && _a !== void 0 ? _a : "undefined"}`);
55
- const file = {
56
- kind: "file",
57
- proto,
58
- deprecated: (_c = (_b = proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) !== null && _c !== void 0 ? _c : false,
59
- syntax: proto.syntax === "proto3" ? "proto3" : "proto2",
60
- name: proto.name.replace(/\.proto/, ""),
61
- enums: [],
62
- messages: [],
63
- extensions: [],
64
- services: [],
65
- toString() {
57
+ const file = Object.assign(Object.assign({ kind: "file", proto, deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false }, parseFileSyntax(proto.syntax, proto.edition)), { name: proto.name.replace(/\.proto/, ""), enums: [], messages: [], extensions: [], services: [], toString() {
66
58
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
67
59
  return `file ${this.proto.name}`;
68
60
  },
@@ -76,7 +68,10 @@ function newFile(proto, cart) {
76
68
  FieldNumber.FileDescriptorProto_Package,
77
69
  ]);
78
70
  },
79
- };
71
+ getFeatures() {
72
+ var _a;
73
+ return cart.resolveFeatures(this.edition, (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
74
+ } });
80
75
  cart.mapEntries.clear(); // map entries are local to the file, we can safely discard
81
76
  for (const enumProto of proto.enumType) {
82
77
  addEnum(enumProto, file, undefined, cart);
@@ -129,7 +124,7 @@ function addExtensions(desc, cart) {
129
124
  * Recurses into nested types.
130
125
  */
131
126
  function addFields(message, cart) {
132
- const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message));
127
+ const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message, cart));
133
128
  const oneofsSeen = new Set();
134
129
  for (const proto of message.proto.field) {
135
130
  const oneof = findOneof(proto, allOneofs);
@@ -186,6 +181,11 @@ function addEnum(proto, file, parent, cart) {
186
181
  ];
187
182
  return findComments(file.proto.sourceCodeInfo, path);
188
183
  },
184
+ getFeatures() {
185
+ var _a, _b, _c;
186
+ const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures();
187
+ return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features);
188
+ },
189
189
  };
190
190
  cart.enums.set(desc.typeName, desc);
191
191
  proto.value.forEach((proto) => {
@@ -218,6 +218,10 @@ function addEnum(proto, file, parent, cart) {
218
218
  ];
219
219
  return findComments(file.proto.sourceCodeInfo, path);
220
220
  },
221
+ getFeatures() {
222
+ var _a;
223
+ return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
224
+ },
221
225
  });
222
226
  });
223
227
  ((_c = parent === null || parent === void 0 ? void 0 : parent.nestedEnums) !== null && _c !== void 0 ? _c : file.enums).push(desc);
@@ -259,6 +263,11 @@ function addMessage(proto, file, parent, cart) {
259
263
  ];
260
264
  return findComments(file.proto.sourceCodeInfo, path);
261
265
  },
266
+ getFeatures() {
267
+ var _a, _b, _c;
268
+ const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures();
269
+ return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features);
270
+ },
262
271
  };
263
272
  if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) {
264
273
  cart.mapEntries.set(desc.typeName, desc);
@@ -299,6 +308,10 @@ function addService(proto, file, cart) {
299
308
  ];
300
309
  return findComments(file.proto.sourceCodeInfo, path);
301
310
  },
311
+ getFeatures() {
312
+ var _a;
313
+ return cart.resolveFeatures(this.file.edition, this.file.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
314
+ },
302
315
  };
303
316
  file.services.push(desc);
304
317
  cart.services.set(desc.typeName, desc);
@@ -366,12 +379,16 @@ function newMethod(proto, parent, cart) {
366
379
  ];
367
380
  return findComments(parent.file.proto.sourceCodeInfo, path);
368
381
  },
382
+ getFeatures() {
383
+ var _a;
384
+ return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
385
+ },
369
386
  };
370
387
  }
371
388
  /**
372
389
  * Create a descriptor for a oneof group.
373
390
  */
374
- function newOneof(proto, parent) {
391
+ function newOneof(proto, parent, cart) {
375
392
  (0, assert_js_1.assert)(proto.name, `invalid OneofDescriptorProto: missing name`);
376
393
  return {
377
394
  kind: "oneof",
@@ -391,17 +408,20 @@ function newOneof(proto, parent) {
391
408
  ];
392
409
  return findComments(parent.file.proto.sourceCodeInfo, path);
393
410
  },
411
+ getFeatures() {
412
+ var _a;
413
+ return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
414
+ },
394
415
  };
395
416
  }
396
417
  /**
397
418
  * Create a descriptor for a field.
398
419
  */
399
420
  function newField(proto, file, parent, oneof, cart) {
400
- var _a, _b, _c, _d, _e;
421
+ var _a, _b, _c;
401
422
  (0, assert_js_1.assert)(proto.name, `invalid FieldDescriptorProto: missing name`);
402
423
  (0, assert_js_1.assert)(proto.number, `invalid FieldDescriptorProto: missing number`);
403
424
  (0, assert_js_1.assert)(proto.type, `invalid FieldDescriptorProto: missing type`);
404
- const packedByDefault = isPackedFieldByDefault(proto, file.syntax);
405
425
  const common = {
406
426
  proto,
407
427
  deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
@@ -410,8 +430,8 @@ function newField(proto, file, parent, oneof, cart) {
410
430
  parent,
411
431
  oneof,
412
432
  optional: isOptionalField(proto, file.syntax),
413
- packed: (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : packedByDefault,
414
- packedByDefault,
433
+ packedByDefault: isPackedFieldByDefault(file, proto, cart.resolveFeatures),
434
+ packed: isPackedField(file, parent, proto, cart.resolveFeatures),
415
435
  jsonName: proto.jsonName === (0, names_js_1.fieldJsonName)(proto.name) ? undefined : proto.jsonName,
416
436
  scalar: undefined,
417
437
  longType: undefined,
@@ -419,11 +439,11 @@ function newField(proto, file, parent, oneof, cart) {
419
439
  enum: undefined,
420
440
  mapKey: undefined,
421
441
  mapValue: undefined,
442
+ declarationString,
443
+ // toString, getComments, getFeatures are overridden in newExtension
422
444
  toString() {
423
- // note that newExtension() calls us with parent = null
424
445
  return `field ${this.parent.typeName}.${this.name}`;
425
446
  },
426
- declarationString,
427
447
  getComments() {
428
448
  const path = [
429
449
  ...this.parent.getComments().sourcePath,
@@ -432,6 +452,10 @@ function newField(proto, file, parent, oneof, cart) {
432
452
  ];
433
453
  return findComments(file.proto.sourceCodeInfo, path);
434
454
  },
455
+ getFeatures() {
456
+ var _a;
457
+ return cart.resolveFeatures(file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features);
458
+ },
435
459
  };
436
460
  const repeated = proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.REPEATED;
437
461
  switch (proto.type) {
@@ -460,7 +484,7 @@ function newField(proto, file, parent, oneof, cart) {
460
484
  (0, assert_js_1.assert)(scalar, `invalid FieldDescriptorProto: unknown type ${proto.type}`);
461
485
  return Object.assign(Object.assign({}, common), { kind: "field", fieldKind: "scalar", getDefaultValue,
462
486
  repeated,
463
- scalar, longType: ((_e = proto.options) === null || _e === void 0 ? void 0 : _e.jstype) == descriptor_pb_js_1.FieldOptions_JSType.JS_STRING
487
+ scalar, longType: ((_c = proto.options) === null || _c === void 0 ? void 0 : _c.jstype) == descriptor_pb_js_1.FieldOptions_JSType.JS_STRING
464
488
  ? field_js_1.LongType.STRING
465
489
  : field_js_1.LongType.BIGINT });
466
490
  }
@@ -478,6 +502,8 @@ function newExtension(proto, file, parent, cart) {
478
502
  return Object.assign(Object.assign({}, field), { kind: "extension", typeName: makeTypeName(proto, parent, file), parent,
479
503
  file,
480
504
  extendee,
505
+ // Must override toString, getComments, getFeatures from newField, because we
506
+ // call newField with parent undefined.
481
507
  toString() {
482
508
  return `extension ${this.typeName}`;
483
509
  },
@@ -493,8 +519,56 @@ function newExtension(proto, file, parent, cart) {
493
519
  this.file.proto.extension.indexOf(proto),
494
520
  ];
495
521
  return findComments(file.proto.sourceCodeInfo, path);
522
+ },
523
+ getFeatures() {
524
+ var _a, _b;
525
+ return cart.resolveFeatures(this.file.edition, ((_a = this.parent) !== null && _a !== void 0 ? _a : this.file).getFeatures(), (_b = this.proto.options) === null || _b === void 0 ? void 0 : _b.features);
496
526
  } });
497
527
  }
528
+ /**
529
+ * Parse the "syntax" and "edition" fields, stripping test editions.
530
+ */
531
+ function parseFileSyntax(syntax, edition) {
532
+ let e;
533
+ let s;
534
+ switch (syntax) {
535
+ case undefined:
536
+ case "proto2":
537
+ s = "proto2";
538
+ e = descriptor_pb_js_1.Edition.EDITION_PROTO2;
539
+ break;
540
+ case "proto3":
541
+ s = "proto3";
542
+ e = descriptor_pb_js_1.Edition.EDITION_PROTO3;
543
+ break;
544
+ case "editions":
545
+ s = "editions";
546
+ switch (edition) {
547
+ case undefined:
548
+ case descriptor_pb_js_1.Edition.EDITION_1_TEST_ONLY:
549
+ case descriptor_pb_js_1.Edition.EDITION_2_TEST_ONLY:
550
+ case descriptor_pb_js_1.Edition.EDITION_99997_TEST_ONLY:
551
+ case descriptor_pb_js_1.Edition.EDITION_99998_TEST_ONLY:
552
+ case descriptor_pb_js_1.Edition.EDITION_99999_TEST_ONLY:
553
+ case descriptor_pb_js_1.Edition.EDITION_UNKNOWN:
554
+ e = descriptor_pb_js_1.Edition.EDITION_UNKNOWN;
555
+ break;
556
+ default:
557
+ e = edition;
558
+ break;
559
+ }
560
+ break;
561
+ default:
562
+ throw new Error(`invalid FileDescriptorProto: unsupported syntax: ${syntax}`);
563
+ }
564
+ if (syntax === "editions" && edition === descriptor_pb_js_1.Edition.EDITION_UNKNOWN) {
565
+ throw new Error(`invalid FileDescriptorProto: syntax ${syntax} cannot have edition ${String(edition)}`);
566
+ }
567
+ return {
568
+ syntax: s,
569
+ edition: e,
570
+ };
571
+ }
498
572
  /**
499
573
  * Create a fully qualified name for a protobuf type or extension field.
500
574
  *
@@ -592,42 +666,66 @@ function isOptionalField(proto, syntax) {
592
666
  proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.OPTIONAL);
593
667
  case "proto3":
594
668
  return proto.proto3Optional === true;
669
+ case "editions":
670
+ return false;
595
671
  }
596
672
  }
597
673
  /**
598
- * Get the default `packed` state of a repeated field.
674
+ * Is this field packed by default? Only valid for repeated enum fields, and
675
+ * for repeated scalar fields except BYTES and STRING.
676
+ *
677
+ * In proto3 syntax, fields are packed by default. In proto2 syntax, fields
678
+ * are unpacked by default. With editions, the default is whatever the edition
679
+ * specifies as a default. In edition 2023, fields are packed by default.
599
680
  */
600
- function isPackedFieldByDefault(proto, syntax) {
601
- (0, assert_js_1.assert)(proto.type, `invalid FieldDescriptorProto: missing type`);
602
- if (syntax === "proto3") {
603
- switch (proto.type) {
604
- case descriptor_pb_js_1.FieldDescriptorProto_Type.DOUBLE:
605
- case descriptor_pb_js_1.FieldDescriptorProto_Type.FLOAT:
606
- case descriptor_pb_js_1.FieldDescriptorProto_Type.INT64:
607
- case descriptor_pb_js_1.FieldDescriptorProto_Type.UINT64:
608
- case descriptor_pb_js_1.FieldDescriptorProto_Type.INT32:
609
- case descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED64:
610
- case descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED32:
611
- case descriptor_pb_js_1.FieldDescriptorProto_Type.UINT32:
612
- case descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED32:
613
- case descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED64:
614
- case descriptor_pb_js_1.FieldDescriptorProto_Type.SINT32:
615
- case descriptor_pb_js_1.FieldDescriptorProto_Type.SINT64:
616
- case descriptor_pb_js_1.FieldDescriptorProto_Type.BOOL:
617
- case descriptor_pb_js_1.FieldDescriptorProto_Type.ENUM:
618
- // From the proto3 language guide:
619
- // > In proto3, repeated fields of scalar numeric types are packed by default.
620
- // This information is incomplete - according to the conformance tests, BOOL
621
- // and ENUM are packed by default as well. This means only STRING and BYTES
622
- // are not packed by default, which makes sense because they are length-delimited.
623
- return true;
624
- default:
625
- return false;
626
- }
681
+ function isPackedFieldByDefault(file, proto, resolveFeatures) {
682
+ const { repeatedFieldEncoding } = resolveFeatures(file.edition);
683
+ if (repeatedFieldEncoding != descriptor_pb_js_1.FeatureSet_RepeatedFieldEncoding.PACKED) {
684
+ return false;
685
+ }
686
+ // From the proto3 language guide:
687
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
688
+ // This information is incomplete - according to the conformance tests, BOOL
689
+ // and ENUM are packed by default as well. This means only STRING and BYTES
690
+ // are not packed by default, which makes sense because they are length-delimited.
691
+ switch (proto.type) {
692
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.STRING:
693
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.BYTES:
694
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP:
695
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.MESSAGE:
696
+ return false;
697
+ default:
698
+ return true;
699
+ }
700
+ }
701
+ /**
702
+ * Pack this repeated field?
703
+ *
704
+ * Respects field type, proto2/proto3 defaults and the `packed` option, or
705
+ * edition defaults and the edition features.repeated_field_encoding options.
706
+ */
707
+ function isPackedField(file, parent, proto, resolveFeatures) {
708
+ var _a, _b, _c, _d, _e, _f;
709
+ switch (proto.type) {
710
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.STRING:
711
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.BYTES:
712
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP:
713
+ case descriptor_pb_js_1.FieldDescriptorProto_Type.MESSAGE:
714
+ // length-delimited types cannot be packed
715
+ return false;
716
+ default:
717
+ switch (file.edition) {
718
+ case descriptor_pb_js_1.Edition.EDITION_PROTO2:
719
+ return (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.packed) !== null && _b !== void 0 ? _b : false;
720
+ case descriptor_pb_js_1.Edition.EDITION_PROTO3:
721
+ return (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : true;
722
+ default: {
723
+ const { repeatedFieldEncoding } = resolveFeatures(file.edition, (_e = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _e !== void 0 ? _e : file.getFeatures(), (_f = proto.options) === null || _f === void 0 ? void 0 : _f.features);
724
+ return (repeatedFieldEncoding == descriptor_pb_js_1.FeatureSet_RepeatedFieldEncoding.PACKED);
725
+ }
726
+ }
627
727
  }
628
- return false;
629
728
  }
630
- exports.isPackedFieldByDefault = isPackedFieldByDefault;
631
729
  /**
632
730
  * Map from a compiler-generated field type to our ScalarType, which is a
633
731
  * subset of field types declared by protobuf enum google.protobuf.FieldDescriptorProto.
@@ -784,175 +882,11 @@ function getDefaultValue() {
784
882
  return undefined;
785
883
  }
786
884
  switch (this.fieldKind) {
787
- case "enum": {
788
- const enumValue = this.enum.values.find((v) => v.name === d);
789
- (0, assert_js_1.assert)(enumValue, `cannot parse ${this.toString()} default value: ${d}`);
790
- return enumValue.number;
791
- }
885
+ case "enum":
886
+ return (0, text_format_js_1.parseTextFormatEnumValue)(this.enum, d);
792
887
  case "scalar":
793
- switch (this.scalar) {
794
- case field_js_1.ScalarType.STRING:
795
- return d;
796
- case field_js_1.ScalarType.BYTES: {
797
- const u = unescapeBytesDefaultValue(d);
798
- if (u === false) {
799
- throw new Error(`cannot parse ${this.toString()} default value: ${d}`);
800
- }
801
- return u;
802
- }
803
- case field_js_1.ScalarType.INT64:
804
- case field_js_1.ScalarType.SFIXED64:
805
- case field_js_1.ScalarType.SINT64:
806
- return proto_int64_js_1.protoInt64.parse(d);
807
- case field_js_1.ScalarType.UINT64:
808
- case field_js_1.ScalarType.FIXED64:
809
- return proto_int64_js_1.protoInt64.uParse(d);
810
- case field_js_1.ScalarType.DOUBLE:
811
- case field_js_1.ScalarType.FLOAT:
812
- switch (d) {
813
- case "inf":
814
- return Number.POSITIVE_INFINITY;
815
- case "-inf":
816
- return Number.NEGATIVE_INFINITY;
817
- case "nan":
818
- return Number.NaN;
819
- default:
820
- return parseFloat(d);
821
- }
822
- case field_js_1.ScalarType.BOOL:
823
- return d === "true";
824
- case field_js_1.ScalarType.INT32:
825
- case field_js_1.ScalarType.UINT32:
826
- case field_js_1.ScalarType.SINT32:
827
- case field_js_1.ScalarType.FIXED32:
828
- case field_js_1.ScalarType.SFIXED32:
829
- return parseInt(d, 10);
830
- }
831
- break;
888
+ return (0, text_format_js_1.parseTextFormatScalarValue)(this.scalar, d);
832
889
  default:
833
890
  return undefined;
834
891
  }
835
892
  }
836
- /**
837
- * Parses a text-encoded default value (proto2) of a BYTES field.
838
- */
839
- function unescapeBytesDefaultValue(str) {
840
- const b = [];
841
- const input = {
842
- tail: str,
843
- c: "",
844
- next() {
845
- if (this.tail.length == 0) {
846
- return false;
847
- }
848
- this.c = this.tail[0];
849
- this.tail = this.tail.substring(1);
850
- return true;
851
- },
852
- take(n) {
853
- if (this.tail.length >= n) {
854
- const r = this.tail.substring(0, n);
855
- this.tail = this.tail.substring(n);
856
- return r;
857
- }
858
- return false;
859
- },
860
- };
861
- while (input.next()) {
862
- switch (input.c) {
863
- case "\\":
864
- if (input.next()) {
865
- switch (input.c) {
866
- case "\\":
867
- b.push(input.c.charCodeAt(0));
868
- break;
869
- case "b":
870
- b.push(0x08);
871
- break;
872
- case "f":
873
- b.push(0x0c);
874
- break;
875
- case "n":
876
- b.push(0x0a);
877
- break;
878
- case "r":
879
- b.push(0x0d);
880
- break;
881
- case "t":
882
- b.push(0x09);
883
- break;
884
- case "v":
885
- b.push(0x0b);
886
- break;
887
- case "0":
888
- case "1":
889
- case "2":
890
- case "3":
891
- case "4":
892
- case "5":
893
- case "6":
894
- case "7": {
895
- const s = input.c;
896
- const t = input.take(2);
897
- if (t === false) {
898
- return false;
899
- }
900
- const n = parseInt(s + t, 8);
901
- if (isNaN(n)) {
902
- return false;
903
- }
904
- b.push(n);
905
- break;
906
- }
907
- case "x": {
908
- const s = input.c;
909
- const t = input.take(2);
910
- if (t === false) {
911
- return false;
912
- }
913
- const n = parseInt(s + t, 16);
914
- if (isNaN(n)) {
915
- return false;
916
- }
917
- b.push(n);
918
- break;
919
- }
920
- case "u": {
921
- const s = input.c;
922
- const t = input.take(4);
923
- if (t === false) {
924
- return false;
925
- }
926
- const n = parseInt(s + t, 16);
927
- if (isNaN(n)) {
928
- return false;
929
- }
930
- const chunk = new Uint8Array(4);
931
- const view = new DataView(chunk.buffer);
932
- view.setInt32(0, n, true);
933
- b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
934
- break;
935
- }
936
- case "U": {
937
- const s = input.c;
938
- const t = input.take(8);
939
- if (t === false) {
940
- return false;
941
- }
942
- const tc = proto_int64_js_1.protoInt64.uEnc(s + t);
943
- const chunk = new Uint8Array(8);
944
- const view = new DataView(chunk.buffer);
945
- view.setInt32(0, tc.lo, true);
946
- view.setInt32(4, tc.hi, true);
947
- b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
948
- break;
949
- }
950
- }
951
- }
952
- break;
953
- default:
954
- b.push(input.c.charCodeAt(0));
955
- }
956
- }
957
- return new Uint8Array(b);
958
- }