ts-proto 1.90.0 → 1.90.1
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/CHANGELOG.md +7 -0
- package/build/encode.js +14 -4
- package/build/main.js +86 -66
- package/build/types.js +13 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.90.1](https://github.com/stephenh/ts-proto/compare/v1.90.0...v1.90.1) (2021-11-27)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* code-generation for Services with Struct response types ([#407](https://github.com/stephenh/ts-proto/issues/407)) ([f041fa1](https://github.com/stephenh/ts-proto/commit/f041fa1047816748c366bcb81895b6b917eb328d))
|
|
7
|
+
|
|
1
8
|
# [1.90.0](https://github.com/stephenh/ts-proto/compare/v1.89.0...v1.90.0) (2021-11-24)
|
|
2
9
|
|
|
3
10
|
|
package/build/encode.js
CHANGED
|
@@ -7,12 +7,20 @@ const options_1 = require("./options");
|
|
|
7
7
|
function generateEncoder(ctx, typeName) {
|
|
8
8
|
const name = types_1.wrapperTypeName(typeName);
|
|
9
9
|
if (!name) {
|
|
10
|
-
return ts_poet_1.code `${types_1.messageToTypeName(ctx, typeName)}.encode(value).finish()`;
|
|
10
|
+
return ts_poet_1.code `${types_1.messageToTypeName(ctx, typeName, { keepValueType: true })}.encode(value).finish()`;
|
|
11
11
|
}
|
|
12
12
|
if (name == 'Timestamp') {
|
|
13
13
|
const TimestampValue = ts_poet_1.imp(`${name}@./google/protobuf/timestamp`);
|
|
14
14
|
return ts_poet_1.code `${TimestampValue}.encode(${ctx.utils.toTimestamp}(value)).finish()`;
|
|
15
15
|
}
|
|
16
|
+
if (name == 'Struct') {
|
|
17
|
+
const StructType = ts_poet_1.imp(`${name}@./google/protobuf/struct`);
|
|
18
|
+
return ts_poet_1.code `${StructType}.encode(${StructType}.wrap(value)).finish()`;
|
|
19
|
+
}
|
|
20
|
+
if (name == 'ListValue') {
|
|
21
|
+
const ListValueType = ts_poet_1.imp(`${name}@./google/protobuf/struct`);
|
|
22
|
+
return ts_poet_1.code `${ListValueType}.encode({values: value ?? []}).finish()`;
|
|
23
|
+
}
|
|
16
24
|
const TypeValue = ts_poet_1.imp(`${name}@./google/protobuf/wrappers`);
|
|
17
25
|
switch (name) {
|
|
18
26
|
case 'StringValue':
|
|
@@ -32,8 +40,6 @@ function generateEncoder(ctx, typeName) {
|
|
|
32
40
|
return ts_poet_1.code `${TypeValue}.encode({value: value ?? false}).finish()`;
|
|
33
41
|
case 'BytesValue':
|
|
34
42
|
return ts_poet_1.code `${TypeValue}.encode({value: value ?? new Uint8Array()}).finish()`;
|
|
35
|
-
case 'ListValue':
|
|
36
|
-
return ts_poet_1.code `${TypeValue}.encode({value: value ?? []]}).finish()`;
|
|
37
43
|
}
|
|
38
44
|
throw new Error(`unknown wrapper type: ${name}`);
|
|
39
45
|
}
|
|
@@ -41,13 +47,17 @@ exports.generateEncoder = generateEncoder;
|
|
|
41
47
|
function generateDecoder(ctx, typeName) {
|
|
42
48
|
let name = types_1.wrapperTypeName(typeName);
|
|
43
49
|
if (!name) {
|
|
44
|
-
return ts_poet_1.code `${types_1.messageToTypeName(ctx, typeName)}.decode(value)`;
|
|
50
|
+
return ts_poet_1.code `${types_1.messageToTypeName(ctx, typeName, { keepValueType: true })}.decode(value)`;
|
|
45
51
|
}
|
|
46
52
|
let TypeValue;
|
|
47
53
|
if (name == 'Timestamp') {
|
|
48
54
|
TypeValue = ts_poet_1.imp(`${name}@./google/protobuf/timestamp`);
|
|
49
55
|
return ts_poet_1.code `${TypeValue}.decode(value)`;
|
|
50
56
|
}
|
|
57
|
+
if (name == 'Struct' || name == 'ListValue') {
|
|
58
|
+
TypeValue = ts_poet_1.imp(`${name}@./google/protobuf/struct`);
|
|
59
|
+
return ts_poet_1.code `${TypeValue}.unwrap(${TypeValue}.decode(value))`;
|
|
60
|
+
}
|
|
51
61
|
TypeValue = ts_poet_1.imp(`${name}@./google/protobuf/wrappers`);
|
|
52
62
|
return ts_poet_1.code `${TypeValue}.decode(value).value`;
|
|
53
63
|
}
|
package/build/main.js
CHANGED
|
@@ -77,6 +77,8 @@ function generateFile(ctx, fileDesc) {
|
|
|
77
77
|
if (options.outputPartialMethods) {
|
|
78
78
|
staticMembers.push(generateFromPartial(ctx, fullName, message));
|
|
79
79
|
}
|
|
80
|
+
staticMembers.push(...generateWrap(fullTypeName));
|
|
81
|
+
staticMembers.push(...generateUnwrap(fullTypeName));
|
|
80
82
|
chunks.push(ts_poet_1.code `
|
|
81
83
|
export const ${ts_poet_1.def(fullName)} = {
|
|
82
84
|
${ts_poet_1.joinCode(staticMembers, { on: ',\n\n' })}
|
|
@@ -184,7 +186,6 @@ function makeUtils(options) {
|
|
|
184
186
|
...makeDeepPartial(options, longs),
|
|
185
187
|
...makeTimestampMethods(options, longs),
|
|
186
188
|
...longs,
|
|
187
|
-
...makeWrappingUtils(),
|
|
188
189
|
};
|
|
189
190
|
}
|
|
190
191
|
exports.makeUtils = makeUtils;
|
|
@@ -241,57 +242,6 @@ function makeLongUtils(options, bytes) {
|
|
|
241
242
|
`);
|
|
242
243
|
return { numberToLong, longToNumber, longToString, longInit, Long };
|
|
243
244
|
}
|
|
244
|
-
function makeWrappingUtils() {
|
|
245
|
-
const wrapAnyValue = ts_poet_1.conditionalOutput('wrapAnyValue', ts_poet_1.code `function wrapAnyValue(value: any): Value {
|
|
246
|
-
if (value === null) {
|
|
247
|
-
return {nullValue: 0} as Value;
|
|
248
|
-
} else if (typeof value === 'boolean') {
|
|
249
|
-
return {boolValue: value} as Value;
|
|
250
|
-
} else if (typeof value === 'number') {
|
|
251
|
-
return {numberValue: value} as Value;
|
|
252
|
-
} else if (typeof value === 'string') {
|
|
253
|
-
return {stringValue: value} as Value;
|
|
254
|
-
} else if (Array.isArray(value)) {
|
|
255
|
-
return {listValue: value} as Value;
|
|
256
|
-
} else if (typeof value === 'object') {
|
|
257
|
-
return {structValue: value} as Value;
|
|
258
|
-
} else if (typeof value === 'undefined') {
|
|
259
|
-
return {} as Value;
|
|
260
|
-
} else {
|
|
261
|
-
throw new Error('Unsupported any value type: ' + typeof value);
|
|
262
|
-
}
|
|
263
|
-
}`);
|
|
264
|
-
const unwrapAnyValue = ts_poet_1.conditionalOutput('unwrapAnyValue', ts_poet_1.code `function unwrapAnyValue(value: Value): string | number | boolean | Object | null | Array<any> | undefined {
|
|
265
|
-
if (value.stringValue !== undefined) {
|
|
266
|
-
return value.stringValue;
|
|
267
|
-
} else if (value.numberValue !== undefined) {
|
|
268
|
-
return value.numberValue;
|
|
269
|
-
} else if (value.boolValue !== undefined) {
|
|
270
|
-
return value.boolValue;
|
|
271
|
-
} else if (value.structValue !== undefined) {
|
|
272
|
-
return value.structValue;
|
|
273
|
-
} else if (value.listValue !== undefined) {
|
|
274
|
-
return value.listValue;
|
|
275
|
-
} else if (value.nullValue !== undefined) {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
}`);
|
|
279
|
-
const wrapStruct = ts_poet_1.conditionalOutput('wrapStruct', ts_poet_1.code `function wrapStruct(object: {[key: string]: any}): Struct {
|
|
280
|
-
const struct = Struct.fromPartial({});
|
|
281
|
-
Object.keys(object).forEach(key => {
|
|
282
|
-
struct.fields[key] = object[key];
|
|
283
|
-
});
|
|
284
|
-
return struct;
|
|
285
|
-
}`);
|
|
286
|
-
const unwrapStruct = ts_poet_1.conditionalOutput('unwrapStruct', ts_poet_1.code `function unwrapStruct(struct: Struct): {[key: string]: any} {
|
|
287
|
-
const object: { [key: string]: any } = {};
|
|
288
|
-
Object.keys(struct.fields).forEach(key => {
|
|
289
|
-
object[key] = struct.fields[key];
|
|
290
|
-
});
|
|
291
|
-
return object;
|
|
292
|
-
}`);
|
|
293
|
-
return { wrapAnyValue, unwrapAnyValue, wrapStruct, unwrapStruct };
|
|
294
|
-
}
|
|
295
245
|
function makeByteUtils() {
|
|
296
246
|
const globalThis = ts_poet_1.conditionalOutput('globalThis', ts_poet_1.code `
|
|
297
247
|
declare var self: any | undefined;
|
|
@@ -580,16 +530,13 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
580
530
|
}
|
|
581
531
|
}
|
|
582
532
|
else if (types_1.isValueType(ctx, field)) {
|
|
533
|
+
const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
|
|
583
534
|
const unwrap = (decodedValue) => {
|
|
584
|
-
if (types_1.isAnyValueType(field))
|
|
585
|
-
return ts_poet_1.code `${
|
|
586
|
-
|
|
587
|
-
return ts_poet_1.code `${ctx.utils.unwrapStruct}(${decodedValue})`;
|
|
588
|
-
if (types_1.isListValueType(field))
|
|
589
|
-
return ts_poet_1.code `${decodedValue}.values`;
|
|
535
|
+
if (types_1.isListValueType(field) || types_1.isStructType(field) || types_1.isAnyValueType(field)) {
|
|
536
|
+
return ts_poet_1.code `${type}.unwrap(${decodedValue})`;
|
|
537
|
+
}
|
|
590
538
|
return ts_poet_1.code `${decodedValue}.value`;
|
|
591
539
|
};
|
|
592
|
-
const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
|
|
593
540
|
const decoder = ts_poet_1.code `${type}.decode(reader, reader.uint32())`;
|
|
594
541
|
readSnippet = ts_poet_1.code `${unwrap(decoder)}`;
|
|
595
542
|
}
|
|
@@ -687,17 +634,14 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
687
634
|
}
|
|
688
635
|
else if (types_1.isValueType(ctx, field)) {
|
|
689
636
|
const maybeTypeField = options.outputTypeRegistry ? `$type: '${field.typeName.slice(1)}',` : '';
|
|
637
|
+
const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
|
|
690
638
|
const wrappedValue = (place) => {
|
|
691
|
-
if (types_1.isAnyValueType(field))
|
|
692
|
-
return ts_poet_1.code `${
|
|
693
|
-
|
|
694
|
-
return ts_poet_1.code `{values: ${place}}`;
|
|
695
|
-
if (types_1.isStructType(field))
|
|
696
|
-
return ts_poet_1.code `${ctx.utils.wrapStruct}(${place})`;
|
|
639
|
+
if (types_1.isAnyValueType(field) || types_1.isListValueType(field) || types_1.isStructType(field)) {
|
|
640
|
+
return ts_poet_1.code `${type}.wrap(${place})`;
|
|
641
|
+
}
|
|
697
642
|
return ts_poet_1.code `{${maybeTypeField} value: ${place}!}`;
|
|
698
643
|
};
|
|
699
644
|
const tag = ((field.number << 3) | 2) >>> 0;
|
|
700
|
-
const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
|
|
701
645
|
writeSnippet = (place) => ts_poet_1.code `${type}.encode(${wrappedValue(place)}, writer.uint32(${tag}).fork()).ldelim()`;
|
|
702
646
|
}
|
|
703
647
|
else if (types_1.isMessage(field)) {
|
|
@@ -1214,6 +1158,82 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1214
1158
|
chunks.push(ts_poet_1.code `}`);
|
|
1215
1159
|
return ts_poet_1.joinCode(chunks, { on: '\n' });
|
|
1216
1160
|
}
|
|
1161
|
+
function generateWrap(fullProtoTypeName) {
|
|
1162
|
+
const chunks = [];
|
|
1163
|
+
if (types_1.isStructTypeName(fullProtoTypeName)) {
|
|
1164
|
+
chunks.push(ts_poet_1.code `wrap(object: {[key: string]: any} | undefined): Struct {
|
|
1165
|
+
const struct = Struct.fromPartial({});
|
|
1166
|
+
if (object !== undefined) {
|
|
1167
|
+
Object.keys(object).forEach(key => {
|
|
1168
|
+
struct.fields[key] = object[key];
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
return struct;
|
|
1172
|
+
}`);
|
|
1173
|
+
}
|
|
1174
|
+
if (types_1.isAnyValueTypeName(fullProtoTypeName)) {
|
|
1175
|
+
chunks.push(ts_poet_1.code `wrap(value: any): Value {
|
|
1176
|
+
if (value === null) {
|
|
1177
|
+
return {nullValue: 0} as Value;
|
|
1178
|
+
} else if (typeof value === 'boolean') {
|
|
1179
|
+
return {boolValue: value} as Value;
|
|
1180
|
+
} else if (typeof value === 'number') {
|
|
1181
|
+
return {numberValue: value} as Value;
|
|
1182
|
+
} else if (typeof value === 'string') {
|
|
1183
|
+
return {stringValue: value} as Value;
|
|
1184
|
+
} else if (Array.isArray(value)) {
|
|
1185
|
+
return {listValue: value} as Value;
|
|
1186
|
+
} else if (typeof value === 'object') {
|
|
1187
|
+
return {structValue: value} as Value;
|
|
1188
|
+
} else if (typeof value === 'undefined') {
|
|
1189
|
+
return {} as Value;
|
|
1190
|
+
} else {
|
|
1191
|
+
throw new Error('Unsupported any value type: ' + typeof value);
|
|
1192
|
+
}
|
|
1193
|
+
}`);
|
|
1194
|
+
}
|
|
1195
|
+
if (types_1.isListValueTypeName(fullProtoTypeName)) {
|
|
1196
|
+
chunks.push(ts_poet_1.code `wrap(value: Array<any>): ListValue {
|
|
1197
|
+
return {values: value};
|
|
1198
|
+
}`);
|
|
1199
|
+
}
|
|
1200
|
+
return chunks;
|
|
1201
|
+
}
|
|
1202
|
+
function generateUnwrap(fullProtoTypeName) {
|
|
1203
|
+
const chunks = [];
|
|
1204
|
+
if (types_1.isStructTypeName(fullProtoTypeName)) {
|
|
1205
|
+
chunks.push(ts_poet_1.code `unwrap(message: Struct): {[key: string]: any} {
|
|
1206
|
+
const object: { [key: string]: any } = {};
|
|
1207
|
+
Object.keys(message.fields).forEach(key => {
|
|
1208
|
+
object[key] = message.fields[key];
|
|
1209
|
+
});
|
|
1210
|
+
return object;
|
|
1211
|
+
}`);
|
|
1212
|
+
}
|
|
1213
|
+
if (types_1.isAnyValueTypeName(fullProtoTypeName)) {
|
|
1214
|
+
chunks.push(ts_poet_1.code `unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
|
|
1215
|
+
if (message.stringValue !== undefined) {
|
|
1216
|
+
return message.stringValue;
|
|
1217
|
+
} else if (message.numberValue !== undefined) {
|
|
1218
|
+
return message.numberValue;
|
|
1219
|
+
} else if (message.boolValue !== undefined) {
|
|
1220
|
+
return message.boolValue;
|
|
1221
|
+
} else if (message.structValue !== undefined) {
|
|
1222
|
+
return message.structValue;
|
|
1223
|
+
} else if (message.listValue !== undefined) {
|
|
1224
|
+
return message.listValue;
|
|
1225
|
+
} else if (message.nullValue !== undefined) {
|
|
1226
|
+
return null;
|
|
1227
|
+
}
|
|
1228
|
+
}`);
|
|
1229
|
+
}
|
|
1230
|
+
if (types_1.isListValueTypeName(fullProtoTypeName)) {
|
|
1231
|
+
chunks.push(ts_poet_1.code `unwrap(message: ListValue): Array<any> {
|
|
1232
|
+
return message.values;
|
|
1233
|
+
}`);
|
|
1234
|
+
}
|
|
1235
|
+
return chunks;
|
|
1236
|
+
}
|
|
1217
1237
|
exports.contextTypeVar = 'Context extends DataLoaders';
|
|
1218
1238
|
function maybeCastToNumber(ctx, messageDesc, field, variableName) {
|
|
1219
1239
|
const { keyType } = types_1.detectMapType(ctx, messageDesc, field);
|
package/build/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isMapType = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
|
|
3
|
+
exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isMapType = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
|
|
4
4
|
const ts_proto_descriptors_1 = require("ts-proto-descriptors");
|
|
5
5
|
const ts_poet_1 = require("ts-poet");
|
|
6
6
|
const options_1 = require("./options");
|
|
@@ -360,9 +360,13 @@ function isValueType(ctx, field) {
|
|
|
360
360
|
}
|
|
361
361
|
exports.isValueType = isValueType;
|
|
362
362
|
function isAnyValueType(field) {
|
|
363
|
-
return field.typeName
|
|
363
|
+
return isAnyValueTypeName(field.typeName);
|
|
364
364
|
}
|
|
365
365
|
exports.isAnyValueType = isAnyValueType;
|
|
366
|
+
function isAnyValueTypeName(typeName) {
|
|
367
|
+
return typeName === 'google.protobuf.Value' || typeName === '.google.protobuf.Value';
|
|
368
|
+
}
|
|
369
|
+
exports.isAnyValueTypeName = isAnyValueTypeName;
|
|
366
370
|
function isBytesValueType(field) {
|
|
367
371
|
return field.typeName === '.google.protobuf.BytesValue';
|
|
368
372
|
}
|
|
@@ -376,9 +380,13 @@ function isListValueTypeName(typeName) {
|
|
|
376
380
|
}
|
|
377
381
|
exports.isListValueTypeName = isListValueTypeName;
|
|
378
382
|
function isStructType(field) {
|
|
379
|
-
return field.typeName
|
|
383
|
+
return isStructTypeName(field.typeName);
|
|
380
384
|
}
|
|
381
385
|
exports.isStructType = isStructType;
|
|
386
|
+
function isStructTypeName(typeName) {
|
|
387
|
+
return typeName === 'google.protobuf.Struct' || typeName === '.google.protobuf.Struct';
|
|
388
|
+
}
|
|
389
|
+
exports.isStructTypeName = isStructTypeName;
|
|
382
390
|
function isLongValueType(field) {
|
|
383
391
|
return field.typeName === '.google.protobuf.Int64Value' || field.typeName === '.google.protobuf.UInt64Value';
|
|
384
392
|
}
|
|
@@ -428,6 +436,7 @@ function wrapperTypeName(typeName) {
|
|
|
428
436
|
case '.google.protobuf.BytesValue':
|
|
429
437
|
case '.google.protobuf.ListValue':
|
|
430
438
|
case '.google.protobuf.Timestamp':
|
|
439
|
+
case '.google.protobuf.Struct':
|
|
431
440
|
return typeName.split('.')[3];
|
|
432
441
|
default:
|
|
433
442
|
return undefined;
|
|
@@ -548,7 +557,7 @@ function requestType(ctx, methodDesc) {
|
|
|
548
557
|
}
|
|
549
558
|
exports.requestType = requestType;
|
|
550
559
|
function responseType(ctx, methodDesc) {
|
|
551
|
-
return messageToTypeName(ctx, methodDesc.outputType);
|
|
560
|
+
return messageToTypeName(ctx, methodDesc.outputType, { keepValueType: true });
|
|
552
561
|
}
|
|
553
562
|
exports.responseType = responseType;
|
|
554
563
|
function responsePromise(ctx, methodDesc) {
|