ts-proto 1.176.3 → 1.177.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/README.markdown +5 -0
- package/build/main.js +6 -2
- package/build/options.d.ts +1 -0
- package/build/options.js +1 -0
- package/build/types.d.ts +2 -2
- package/build/types.js +14 -5
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -603,6 +603,11 @@ export interface User {
|
|
|
603
603
|
}
|
|
604
604
|
```
|
|
605
605
|
|
|
606
|
+
- With `--ts_proto_opt=noDefaultsForOptionals=true`, `undefined` primitive values will not be defaulted as per the protobuf spec. Additionally unlike the standard behavior, when a field is set to it's standard default value, it *will* be encoded allowing it to be sent over the wire and distinguished from undefined values. For example if a message does not set a boolean value, ordinarily this would be defaulted to `false` which is different to it being undefined.
|
|
607
|
+
|
|
608
|
+
This option allows the library to act in a compatible way with the [Wire implementation](https://square.github.io/wire/) maintained and used by Square/Block. Note: this option should only be used in combination with other client/server code generated using Wire or ts-proto with this option enabled.
|
|
609
|
+
|
|
610
|
+
|
|
606
611
|
### NestJS Support
|
|
607
612
|
|
|
608
613
|
We have a great way of working together with [nestjs](https://docs.nestjs.com/microservices/grpc). `ts-proto` generates `interfaces` and `decorators` for you controller, client. For more information see the [nestjs readme](NESTJS.markdown).
|
package/build/main.js
CHANGED
|
@@ -509,7 +509,7 @@ function makeTimestampMethods(options, longs, bytes) {
|
|
|
509
509
|
const NanoDate = (0, ts_poet_1.imp)("NanoDate=nano-date");
|
|
510
510
|
let seconds = "Math.trunc(date.getTime() / 1_000)";
|
|
511
511
|
let toNumberCode = "t.seconds";
|
|
512
|
-
const makeToNumberCode = (methodCall) => `t.seconds${options.useOptionals === "all" ? "?" : ""}.${methodCall}`;
|
|
512
|
+
const makeToNumberCode = (methodCall) => `t.seconds${options.useOptionals === "all" || options.noDefaultsForOptionals ? "?" : ""}.${methodCall}`;
|
|
513
513
|
if (options.forceLong === options_1.LongOption.LONG) {
|
|
514
514
|
toNumberCode = makeToNumberCode("toNumber()");
|
|
515
515
|
seconds = (0, ts_poet_1.code) `${longs.numberToLong}(${seconds})`;
|
|
@@ -926,10 +926,14 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
926
926
|
${messageProperty} = ${generateMapType ? "new Map()" : "{}"};
|
|
927
927
|
}`
|
|
928
928
|
: "";
|
|
929
|
+
const ifValueCheck = `${varName}.value !== undefined ${(0, utils_1.withAndMaybeCheckIsNotNull)(options, `${varName}.value`)}`;
|
|
930
|
+
const maybeIfKeyCheck = `${options.noDefaultsForOptionals
|
|
931
|
+
? ` && ${varName}.key !== undefined ${(0, utils_1.withAndMaybeCheckIsNotNull)(options, `${varName}.key`)}`
|
|
932
|
+
: ""}`;
|
|
929
933
|
chunks.push((0, ts_poet_1.code) `
|
|
930
934
|
${tagCheck}
|
|
931
935
|
const ${varName} = ${readSnippet};
|
|
932
|
-
if (${
|
|
936
|
+
if (${ifValueCheck}${maybeIfKeyCheck}) {
|
|
933
937
|
${initializerSnippet}
|
|
934
938
|
${valueSetterSnippet};
|
|
935
939
|
}
|
package/build/options.d.ts
CHANGED
|
@@ -96,6 +96,7 @@ export type Options = {
|
|
|
96
96
|
disableProto2DefaultValues: boolean;
|
|
97
97
|
useNullAsOptional: boolean;
|
|
98
98
|
annotateFilesWithVersion: boolean;
|
|
99
|
+
noDefaultsForOptionals: boolean;
|
|
99
100
|
};
|
|
100
101
|
export declare function defaultOptions(): Options;
|
|
101
102
|
export declare function optionsFromParameter(parameter: string | undefined): Options;
|
package/build/options.js
CHANGED
package/build/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { CodeGeneratorRequest, DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FieldDescriptorProto_Type, FileDescriptorProto, MessageOptions, MethodDescriptorProto, ServiceDescriptorProto } from "ts-proto-descriptors";
|
|
2
1
|
import { Code, Import } from "ts-poet";
|
|
3
|
-
import {
|
|
2
|
+
import { CodeGeneratorRequest, DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FieldDescriptorProto_Type, FileDescriptorProto, MessageOptions, MethodDescriptorProto, ServiceDescriptorProto } from "ts-proto-descriptors";
|
|
4
3
|
import { Context } from "./context";
|
|
4
|
+
import { Options } from "./options";
|
|
5
5
|
/** Based on https://github.com/dcodeIO/protobuf.js/blob/master/src/types.js#L37. */
|
|
6
6
|
export declare function basicWireType(type: FieldDescriptorProto_Type): number;
|
|
7
7
|
export declare function basicLongWireType(type: FieldDescriptorProto_Type): number | undefined;
|
package/build/types.js
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.observableType = exports.rawRequestType = exports.detectMapType = exports.shouldGenerateJSMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isFieldMaskTypeName = exports.isFieldMaskType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isObjectId = 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.getFieldOptionsJsType = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
|
|
4
4
|
exports.isJsTypeFieldOption = void 0;
|
|
5
|
-
const ts_proto_descriptors_1 = require("ts-proto-descriptors");
|
|
6
5
|
const ts_poet_1 = require("ts-poet");
|
|
7
|
-
const
|
|
8
|
-
const visit_1 = require("./visit");
|
|
9
|
-
const utils_1 = require("./utils");
|
|
10
|
-
const sourceInfo_1 = require("./sourceInfo");
|
|
6
|
+
const ts_proto_descriptors_1 = require("ts-proto-descriptors");
|
|
11
7
|
const case_1 = require("./case");
|
|
12
8
|
const enums_1 = require("./enums");
|
|
9
|
+
const options_1 = require("./options");
|
|
10
|
+
const sourceInfo_1 = require("./sourceInfo");
|
|
11
|
+
const utils_1 = require("./utils");
|
|
12
|
+
const visit_1 = require("./visit");
|
|
13
13
|
/** Based on https://github.com/dcodeIO/protobuf.js/blob/master/src/types.js#L37. */
|
|
14
14
|
function basicWireType(type) {
|
|
15
15
|
switch (type) {
|
|
@@ -189,6 +189,9 @@ function getFieldOptionsJsType(field, options) {
|
|
|
189
189
|
exports.getFieldOptionsJsType = getFieldOptionsJsType;
|
|
190
190
|
function defaultValue(ctx, field) {
|
|
191
191
|
const { typeMap, options, utils, currentFile } = ctx;
|
|
192
|
+
if (options.noDefaultsForOptionals) {
|
|
193
|
+
return options.useNullAsOptional ? null : undefined;
|
|
194
|
+
}
|
|
192
195
|
const useDefaultValue = !currentFile.isProto3Syntax && !options.disableProto2DefaultValues && field.defaultValue;
|
|
193
196
|
const numericDefaultVal = useDefaultValue ? field.defaultValue : 0;
|
|
194
197
|
switch (field.type) {
|
|
@@ -265,6 +268,11 @@ exports.defaultValue = defaultValue;
|
|
|
265
268
|
function notDefaultCheck(ctx, field, messageOptions, place) {
|
|
266
269
|
const { typeMap, options, currentFile } = ctx;
|
|
267
270
|
const isOptional = isOptionalProperty(field, messageOptions, options, currentFile.isProto3Syntax);
|
|
271
|
+
if (options.noDefaultsForOptionals) {
|
|
272
|
+
return isOptional
|
|
273
|
+
? (0, ts_poet_1.code) `${place} !== undefined ${(0, utils_1.withAndMaybeCheckIsNotNull)(options, place)}`
|
|
274
|
+
: (0, ts_poet_1.code) `${place} !== undefined`;
|
|
275
|
+
}
|
|
268
276
|
const maybeNotUndefinedAnd = isOptional
|
|
269
277
|
? `${place} !== undefined ${(0, utils_1.withAndMaybeCheckIsNotNull)(options, place)} &&`
|
|
270
278
|
: "";
|
|
@@ -368,6 +376,7 @@ function isOptionalProperty(field, messageOptions, options, isProto3Syntax) {
|
|
|
368
376
|
const deprecatedOnly = options.useOptionals === "deprecatedOnly" && field.options && field.options.deprecated;
|
|
369
377
|
return ((optionalMessages && isMessage(field) && !isRepeated(field)) ||
|
|
370
378
|
((optionalAll || deprecatedOnly) && !(messageOptions === null || messageOptions === void 0 ? void 0 : messageOptions.mapEntry)) ||
|
|
379
|
+
(options.noDefaultsForOptionals && !isRepeated(field) && (isScalar(field) || isEnum(field))) ||
|
|
371
380
|
// file is proto2, we have enabled proto2 optionals, and the field itself is optional
|
|
372
381
|
(!isProto3Syntax &&
|
|
373
382
|
field.label === ts_proto_descriptors_1.FieldDescriptorProto_Label.LABEL_OPTIONAL &&
|