@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.2-alpha.2

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 (138) hide show
  1. package/dist/cjs/binary-encoding.js +414 -409
  2. package/dist/cjs/binary-format.js +13 -0
  3. package/dist/cjs/descriptor-registry.js +420 -449
  4. package/dist/cjs/descriptor-set.js +429 -513
  5. package/dist/cjs/enum.js +13 -0
  6. package/dist/cjs/field-list.js +13 -0
  7. package/dist/cjs/field.js +43 -30
  8. package/dist/cjs/google/protobuf/any_pb.js +122 -140
  9. package/dist/cjs/google/protobuf/api_pb.js +177 -224
  10. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
  11. package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
  12. package/dist/cjs/google/protobuf/duration_pb.js +72 -88
  13. package/dist/cjs/google/protobuf/empty_pb.js +17 -17
  14. package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
  15. package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
  16. package/dist/cjs/google/protobuf/struct_pb.js +144 -181
  17. package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
  18. package/dist/cjs/google/protobuf/type_pb.js +406 -462
  19. package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
  20. package/dist/cjs/google/varint.js +166 -171
  21. package/dist/cjs/index.js +41 -122
  22. package/dist/cjs/json-format.js +13 -0
  23. package/dist/cjs/message-type.js +13 -0
  24. package/dist/cjs/message.js +89 -85
  25. package/dist/cjs/private/assert.js +33 -26
  26. package/dist/cjs/private/base64.js +93 -79
  27. package/dist/cjs/private/binary-format-common.js +208 -208
  28. package/dist/cjs/private/binary-format-proto2.js +88 -109
  29. package/dist/cjs/private/binary-format-proto3.js +70 -90
  30. package/dist/cjs/private/enum.js +77 -44
  31. package/dist/cjs/private/field-list.js +64 -50
  32. package/dist/cjs/private/field-wrapper.js +21 -10
  33. package/dist/cjs/private/field.js +35 -25
  34. package/dist/cjs/private/json-format-common.js +427 -440
  35. package/dist/cjs/private/json-format-proto2.js +91 -89
  36. package/dist/cjs/private/json-format-proto3.js +82 -93
  37. package/dist/cjs/private/message-type.js +39 -27
  38. package/dist/cjs/private/names.js +71 -62
  39. package/dist/cjs/private/options-map.js +13 -0
  40. package/dist/cjs/private/proto-runtime.js +25 -16
  41. package/dist/cjs/private/scalars.js +113 -103
  42. package/dist/cjs/private/util-common.js +193 -210
  43. package/dist/cjs/private/util.js +13 -0
  44. package/dist/cjs/proto-int64.js +123 -111
  45. package/dist/cjs/proto2.js +63 -63
  46. package/dist/cjs/proto3.js +74 -74
  47. package/dist/cjs/service-type.js +27 -17
  48. package/dist/cjs/type-registry.js +46 -31
  49. package/dist/esm/binary-encoding.js +414 -414
  50. package/dist/esm/binary-format.js +13 -0
  51. package/dist/esm/descriptor-registry.js +422 -458
  52. package/dist/esm/descriptor-set.js +430 -478
  53. package/dist/esm/enum.js +13 -0
  54. package/dist/esm/field-list.js +13 -0
  55. package/dist/esm/field.js +42 -29
  56. package/dist/esm/google/protobuf/any_pb.js +122 -140
  57. package/dist/esm/google/protobuf/api_pb.js +177 -197
  58. package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
  59. package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
  60. package/dist/esm/google/protobuf/duration_pb.js +72 -88
  61. package/dist/esm/google/protobuf/empty_pb.js +17 -17
  62. package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
  63. package/dist/esm/google/protobuf/source_context_pb.js +25 -25
  64. package/dist/esm/google/protobuf/struct_pb.js +143 -180
  65. package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
  66. package/dist/esm/google/protobuf/type_pb.js +401 -421
  67. package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
  68. package/dist/esm/google/varint.js +165 -164
  69. package/dist/esm/index.js +14 -1
  70. package/dist/esm/json-format.js +13 -0
  71. package/dist/esm/message-type.js +13 -0
  72. package/dist/esm/message.js +89 -85
  73. package/dist/esm/private/assert.js +32 -21
  74. package/dist/esm/private/base64.js +92 -78
  75. package/dist/esm/private/binary-format-common.js +209 -207
  76. package/dist/esm/private/binary-format-proto2.js +89 -81
  77. package/dist/esm/private/binary-format-proto3.js +71 -67
  78. package/dist/esm/private/enum.js +75 -43
  79. package/dist/esm/private/field-list.js +64 -50
  80. package/dist/esm/private/field-wrapper.js +21 -10
  81. package/dist/esm/private/field.js +35 -22
  82. package/dist/esm/private/json-format-common.js +427 -435
  83. package/dist/esm/private/json-format-proto2.js +89 -81
  84. package/dist/esm/private/json-format-proto3.js +79 -80
  85. package/dist/esm/private/message-type.js +40 -28
  86. package/dist/esm/private/names.js +70 -57
  87. package/dist/esm/private/options-map.js +13 -0
  88. package/dist/esm/private/proto-runtime.js +26 -12
  89. package/dist/esm/private/scalars.js +112 -99
  90. package/dist/esm/private/util-common.js +193 -192
  91. package/dist/esm/private/util.js +13 -0
  92. package/dist/esm/proto-int64.js +123 -111
  93. package/dist/esm/proto2.js +63 -57
  94. package/dist/esm/proto3.js +74 -68
  95. package/dist/esm/service-type.js +25 -12
  96. package/dist/esm/type-registry.js +46 -31
  97. package/dist/types/binary-encoding.d.ts +398 -398
  98. package/dist/types/binary-format.d.ts +69 -91
  99. package/dist/types/descriptor-registry.d.ts +23 -29
  100. package/dist/types/descriptor-set.d.ts +107 -128
  101. package/dist/types/enum.d.ts +21 -21
  102. package/dist/types/field-list.d.ts +21 -21
  103. package/dist/types/field.d.ts +191 -231
  104. package/dist/types/google/protobuf/any_pb.d.ts +56 -77
  105. package/dist/types/google/protobuf/api_pb.d.ts +144 -187
  106. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
  107. package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
  108. package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
  109. package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
  110. package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
  111. package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
  112. package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
  113. package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
  114. package/dist/types/google/protobuf/type_pb.d.ts +355 -422
  115. package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
  116. package/dist/types/google/varint.d.ts +6 -13
  117. package/dist/types/index.d.ts +4 -27
  118. package/dist/types/json-format.d.ts +70 -89
  119. package/dist/types/message-type.d.ts +37 -45
  120. package/dist/types/message.d.ts +62 -87
  121. package/dist/types/private/assert.d.ts +1 -4
  122. package/dist/types/private/binary-format-common.d.ts +6 -32
  123. package/dist/types/private/enum.d.ts +13 -11
  124. package/dist/types/private/field-list.d.ts +14 -21
  125. package/dist/types/private/field-wrapper.d.ts +4 -10
  126. package/dist/types/private/field.d.ts +12 -12
  127. package/dist/types/private/json-format-common.d.ts +5 -27
  128. package/dist/types/private/message-type.d.ts +12 -7
  129. package/dist/types/private/names.d.ts +1 -4
  130. package/dist/types/private/options-map.d.ts +1 -1
  131. package/dist/types/private/proto-runtime.d.ts +34 -42
  132. package/dist/types/private/scalars.d.ts +5 -12
  133. package/dist/types/private/util-common.d.ts +1 -4
  134. package/dist/types/private/util.d.ts +29 -41
  135. package/dist/types/proto-int64.d.ts +42 -42
  136. package/dist/types/service-type.d.ts +36 -56
  137. package/dist/types/type-registry.d.ts +13 -15
  138. package/package.json +2 -3
@@ -1,74 +1,78 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  import { ScalarType } from "../field.js";
2
- import {
3
- makeBinaryFormatCommon,
4
- writeMapEntry,
5
- writeMessageField,
6
- writePacked,
7
- writeScalar,
8
- } from "./binary-format-common.js";
15
+ import { makeBinaryFormatCommon, writeMapEntry, writeMessageField, writePacked, writeScalar, } from "./binary-format-common.js";
9
16
  /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, prefer-const, no-case-declarations */
10
17
  export function makeBinaryFormatProto3() {
11
- return {
12
- ...makeBinaryFormatCommon(),
13
- writeMessage(message, writer, options) {
14
- const type = message.getType();
15
- for (const field of type.fields.byNumber()) {
16
- let value, // this will be our field value, whether it is member of a oneof or regular field
17
- repeated = field.repeated,
18
- localName = field.localName;
19
- if (field.oneof) {
20
- const oneof = message[field.oneof.localName];
21
- if (oneof.case !== localName) {
22
- continue; // field is not selected, skip
23
- }
24
- value = oneof.value;
25
- } else {
26
- value = message[localName];
27
- }
28
- switch (field.kind) {
29
- case "scalar":
30
- case "enum":
31
- let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
32
- if (repeated) {
33
- if (field.packed) {
34
- writePacked(writer, scalarType, field.no, value);
35
- } else {
36
- for (const item of value) {
37
- writeScalar(writer, scalarType, field.no, item, true);
18
+ return {
19
+ ...makeBinaryFormatCommon(),
20
+ writeMessage(message, writer, options) {
21
+ const type = message.getType();
22
+ for (const field of type.fields.byNumber()) {
23
+ let value, // this will be our field value, whether it is member of a oneof or regular field
24
+ repeated = field.repeated, localName = field.localName;
25
+ if (field.oneof) {
26
+ const oneof = message[field.oneof.localName];
27
+ if (oneof.case !== localName) {
28
+ continue; // field is not selected, skip
29
+ }
30
+ value = oneof.value;
31
+ }
32
+ else {
33
+ value = message[localName];
34
+ }
35
+ switch (field.kind) {
36
+ case "scalar":
37
+ case "enum":
38
+ let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
39
+ if (repeated) {
40
+ if (field.packed) {
41
+ writePacked(writer, scalarType, field.no, value);
42
+ }
43
+ else {
44
+ for (const item of value) {
45
+ writeScalar(writer, scalarType, field.no, item, true);
46
+ }
47
+ }
48
+ }
49
+ else {
50
+ if (value !== undefined) {
51
+ writeScalar(writer, scalarType, field.no, value, !!field.oneof || field.opt);
52
+ }
53
+ }
54
+ break;
55
+ case "message":
56
+ if (repeated) {
57
+ for (const item of value) {
58
+ writeMessageField(writer, options, field.T, field.no, item);
59
+ }
60
+ }
61
+ else {
62
+ writeMessageField(writer, options, field.T, field.no, value);
63
+ }
64
+ break;
65
+ case "map":
66
+ for (const [key, val] of Object.entries(value)) {
67
+ writeMapEntry(writer, options, field, key, val);
68
+ }
69
+ break;
38
70
  }
39
- }
40
- } else {
41
- if (value !== undefined) {
42
- writeScalar(
43
- writer,
44
- scalarType,
45
- field.no,
46
- value,
47
- !!field.oneof || field.opt
48
- );
49
- }
50
- }
51
- break;
52
- case "message":
53
- if (repeated) {
54
- for (const item of value) {
55
- writeMessageField(writer, options, field.T, field.no, item);
56
- }
57
- } else {
58
- writeMessageField(writer, options, field.T, field.no, value);
59
71
  }
60
- break;
61
- case "map":
62
- for (const [key, val] of Object.entries(value)) {
63
- writeMapEntry(writer, options, field, key, val);
72
+ if (options.writeUnknownFields) {
73
+ this.writeUnknownFields(message, writer);
64
74
  }
65
- break;
66
- }
67
- }
68
- if (options.writeUnknownFields) {
69
- this.writeUnknownFields(message, writer);
70
- }
71
- return writer;
72
- },
73
- };
75
+ return writer;
76
+ },
77
+ };
74
78
  }
@@ -1,3 +1,16 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  import { assert } from "./assert.js";
2
15
  const enumTypeSymbol = Symbol("@bufbuild/protobuf/enum-type");
3
16
  /**
@@ -6,59 +19,78 @@ const enumTypeSymbol = Symbol("@bufbuild/protobuf/enum-type");
6
19
  * enum, it raises an error.
7
20
  */
8
21
  export function getEnumType(enumObject) {
9
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any
10
- const t = enumObject[enumTypeSymbol];
11
- assert(t, "missing enum type on enum object");
12
- return t; // eslint-disable-line @typescript-eslint/no-unsafe-return
22
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any
23
+ const t = enumObject[enumTypeSymbol];
24
+ assert(t, "missing enum type on enum object");
25
+ return t; // eslint-disable-line @typescript-eslint/no-unsafe-return
13
26
  }
14
27
  /**
15
28
  * Sets reflection information on a generated enum.
16
29
  */
17
- export function setEnumType(
18
- enumObject,
19
- typeName,
20
- values
21
- // We do not surface options at this time
22
- // opt?: {
23
- // options?: { readonly [extensionName: string]: JsonValue };
24
- // },
30
+ export function setEnumType(enumObject, typeName, values
31
+ // We do not surface options at this time
32
+ // opt?: {
33
+ // options?: { readonly [extensionName: string]: JsonValue };
34
+ // },
25
35
  ) {
26
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
27
- enumObject[enumTypeSymbol] = makeEnumType(typeName, values);
36
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
37
+ enumObject[enumTypeSymbol] = makeEnumType(typeName, values);
28
38
  }
29
39
  // We do not surface options at this time
30
40
  // export type PartialEnumValue = Omit<EnumValueInfo, 'options'> & Partial<Pick<EnumValueInfo, "options">>;
31
41
  /**
32
42
  * Create a new EnumType with the given values.
33
43
  */
34
- export function makeEnumType(
35
- typeName,
36
- values
37
- // We do not surface options at this time
38
- // opt?: {
39
- // options?: { readonly [extensionName: string]: JsonValue };
40
- // },
44
+ export function makeEnumType(typeName, values
45
+ // We do not surface options at this time
46
+ // opt?: {
47
+ // options?: { readonly [extensionName: string]: JsonValue };
48
+ // },
41
49
  ) {
42
- const names = Object.create(null);
43
- const numbers = Object.create(null);
44
- const normalValues = [];
45
- for (const value of values) {
46
- // We do not surface options at this time
47
- // const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
48
- normalValues.push(value);
49
- names[value.name] = value;
50
- numbers[value.no] = value;
51
- }
52
- return {
53
- typeName,
54
- values: normalValues,
55
- // We do not surface options at this time
56
- // options: opt?.options ?? Object.create(null),
57
- findName(name) {
58
- return names[name];
59
- },
60
- findNumber(no) {
61
- return numbers[no];
62
- },
63
- };
50
+ const names = Object.create(null);
51
+ const numbers = Object.create(null);
52
+ const normalValues = [];
53
+ for (const value of values) {
54
+ // We do not surface options at this time
55
+ // const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
56
+ normalValues.push(value);
57
+ names[value.name] = value;
58
+ numbers[value.no] = value;
59
+ }
60
+ return {
61
+ typeName,
62
+ values: normalValues,
63
+ // We do not surface options at this time
64
+ // options: opt?.options ?? Object.create(null),
65
+ findName(name) {
66
+ return names[name];
67
+ },
68
+ findNumber(no) {
69
+ return numbers[no];
70
+ },
71
+ };
72
+ }
73
+ /**
74
+ * Create a new enum object with the given values.
75
+ */
76
+ export function makeEnum(typeName, values, opt) {
77
+ const enumObject = {};
78
+ for (const value of values) {
79
+ const name = makeEnumValueName(value, opt?.sharedPrefix);
80
+ enumObject[name] = value.no;
81
+ enumObject[value.no] = name;
82
+ }
83
+ setEnumType(enumObject, typeName, values);
84
+ return enumObject;
85
+ }
86
+ // Construct a local name for an enum value.
87
+ // This logic matches the Go function with the same name in private/protoplugin/names.go
88
+ function makeEnumValueName(value, sharedPrefix) {
89
+ if (sharedPrefix === undefined) {
90
+ return value.name;
91
+ }
92
+ if (!value.name.startsWith(sharedPrefix)) {
93
+ return value.name;
94
+ }
95
+ return value.name.substring(sharedPrefix.length);
64
96
  }
@@ -1,58 +1,72 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  export class InternalFieldList {
2
- constructor(fields, normalizer) {
3
- this._fields = fields;
4
- this._normalizer = normalizer;
5
- }
6
- findJsonName(jsonName) {
7
- if (!this.jsonNames) {
8
- const t = {};
9
- for (const f of this.list()) {
10
- t[f.jsonName] = t[f.name] = f;
11
- }
12
- this.jsonNames = t;
15
+ constructor(fields, normalizer) {
16
+ this._fields = fields;
17
+ this._normalizer = normalizer;
13
18
  }
14
- return this.jsonNames[jsonName];
15
- }
16
- find(fieldNo) {
17
- if (!this.numbers) {
18
- const t = {};
19
- for (const f of this.list()) {
20
- t[f.no] = f;
21
- }
22
- this.numbers = t;
19
+ findJsonName(jsonName) {
20
+ if (!this.jsonNames) {
21
+ const t = {};
22
+ for (const f of this.list()) {
23
+ t[f.jsonName] = t[f.name] = f;
24
+ }
25
+ this.jsonNames = t;
26
+ }
27
+ return this.jsonNames[jsonName];
23
28
  }
24
- return this.numbers[fieldNo];
25
- }
26
- list() {
27
- if (!this.all) {
28
- this.all = this._normalizer(this._fields);
29
+ find(fieldNo) {
30
+ if (!this.numbers) {
31
+ const t = {};
32
+ for (const f of this.list()) {
33
+ t[f.no] = f;
34
+ }
35
+ this.numbers = t;
36
+ }
37
+ return this.numbers[fieldNo];
29
38
  }
30
- return this.all;
31
- }
32
- byNumber() {
33
- if (!this.numbersAsc) {
34
- this.numbersAsc = this.list()
35
- .concat()
36
- .sort((a, b) => a.no - b.no);
39
+ list() {
40
+ if (!this.all) {
41
+ this.all = this._normalizer(this._fields);
42
+ }
43
+ return this.all;
44
+ }
45
+ byNumber() {
46
+ if (!this.numbersAsc) {
47
+ this.numbersAsc = this.list()
48
+ .concat()
49
+ .sort((a, b) => a.no - b.no);
50
+ }
51
+ return this.numbersAsc;
37
52
  }
38
- return this.numbersAsc;
39
- }
40
- byMember() {
41
- if (!this.members) {
42
- this.members = [];
43
- const a = this.members;
44
- let o;
45
- for (const f of this.list()) {
46
- if (f.oneof) {
47
- if (f.oneof !== o) {
48
- o = f.oneof;
49
- a.push(o);
50
- }
51
- } else {
52
- a.push(f);
53
+ byMember() {
54
+ if (!this.members) {
55
+ this.members = [];
56
+ const a = this.members;
57
+ let o;
58
+ for (const f of this.list()) {
59
+ if (f.oneof) {
60
+ if (f.oneof !== o) {
61
+ o = f.oneof;
62
+ a.push(o);
63
+ }
64
+ }
65
+ else {
66
+ a.push(f);
67
+ }
68
+ }
53
69
  }
54
- }
70
+ return this.members;
55
71
  }
56
- return this.members;
57
- }
58
72
  }
@@ -1,20 +1,31 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  /**
2
15
  * Wrap field values whose message type has a wrapper.
3
16
  */
4
17
  export function wrapField(type, value) {
5
- if (value instanceof type) {
6
- return value;
7
- }
8
- if (type.fieldWrapper) {
9
- return type.fieldWrapper.wrapField(value);
10
- }
11
- throw new Error(
12
- `cannot unwrap field value, ${type.typeName} does not define a field wrapper`
13
- );
18
+ if (value instanceof type) {
19
+ return value;
20
+ }
21
+ if (type.fieldWrapper) {
22
+ return type.fieldWrapper.wrapField(value);
23
+ }
24
+ throw new Error(`cannot unwrap field value, ${type.typeName} does not define a field wrapper`);
14
25
  }
15
26
  /**
16
27
  * Unwrap field values whose message type has a wrapper.
17
28
  */
18
29
  export function unwrapField(type, value) {
19
- return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
30
+ return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
20
31
  }
@@ -1,27 +1,40 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  import { makeOneofName } from "./names.js";
2
15
  import { assert } from "./assert.js";
3
16
  export class InternalOneofInfo {
4
- constructor(name) {
5
- this.kind = "oneof";
6
- this.repeated = false;
7
- this.packed = false;
8
- this.opt = false;
9
- this.default = undefined;
10
- this.fields = [];
11
- this.name = name;
12
- this.localName = makeOneofName(name);
13
- }
14
- addField(field) {
15
- assert(field.oneof === this, `field ${field.name} not one of ${this.name}`);
16
- this.fields.push(field);
17
- }
18
- findField(localName) {
19
- if (!this._lookup) {
20
- this._lookup = Object.create(null);
21
- for (let i = 0; i < this.fields.length; i++) {
22
- this._lookup[this.fields[i].localName] = this.fields[i];
23
- }
17
+ constructor(name) {
18
+ this.kind = "oneof";
19
+ this.repeated = false;
20
+ this.packed = false;
21
+ this.opt = false;
22
+ this.default = undefined;
23
+ this.fields = [];
24
+ this.name = name;
25
+ this.localName = makeOneofName(name);
26
+ }
27
+ addField(field) {
28
+ assert(field.oneof === this, `field ${field.name} not one of ${this.name}`);
29
+ this.fields.push(field);
30
+ }
31
+ findField(localName) {
32
+ if (!this._lookup) {
33
+ this._lookup = Object.create(null);
34
+ for (let i = 0; i < this.fields.length; i++) {
35
+ this._lookup[this.fields[i].localName] = this.fields[i];
36
+ }
37
+ }
38
+ return this._lookup[localName];
24
39
  }
25
- return this._lookup[localName];
26
- }
27
40
  }