@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.
- package/dist/cjs/binary-encoding.js +414 -409
- package/dist/cjs/binary-format.js +13 -0
- package/dist/cjs/descriptor-registry.js +420 -449
- package/dist/cjs/descriptor-set.js +429 -513
- package/dist/cjs/enum.js +13 -0
- package/dist/cjs/field-list.js +13 -0
- package/dist/cjs/field.js +43 -30
- package/dist/cjs/google/protobuf/any_pb.js +122 -140
- package/dist/cjs/google/protobuf/api_pb.js +177 -224
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
- package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
- package/dist/cjs/google/protobuf/duration_pb.js +72 -88
- package/dist/cjs/google/protobuf/empty_pb.js +17 -17
- package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
- package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
- package/dist/cjs/google/protobuf/struct_pb.js +144 -181
- package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
- package/dist/cjs/google/protobuf/type_pb.js +406 -462
- package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
- package/dist/cjs/google/varint.js +166 -171
- package/dist/cjs/index.js +41 -122
- package/dist/cjs/json-format.js +13 -0
- package/dist/cjs/message-type.js +13 -0
- package/dist/cjs/message.js +89 -85
- package/dist/cjs/private/assert.js +33 -26
- package/dist/cjs/private/base64.js +93 -79
- package/dist/cjs/private/binary-format-common.js +208 -208
- package/dist/cjs/private/binary-format-proto2.js +88 -109
- package/dist/cjs/private/binary-format-proto3.js +70 -90
- package/dist/cjs/private/enum.js +77 -44
- package/dist/cjs/private/field-list.js +64 -50
- package/dist/cjs/private/field-wrapper.js +21 -10
- package/dist/cjs/private/field.js +35 -25
- package/dist/cjs/private/json-format-common.js +427 -440
- package/dist/cjs/private/json-format-proto2.js +91 -89
- package/dist/cjs/private/json-format-proto3.js +82 -93
- package/dist/cjs/private/message-type.js +39 -27
- package/dist/cjs/private/names.js +71 -62
- package/dist/cjs/private/options-map.js +13 -0
- package/dist/cjs/private/proto-runtime.js +25 -16
- package/dist/cjs/private/scalars.js +113 -103
- package/dist/cjs/private/util-common.js +193 -210
- package/dist/cjs/private/util.js +13 -0
- package/dist/cjs/proto-int64.js +123 -111
- package/dist/cjs/proto2.js +63 -63
- package/dist/cjs/proto3.js +74 -74
- package/dist/cjs/service-type.js +27 -17
- package/dist/cjs/type-registry.js +46 -31
- package/dist/esm/binary-encoding.js +414 -414
- package/dist/esm/binary-format.js +13 -0
- package/dist/esm/descriptor-registry.js +422 -458
- package/dist/esm/descriptor-set.js +430 -478
- package/dist/esm/enum.js +13 -0
- package/dist/esm/field-list.js +13 -0
- package/dist/esm/field.js +42 -29
- package/dist/esm/google/protobuf/any_pb.js +122 -140
- package/dist/esm/google/protobuf/api_pb.js +177 -197
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
- package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
- package/dist/esm/google/protobuf/duration_pb.js +72 -88
- package/dist/esm/google/protobuf/empty_pb.js +17 -17
- package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
- package/dist/esm/google/protobuf/source_context_pb.js +25 -25
- package/dist/esm/google/protobuf/struct_pb.js +143 -180
- package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
- package/dist/esm/google/protobuf/type_pb.js +401 -421
- package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
- package/dist/esm/google/varint.js +165 -164
- package/dist/esm/index.js +14 -1
- package/dist/esm/json-format.js +13 -0
- package/dist/esm/message-type.js +13 -0
- package/dist/esm/message.js +89 -85
- package/dist/esm/private/assert.js +32 -21
- package/dist/esm/private/base64.js +92 -78
- package/dist/esm/private/binary-format-common.js +209 -207
- package/dist/esm/private/binary-format-proto2.js +89 -81
- package/dist/esm/private/binary-format-proto3.js +71 -67
- package/dist/esm/private/enum.js +75 -43
- package/dist/esm/private/field-list.js +64 -50
- package/dist/esm/private/field-wrapper.js +21 -10
- package/dist/esm/private/field.js +35 -22
- package/dist/esm/private/json-format-common.js +427 -435
- package/dist/esm/private/json-format-proto2.js +89 -81
- package/dist/esm/private/json-format-proto3.js +79 -80
- package/dist/esm/private/message-type.js +40 -28
- package/dist/esm/private/names.js +70 -57
- package/dist/esm/private/options-map.js +13 -0
- package/dist/esm/private/proto-runtime.js +26 -12
- package/dist/esm/private/scalars.js +112 -99
- package/dist/esm/private/util-common.js +193 -192
- package/dist/esm/private/util.js +13 -0
- package/dist/esm/proto-int64.js +123 -111
- package/dist/esm/proto2.js +63 -57
- package/dist/esm/proto3.js +74 -68
- package/dist/esm/service-type.js +25 -12
- package/dist/esm/type-registry.js +46 -31
- package/dist/types/binary-encoding.d.ts +398 -398
- package/dist/types/binary-format.d.ts +69 -91
- package/dist/types/descriptor-registry.d.ts +23 -29
- package/dist/types/descriptor-set.d.ts +107 -128
- package/dist/types/enum.d.ts +21 -21
- package/dist/types/field-list.d.ts +21 -21
- package/dist/types/field.d.ts +191 -231
- package/dist/types/google/protobuf/any_pb.d.ts +56 -77
- package/dist/types/google/protobuf/api_pb.d.ts +144 -187
- package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
- package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
- package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
- package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
- package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
- package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
- package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
- package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
- package/dist/types/google/protobuf/type_pb.d.ts +355 -422
- package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
- package/dist/types/google/varint.d.ts +6 -13
- package/dist/types/index.d.ts +4 -27
- package/dist/types/json-format.d.ts +70 -89
- package/dist/types/message-type.d.ts +37 -45
- package/dist/types/message.d.ts +62 -87
- package/dist/types/private/assert.d.ts +1 -4
- package/dist/types/private/binary-format-common.d.ts +6 -32
- package/dist/types/private/enum.d.ts +13 -11
- package/dist/types/private/field-list.d.ts +14 -21
- package/dist/types/private/field-wrapper.d.ts +4 -10
- package/dist/types/private/field.d.ts +12 -12
- package/dist/types/private/json-format-common.d.ts +5 -27
- package/dist/types/private/message-type.d.ts +12 -7
- package/dist/types/private/names.d.ts +1 -4
- package/dist/types/private/options-map.d.ts +1 -1
- package/dist/types/private/proto-runtime.d.ts +34 -42
- package/dist/types/private/scalars.d.ts +5 -12
- package/dist/types/private/util-common.d.ts +1 -4
- package/dist/types/private/util.d.ts +29 -41
- package/dist/types/proto-int64.d.ts +42 -42
- package/dist/types/service-type.d.ts +36 -56
- package/dist/types/type-registry.d.ts +13 -15
- package/package.json +2 -3
|
@@ -1,90 +1,98 @@
|
|
|
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 { wrapField } from "./field-wrapper.js";
|
|
2
15
|
import { assert } from "./assert.js";
|
|
3
16
|
import { makeJsonFormatCommon } from "./json-format-common.js";
|
|
4
17
|
/* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
|
|
5
18
|
export function makeJsonFormatProto2() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
return makeJsonFormatCommon((writeEnum, writeScalar) => {
|
|
20
|
+
return function writeField(field, value, options) {
|
|
21
|
+
if (field.kind == "map") {
|
|
22
|
+
const jsonObj = {};
|
|
23
|
+
switch (field.V.kind) {
|
|
24
|
+
case "scalar":
|
|
25
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
26
|
+
const val = writeScalar(field.V.T, entryValue, true);
|
|
27
|
+
assert(val !== undefined);
|
|
28
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
case "message":
|
|
32
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
33
|
+
// JSON standard allows only (double quoted) string as property key
|
|
34
|
+
jsonObj[entryKey.toString()] = entryValue.toJson(options);
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
case "enum":
|
|
38
|
+
const enumType = field.V.T;
|
|
39
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
40
|
+
assert(entryValue === undefined || typeof entryValue == "number");
|
|
41
|
+
const val = writeEnum(enumType, entryValue, true, options.enumAsInteger);
|
|
42
|
+
assert(val !== undefined);
|
|
43
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
return options.emitDefaultValues || Object.keys(jsonObj).length > 0
|
|
48
|
+
? jsonObj
|
|
49
|
+
: undefined;
|
|
16
50
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
51
|
+
else if (field.repeated) {
|
|
52
|
+
const jsonArr = [];
|
|
53
|
+
switch (field.kind) {
|
|
54
|
+
case "scalar":
|
|
55
|
+
for (let i = 0; i < value.length; i++) {
|
|
56
|
+
jsonArr.push(writeScalar(field.T, value[i], true));
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case "enum":
|
|
60
|
+
for (let i = 0; i < value.length; i++) {
|
|
61
|
+
jsonArr.push(writeEnum(field.T, value[i], true, options.enumAsInteger));
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
case "message":
|
|
65
|
+
for (let i = 0; i < value.length; i++) {
|
|
66
|
+
jsonArr.push(value[i].toJson(options));
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
return options.emitDefaultValues || jsonArr.length > 0
|
|
71
|
+
? jsonArr
|
|
72
|
+
: undefined;
|
|
22
73
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
74
|
+
else {
|
|
75
|
+
// In contrast to proto3, we raise an error if a non-optional (proto2 required)
|
|
76
|
+
// field is missing a value.
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
if (!field.oneof && !field.opt) {
|
|
79
|
+
throw `required field not set`;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
switch (field.kind) {
|
|
84
|
+
case "scalar":
|
|
85
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
86
|
+
// Explicit default values are not special cased either.
|
|
87
|
+
return writeScalar(field.T, value, true);
|
|
88
|
+
case "enum":
|
|
89
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
90
|
+
// Explicit default values are not special cased either.
|
|
91
|
+
return writeEnum(field.T, value, true, options.enumAsInteger);
|
|
92
|
+
case "message":
|
|
93
|
+
return wrapField(field.T, value).toJson(options);
|
|
94
|
+
}
|
|
36
95
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return options.emitDefaultValues || Object.keys(jsonObj).length > 0
|
|
40
|
-
? jsonObj
|
|
41
|
-
: undefined;
|
|
42
|
-
} else if (field.repeated) {
|
|
43
|
-
const jsonArr = [];
|
|
44
|
-
switch (field.kind) {
|
|
45
|
-
case "scalar":
|
|
46
|
-
for (let i = 0; i < value.length; i++) {
|
|
47
|
-
jsonArr.push(writeScalar(field.T, value[i], true));
|
|
48
|
-
}
|
|
49
|
-
break;
|
|
50
|
-
case "enum":
|
|
51
|
-
for (let i = 0; i < value.length; i++) {
|
|
52
|
-
jsonArr.push(
|
|
53
|
-
writeEnum(field.T, value[i], true, options.enumAsInteger)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
break;
|
|
57
|
-
case "message":
|
|
58
|
-
for (let i = 0; i < value.length; i++) {
|
|
59
|
-
jsonArr.push(value[i].toJson(options));
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
return options.emitDefaultValues || jsonArr.length > 0
|
|
64
|
-
? jsonArr
|
|
65
|
-
: undefined;
|
|
66
|
-
} else {
|
|
67
|
-
// In contrast to proto3, we raise an error if a non-optional (proto2 required)
|
|
68
|
-
// field is missing a value.
|
|
69
|
-
if (value === undefined) {
|
|
70
|
-
if (!field.oneof && !field.opt) {
|
|
71
|
-
throw `required field not set`;
|
|
72
|
-
}
|
|
73
|
-
return undefined;
|
|
74
|
-
}
|
|
75
|
-
switch (field.kind) {
|
|
76
|
-
case "scalar":
|
|
77
|
-
// In contrast to proto3, we do not skip intrinsic default values.
|
|
78
|
-
// Explicit default values are not special cased either.
|
|
79
|
-
return writeScalar(field.T, value, true);
|
|
80
|
-
case "enum":
|
|
81
|
-
// In contrast to proto3, we do not skip intrinsic default values.
|
|
82
|
-
// Explicit default values are not special cased either.
|
|
83
|
-
return writeEnum(field.T, value, true, options.enumAsInteger);
|
|
84
|
-
case "message":
|
|
85
|
-
return wrapField(field.T, value).toJson(options);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
});
|
|
96
|
+
};
|
|
97
|
+
});
|
|
90
98
|
}
|
|
@@ -1,89 +1,88 @@
|
|
|
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 { wrapField } from "./field-wrapper.js";
|
|
2
15
|
import { assert } from "./assert.js";
|
|
3
16
|
import { makeJsonFormatCommon } from "./json-format-common.js";
|
|
4
17
|
/* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
|
|
5
18
|
export function makeJsonFormatProto3() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
return makeJsonFormatCommon((writeEnum, writeScalar) => {
|
|
20
|
+
return function writeField(field, value, options) {
|
|
21
|
+
if (field.kind == "map") {
|
|
22
|
+
const jsonObj = {};
|
|
23
|
+
switch (field.V.kind) {
|
|
24
|
+
case "scalar":
|
|
25
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
26
|
+
const val = writeScalar(field.V.T, entryValue, true);
|
|
27
|
+
assert(val !== undefined);
|
|
28
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
case "message":
|
|
32
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
33
|
+
// JSON standard allows only (double quoted) string as property key
|
|
34
|
+
jsonObj[entryKey.toString()] = entryValue.toJson(options);
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
case "enum":
|
|
38
|
+
const enumType = field.V.T;
|
|
39
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
40
|
+
assert(entryValue === undefined || typeof entryValue == "number");
|
|
41
|
+
const val = writeEnum(enumType, entryValue, true, options.enumAsInteger);
|
|
42
|
+
assert(val !== undefined);
|
|
43
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
return options.emitDefaultValues || Object.keys(jsonObj).length > 0
|
|
48
|
+
? jsonObj
|
|
49
|
+
: undefined;
|
|
16
50
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
51
|
+
else if (field.repeated) {
|
|
52
|
+
const jsonArr = [];
|
|
53
|
+
switch (field.kind) {
|
|
54
|
+
case "scalar":
|
|
55
|
+
for (let i = 0; i < value.length; i++) {
|
|
56
|
+
jsonArr.push(writeScalar(field.T, value[i], true));
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case "enum":
|
|
60
|
+
for (let i = 0; i < value.length; i++) {
|
|
61
|
+
jsonArr.push(writeEnum(field.T, value[i], true, options.enumAsInteger));
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
case "message":
|
|
65
|
+
for (let i = 0; i < value.length; i++) {
|
|
66
|
+
jsonArr.push(wrapField(field.T, value[i]).toJson(options));
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
return options.emitDefaultValues || jsonArr.length > 0
|
|
71
|
+
? jsonArr
|
|
72
|
+
: undefined;
|
|
22
73
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
assert(val !== undefined);
|
|
35
|
-
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
74
|
+
else {
|
|
75
|
+
switch (field.kind) {
|
|
76
|
+
case "scalar":
|
|
77
|
+
return writeScalar(field.T, value, !!field.oneof || field.opt || options.emitDefaultValues);
|
|
78
|
+
case "enum":
|
|
79
|
+
return writeEnum(field.T, value, !!field.oneof || field.opt || options.emitDefaultValues, options.enumAsInteger);
|
|
80
|
+
case "message":
|
|
81
|
+
return value !== undefined
|
|
82
|
+
? wrapField(field.T, value).toJson(options)
|
|
83
|
+
: undefined;
|
|
84
|
+
}
|
|
36
85
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return options.emitDefaultValues || Object.keys(jsonObj).length > 0
|
|
40
|
-
? jsonObj
|
|
41
|
-
: undefined;
|
|
42
|
-
} else if (field.repeated) {
|
|
43
|
-
const jsonArr = [];
|
|
44
|
-
switch (field.kind) {
|
|
45
|
-
case "scalar":
|
|
46
|
-
for (let i = 0; i < value.length; i++) {
|
|
47
|
-
jsonArr.push(writeScalar(field.T, value[i], true));
|
|
48
|
-
}
|
|
49
|
-
break;
|
|
50
|
-
case "enum":
|
|
51
|
-
for (let i = 0; i < value.length; i++) {
|
|
52
|
-
jsonArr.push(
|
|
53
|
-
writeEnum(field.T, value[i], true, options.enumAsInteger)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
break;
|
|
57
|
-
case "message":
|
|
58
|
-
for (let i = 0; i < value.length; i++) {
|
|
59
|
-
jsonArr.push(wrapField(field.T, value[i]).toJson(options));
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
return options.emitDefaultValues || jsonArr.length > 0
|
|
64
|
-
? jsonArr
|
|
65
|
-
: undefined;
|
|
66
|
-
} else {
|
|
67
|
-
switch (field.kind) {
|
|
68
|
-
case "scalar":
|
|
69
|
-
return writeScalar(
|
|
70
|
-
field.T,
|
|
71
|
-
value,
|
|
72
|
-
!!field.oneof || field.opt || options.emitDefaultValues
|
|
73
|
-
);
|
|
74
|
-
case "enum":
|
|
75
|
-
return writeEnum(
|
|
76
|
-
field.T,
|
|
77
|
-
value,
|
|
78
|
-
!!field.oneof || field.opt || options.emitDefaultValues,
|
|
79
|
-
options.enumAsInteger
|
|
80
|
-
);
|
|
81
|
-
case "message":
|
|
82
|
-
return value !== undefined
|
|
83
|
-
? wrapField(field.T, value).toJson(options)
|
|
84
|
-
: undefined;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
});
|
|
86
|
+
};
|
|
87
|
+
});
|
|
89
88
|
}
|
|
@@ -1,33 +1,45 @@
|
|
|
1
|
-
|
|
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.
|
|
14
|
+
import { Message, } from "../message.js";
|
|
2
15
|
/**
|
|
3
16
|
* Create a new message type using the given runtime.
|
|
4
17
|
*/
|
|
5
18
|
export function makeMessageType(runtime, typeName, fields, opt) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return type;
|
|
19
|
+
const localName = opt?.localName ?? typeName.substring(typeName.lastIndexOf(".") + 1);
|
|
20
|
+
const type = {
|
|
21
|
+
[localName]: function (data) {
|
|
22
|
+
runtime.util.initFields(this);
|
|
23
|
+
runtime.util.initPartial(data, this);
|
|
24
|
+
},
|
|
25
|
+
}[localName];
|
|
26
|
+
Object.setPrototypeOf(type.prototype, new Message());
|
|
27
|
+
Object.assign(type, {
|
|
28
|
+
runtime,
|
|
29
|
+
typeName,
|
|
30
|
+
fields: runtime.util.newFieldList(fields),
|
|
31
|
+
fromBinary(bytes, options) {
|
|
32
|
+
return new type().fromBinary(bytes, options);
|
|
33
|
+
},
|
|
34
|
+
fromJson(jsonValue, options) {
|
|
35
|
+
return new type().fromJson(jsonValue, options);
|
|
36
|
+
},
|
|
37
|
+
fromJsonString(jsonString, options) {
|
|
38
|
+
return new type().fromJsonString(jsonString, options);
|
|
39
|
+
},
|
|
40
|
+
equals(a, b) {
|
|
41
|
+
return runtime.util.equals(type, a, b);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
return type;
|
|
33
45
|
}
|
|
@@ -1,68 +1,81 @@
|
|
|
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
|
* Returns the JSON name for a protobuf field, exactly like protoc does.
|
|
3
16
|
*/
|
|
4
17
|
export function makeJsonName(protoName) {
|
|
5
|
-
|
|
18
|
+
return protoCamelCase(protoName);
|
|
6
19
|
}
|
|
7
20
|
/**
|
|
8
21
|
* Returns the local name of a field, exactly like protoc-gen-es does.
|
|
9
22
|
*/
|
|
10
23
|
export function makeFieldName(protoName, inOneof) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
const n = protoCamelCase(protoName);
|
|
25
|
+
if (inOneof) {
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
28
|
+
return rProp[n] ? n + escapeChar : n;
|
|
16
29
|
}
|
|
17
30
|
/**
|
|
18
31
|
* Returns the local name of a oneof group, exactly like protoc-gen-es does.
|
|
19
32
|
*/
|
|
20
33
|
export function makeOneofName(protoName) {
|
|
21
|
-
|
|
34
|
+
return makeFieldName(protoName, false);
|
|
22
35
|
}
|
|
23
36
|
/**
|
|
24
37
|
* Returns the local name of a rpc, exactly like protoc-gen-es does.
|
|
25
38
|
*/
|
|
26
39
|
export function makeMethodName(protoName) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
40
|
+
if (protoName.length == 0) {
|
|
41
|
+
return protoName;
|
|
42
|
+
}
|
|
43
|
+
return protoName[0].toLowerCase() + protoName.substring(1);
|
|
31
44
|
}
|
|
32
45
|
// Converts snake_case to protoCamelCase according to the convention
|
|
33
46
|
// used by protoc to convert a field name to a JSON name.
|
|
34
47
|
function protoCamelCase(snakeCase) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
48
|
+
let capNext = false;
|
|
49
|
+
const b = [];
|
|
50
|
+
for (let i = 0; i < snakeCase.length; i++) {
|
|
51
|
+
let c = snakeCase.charAt(i);
|
|
52
|
+
switch (c) {
|
|
53
|
+
case "_":
|
|
54
|
+
capNext = true;
|
|
55
|
+
break;
|
|
56
|
+
case "0":
|
|
57
|
+
case "1":
|
|
58
|
+
case "2":
|
|
59
|
+
case "3":
|
|
60
|
+
case "4":
|
|
61
|
+
case "5":
|
|
62
|
+
case "6":
|
|
63
|
+
case "7":
|
|
64
|
+
case "8":
|
|
65
|
+
case "9":
|
|
66
|
+
b.push(c);
|
|
67
|
+
capNext = false;
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
if (capNext) {
|
|
71
|
+
capNext = false;
|
|
72
|
+
c = c.toUpperCase();
|
|
73
|
+
}
|
|
74
|
+
b.push(c);
|
|
75
|
+
break;
|
|
60
76
|
}
|
|
61
|
-
b.push(c);
|
|
62
|
-
break;
|
|
63
77
|
}
|
|
64
|
-
|
|
65
|
-
return b.join("");
|
|
78
|
+
return b.join("");
|
|
66
79
|
}
|
|
67
80
|
// escapeChar must be appended to a reserved name.
|
|
68
81
|
// We choose '$' because it is invalid in proto identifiers.
|
|
@@ -70,21 +83,21 @@ const escapeChar = "$";
|
|
|
70
83
|
// Names that cannot be used for object properties.
|
|
71
84
|
// See buf_es/protoc-gen-es/internal/protoplugin/names.go
|
|
72
85
|
const rProp = {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
// names reserved by JavaScript
|
|
87
|
+
constructor: true,
|
|
88
|
+
toString: true,
|
|
89
|
+
toJSON: true,
|
|
90
|
+
valueOf: true,
|
|
91
|
+
// names reserved by the runtime
|
|
92
|
+
getType: true,
|
|
93
|
+
clone: true,
|
|
94
|
+
equals: true,
|
|
95
|
+
fromBinary: true,
|
|
96
|
+
fromJson: true,
|
|
97
|
+
fromJsonString: true,
|
|
98
|
+
toBinary: true,
|
|
99
|
+
toJson: true,
|
|
100
|
+
toJsonString: true,
|
|
101
|
+
// names reserved by the runtime for the future
|
|
102
|
+
toObject: true,
|
|
90
103
|
};
|
|
@@ -1 +1,14 @@
|
|
|
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 {};
|
|
@@ -1,15 +1,29 @@
|
|
|
1
|
-
|
|
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.
|
|
14
|
+
import { getEnumType, makeEnum, makeEnumType } from "./enum.js";
|
|
2
15
|
import { makeMessageType } from "./message-type.js";
|
|
3
16
|
export function makeProtoRuntime(syntax, json, bin, util) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
return {
|
|
18
|
+
syntax,
|
|
19
|
+
json,
|
|
20
|
+
bin,
|
|
21
|
+
util,
|
|
22
|
+
makeMessageType(typeName, fields, opt) {
|
|
23
|
+
return makeMessageType(this, typeName, fields, opt);
|
|
24
|
+
},
|
|
25
|
+
makeEnum,
|
|
26
|
+
makeEnumType,
|
|
27
|
+
getEnumType,
|
|
28
|
+
};
|
|
15
29
|
}
|