ts-proto 1.94.0 → 1.95.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/build/main.js +49 -13
- package/build/options.js +1 -1
- package/build/types.js +37 -17
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.95.0](https://github.com/stephenh/ts-proto/compare/v1.94.0...v1.95.0) (2021-12-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* Add useOptionals=all to enable non-field members to be optional. ([#402](https://github.com/stephenh/ts-proto/issues/402)) ([e7b70cb](https://github.com/stephenh/ts-proto/commit/e7b70cbd7b9bd43bf9e6e54e25bc48c527718317))
|
|
7
|
+
|
|
1
8
|
# [1.94.0](https://github.com/stephenh/ts-proto/compare/v1.93.3...v1.94.0) (2021-12-14)
|
|
2
9
|
|
|
3
10
|
|
package/build/main.js
CHANGED
|
@@ -19,6 +19,14 @@ const generate_generic_service_definition_1 = require("./generate-generic-servic
|
|
|
19
19
|
function generateFile(ctx, fileDesc) {
|
|
20
20
|
var _a;
|
|
21
21
|
const { options, utils } = ctx;
|
|
22
|
+
if (options.useOptionals === false) {
|
|
23
|
+
console.warn("ts-proto: Passing useOptionals as a boolean option is deprecated and will be removed in a future version. Please pass the string 'none' instead of false.");
|
|
24
|
+
options.useOptionals = 'none';
|
|
25
|
+
}
|
|
26
|
+
else if (options.useOptionals === true) {
|
|
27
|
+
console.warn("ts-proto: Passing useOptionals as a boolean option is deprecated and will be removed in a future version. Please pass the string 'messages' instead of true.");
|
|
28
|
+
options.useOptionals = 'messages';
|
|
29
|
+
}
|
|
22
30
|
// Google's protofiles are organized like Java, where package == the folder the file
|
|
23
31
|
// is in, and file == a specific service within the package. I.e. you can have multiple
|
|
24
32
|
// company/foo.proto and company/bar.proto files, where package would be 'company'.
|
|
@@ -388,10 +396,6 @@ function makeTimestampMethods(options, longs) {
|
|
|
388
396
|
`);
|
|
389
397
|
return { toTimestamp, fromTimestamp, fromJsonTimestamp };
|
|
390
398
|
}
|
|
391
|
-
// When useOptionals=true, non-scalar fields are translated into optional properties.
|
|
392
|
-
function isOptionalProperty(field, options) {
|
|
393
|
-
return (options.useOptionals && types_1.isMessage(field) && !types_1.isRepeated(field)) || field.proto3Optional;
|
|
394
|
-
}
|
|
395
399
|
// Create the interface with properties
|
|
396
400
|
function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fullTypeName) {
|
|
397
401
|
var _a;
|
|
@@ -419,7 +423,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
419
423
|
utils_1.maybeAddComment(info, chunks, (_a = fieldDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
|
|
420
424
|
const name = case_1.maybeSnakeToCamel(fieldDesc.name, options);
|
|
421
425
|
const type = types_1.toTypeName(ctx, messageDesc, fieldDesc);
|
|
422
|
-
const q = isOptionalProperty(fieldDesc, options) ? '?' : '';
|
|
426
|
+
const q = types_1.isOptionalProperty(fieldDesc, messageDesc.options, options) ? '?' : '';
|
|
423
427
|
chunks.push(ts_poet_1.code `${name}${q}: ${type}, `);
|
|
424
428
|
});
|
|
425
429
|
chunks.push(ts_poet_1.code `}`);
|
|
@@ -662,6 +666,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
662
666
|
else {
|
|
663
667
|
throw new Error(`Unhandled field ${field}`);
|
|
664
668
|
}
|
|
669
|
+
const isOptional = types_1.isOptionalProperty(field, messageDesc.options, options);
|
|
665
670
|
if (types_1.isRepeated(field)) {
|
|
666
671
|
if (types_1.isMapType(ctx, messageDesc, field)) {
|
|
667
672
|
const valueType = typeMap.get(field.typeName)[2].field[1];
|
|
@@ -673,18 +678,29 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
673
678
|
}
|
|
674
679
|
`
|
|
675
680
|
: writeSnippet(`{ ${maybeTypeField} key: key as any, value }`);
|
|
681
|
+
const optionalAlternative = isOptional ? ' || {}' : '';
|
|
676
682
|
chunks.push(ts_poet_1.code `
|
|
677
|
-
Object.entries(message.${fieldName}).forEach(([key, value]) => {
|
|
683
|
+
Object.entries(message.${fieldName}${optionalAlternative}).forEach(([key, value]) => {
|
|
678
684
|
${entryWriteSnippet}
|
|
679
685
|
});
|
|
680
686
|
`);
|
|
681
687
|
}
|
|
682
688
|
else if (types_1.packedType(field.type) === undefined) {
|
|
683
|
-
|
|
689
|
+
const listWriteSnippet = ts_poet_1.code `
|
|
684
690
|
for (const v of message.${fieldName}) {
|
|
685
691
|
${writeSnippet('v!')};
|
|
686
692
|
}
|
|
687
|
-
|
|
693
|
+
`;
|
|
694
|
+
if (isOptional) {
|
|
695
|
+
chunks.push(ts_poet_1.code `
|
|
696
|
+
if (message.${fieldName} !== undefined && message.${fieldName}.length !== 0) {
|
|
697
|
+
${listWriteSnippet}
|
|
698
|
+
}
|
|
699
|
+
`);
|
|
700
|
+
}
|
|
701
|
+
else {
|
|
702
|
+
chunks.push(listWriteSnippet);
|
|
703
|
+
}
|
|
688
704
|
}
|
|
689
705
|
else if (types_1.isEnum(field) && options.stringEnums) {
|
|
690
706
|
// This is a lot like the `else` clause, but we wrap `fooToNumber` around it.
|
|
@@ -693,24 +709,44 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
693
709
|
// (i.e. just one tag and multiple values).
|
|
694
710
|
const tag = ((field.number << 3) | 2) >>> 0;
|
|
695
711
|
const toNumber = types_1.getEnumMethod(ctx, field.typeName, 'ToNumber');
|
|
696
|
-
|
|
712
|
+
const listWriteSnippet = ts_poet_1.code `
|
|
697
713
|
writer.uint32(${tag}).fork();
|
|
698
714
|
for (const v of message.${fieldName}) {
|
|
699
715
|
writer.${types_1.toReaderCall(field)}(${toNumber}(v));
|
|
700
716
|
}
|
|
701
717
|
writer.ldelim();
|
|
702
|
-
|
|
718
|
+
`;
|
|
719
|
+
if (isOptional) {
|
|
720
|
+
chunks.push(ts_poet_1.code `
|
|
721
|
+
if (message.${fieldName} !== undefined && message.${fieldName}.length !== 0) {
|
|
722
|
+
${listWriteSnippet}
|
|
723
|
+
}
|
|
724
|
+
`);
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
chunks.push(listWriteSnippet);
|
|
728
|
+
}
|
|
703
729
|
}
|
|
704
730
|
else {
|
|
705
731
|
// Ideally we'd reuse `writeSnippet` but it has tagging embedded inside of it.
|
|
706
732
|
const tag = ((field.number << 3) | 2) >>> 0;
|
|
707
|
-
|
|
733
|
+
const listWriteSnippet = ts_poet_1.code `
|
|
708
734
|
writer.uint32(${tag}).fork();
|
|
709
735
|
for (const v of message.${fieldName}) {
|
|
710
736
|
writer.${types_1.toReaderCall(field)}(v);
|
|
711
737
|
}
|
|
712
738
|
writer.ldelim();
|
|
713
|
-
|
|
739
|
+
`;
|
|
740
|
+
if (isOptional) {
|
|
741
|
+
chunks.push(ts_poet_1.code `
|
|
742
|
+
if (message.${fieldName} !== undefined && message.${fieldName}.length !== 0) {
|
|
743
|
+
${listWriteSnippet}
|
|
744
|
+
}
|
|
745
|
+
`);
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
chunks.push(listWriteSnippet);
|
|
749
|
+
}
|
|
714
750
|
}
|
|
715
751
|
}
|
|
716
752
|
else if (types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
|
|
@@ -738,7 +774,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
738
774
|
}
|
|
739
775
|
else if (types_1.isScalar(field) || types_1.isEnum(field)) {
|
|
740
776
|
chunks.push(ts_poet_1.code `
|
|
741
|
-
if (${types_1.notDefaultCheck(ctx, field, `message.${fieldName}`)}) {
|
|
777
|
+
if (${types_1.notDefaultCheck(ctx, field, messageDesc.options, `message.${fieldName}`)}) {
|
|
742
778
|
${writeSnippet(`message.${fieldName}`)};
|
|
743
779
|
}
|
|
744
780
|
`);
|
package/build/options.js
CHANGED
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.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isMapType = exports.isWholeNumber = 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.isWholeNumber = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isOptionalProperty = 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");
|
|
@@ -225,8 +225,10 @@ function defaultValue(ctx, field) {
|
|
|
225
225
|
}
|
|
226
226
|
exports.defaultValue = defaultValue;
|
|
227
227
|
/** Creates code that checks that the field is not the default value. Supports scalars and enums. */
|
|
228
|
-
function notDefaultCheck(ctx, field, place) {
|
|
228
|
+
function notDefaultCheck(ctx, field, messageOptions, place) {
|
|
229
229
|
const { typeMap, options } = ctx;
|
|
230
|
+
const isOptional = isOptionalProperty(field, messageOptions, options);
|
|
231
|
+
const maybeNotUndefinedAnd = isOptional ? `${place} !== undefined && ` : '';
|
|
230
232
|
switch (field.type) {
|
|
231
233
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_DOUBLE:
|
|
232
234
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FLOAT:
|
|
@@ -235,7 +237,7 @@ function notDefaultCheck(ctx, field, place) {
|
|
|
235
237
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SINT32:
|
|
236
238
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FIXED32:
|
|
237
239
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED32:
|
|
238
|
-
return ts_poet_1.code `${place} !== 0`;
|
|
240
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place} !== 0`;
|
|
239
241
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_ENUM:
|
|
240
242
|
// proto3 enforces enums starting at 0, however proto2 does not, so we have
|
|
241
243
|
// to probe and see if zero is an allowed value. If it's not, pick the first one.
|
|
@@ -245,10 +247,10 @@ function notDefaultCheck(ctx, field, place) {
|
|
|
245
247
|
const zerothValue = enumProto.value.find((v) => v.number === 0) || enumProto.value[0];
|
|
246
248
|
if (options.stringEnums) {
|
|
247
249
|
const enumType = messageToTypeName(ctx, field.typeName);
|
|
248
|
-
return ts_poet_1.code `${place} !== ${enumType}.${zerothValue.name}`;
|
|
250
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place} !== ${enumType}.${zerothValue.name}`;
|
|
249
251
|
}
|
|
250
252
|
else {
|
|
251
|
-
return ts_poet_1.code `${place} !== ${zerothValue.number}`;
|
|
253
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place} !== ${zerothValue.number}`;
|
|
252
254
|
}
|
|
253
255
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_UINT64:
|
|
254
256
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FIXED64:
|
|
@@ -256,20 +258,20 @@ function notDefaultCheck(ctx, field, place) {
|
|
|
256
258
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SINT64:
|
|
257
259
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED64:
|
|
258
260
|
if (options.forceLong === options_1.LongOption.LONG) {
|
|
259
|
-
return ts_poet_1.code
|
|
261
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} !${place}.isZero()`;
|
|
260
262
|
}
|
|
261
263
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
262
|
-
return ts_poet_1.code `${place} !== "0"`;
|
|
264
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place} !== "0"`;
|
|
263
265
|
}
|
|
264
266
|
else {
|
|
265
|
-
return ts_poet_1.code `${place} !== 0`;
|
|
267
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place} !== 0`;
|
|
266
268
|
}
|
|
267
269
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL:
|
|
268
270
|
return ts_poet_1.code `${place} === true`;
|
|
269
271
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_STRING:
|
|
270
|
-
return ts_poet_1.code `${place} !== ""`;
|
|
272
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place} !== ""`;
|
|
271
273
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BYTES:
|
|
272
|
-
return ts_poet_1.code `${place}.length !== 0`;
|
|
274
|
+
return ts_poet_1.code `${maybeNotUndefinedAnd} ${place}.length !== 0`;
|
|
273
275
|
default:
|
|
274
276
|
throw new Error('Not implemented for the given type.');
|
|
275
277
|
}
|
|
@@ -314,6 +316,18 @@ function isScalar(field) {
|
|
|
314
316
|
return scalarTypes.includes(field.type);
|
|
315
317
|
}
|
|
316
318
|
exports.isScalar = isScalar;
|
|
319
|
+
// When useOptionals='messages', non-scalar fields are translated into optional
|
|
320
|
+
// properties. When useOptionals='all', all fields are translated into
|
|
321
|
+
// optional properties, with the exception of map Entry key/values, which must
|
|
322
|
+
// always be present.
|
|
323
|
+
function isOptionalProperty(field, messageOptions, options) {
|
|
324
|
+
const optionalMessages = options.useOptionals === true || options.useOptionals === 'messages' || options.useOptionals === 'all';
|
|
325
|
+
const optionalAll = options.useOptionals === 'all';
|
|
326
|
+
return ((optionalMessages && isMessage(field) && !isRepeated(field)) ||
|
|
327
|
+
(optionalAll && !(messageOptions === null || messageOptions === void 0 ? void 0 : messageOptions.mapEntry)) ||
|
|
328
|
+
field.proto3Optional);
|
|
329
|
+
}
|
|
330
|
+
exports.isOptionalProperty = isOptionalProperty;
|
|
317
331
|
/** This includes all scalars, enums and the [groups type](https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/DescriptorProtos.FieldDescriptorProto.Type.html#TYPE_GROUP) */
|
|
318
332
|
function isPrimitive(field) {
|
|
319
333
|
return !isMessage(field);
|
|
@@ -475,11 +489,14 @@ function messageToTypeName(ctx, protoType, typeOptions = {}) {
|
|
|
475
489
|
// them to basic built-in types, we union the type with undefined to
|
|
476
490
|
// indicate the value is optional. Exceptions:
|
|
477
491
|
// - If the field is repeated, values cannot be undefined.
|
|
478
|
-
// - If useOptionals=
|
|
479
|
-
// properties, so there's no need for that union.
|
|
492
|
+
// - If useOptionals='messages' or useOptionals='all', all non-scalar types
|
|
493
|
+
// are already optional properties, so there's no need for that union.
|
|
480
494
|
let valueType = valueTypeName(ctx, protoType);
|
|
481
495
|
if (!typeOptions.keepValueType && valueType) {
|
|
482
|
-
if (!!typeOptions.repeated ||
|
|
496
|
+
if (!!typeOptions.repeated ||
|
|
497
|
+
options.useOptionals === true ||
|
|
498
|
+
options.useOptionals === 'messages' ||
|
|
499
|
+
options.useOptionals === 'all') {
|
|
483
500
|
return valueType;
|
|
484
501
|
}
|
|
485
502
|
return ts_poet_1.code `${valueType} | undefined`;
|
|
@@ -522,18 +539,21 @@ function toTypeName(ctx, messageDesc, field) {
|
|
|
522
539
|
// in messageToTypeName, so no need to consider them for that here.
|
|
523
540
|
return type;
|
|
524
541
|
}
|
|
525
|
-
// By default (useOptionals=
|
|
542
|
+
// By default (useOptionals='none', oneof=properties), non-scalar fields
|
|
526
543
|
// outside oneofs and all fields within a oneof clause need to be unioned
|
|
527
544
|
// with `undefined` to indicate the value is optional.
|
|
528
545
|
//
|
|
529
|
-
// When useOptionals=
|
|
530
|
-
// properties, so no need for the union with
|
|
546
|
+
// When useOptionals='messages' or useOptionals='all', non-scalar fields are
|
|
547
|
+
// translated to optional properties, so no need for the union with
|
|
548
|
+
// `undefined` here.
|
|
531
549
|
//
|
|
532
550
|
// When oneof=unions, we generate a single property for the entire `oneof`
|
|
533
551
|
// clause, spelling each option out inside a large type union. No need for
|
|
534
552
|
// union with `undefined` here, either.
|
|
535
553
|
const { options } = ctx;
|
|
536
|
-
if ((!isWithinOneOf(field) &&
|
|
554
|
+
if ((!isWithinOneOf(field) &&
|
|
555
|
+
isMessage(field) &&
|
|
556
|
+
(options.useOptionals === false || options.useOptionals === 'none')) ||
|
|
537
557
|
(isWithinOneOf(field) && options.oneof === options_1.OneofOption.PROPERTIES) ||
|
|
538
558
|
(isWithinOneOf(field) && field.proto3Optional)) {
|
|
539
559
|
return ts_poet_1.code `${type} | undefined`;
|