ts-proto 1.176.3 → 1.178.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 CHANGED
@@ -434,7 +434,7 @@ Generated code will be placed in the Gradle build directory.
434
434
 
435
435
  - With `--ts_proto_opt=annotateFilesWithVersion=false`, the generated files will not contain the versions of `protoc` and `ts-proto` used to generate the file. This option is normally set to `true`, such that files list the versions used.
436
436
 
437
- - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
437
+ - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators. If outputSchema is instead specified to be `no-file-descriptor` then we do not include the file descriptor in the generated schema. This is useful if you are trying to minimize the size of the generated schema.
438
438
 
439
439
  - With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration, or `--ts_proto_opt=outputTypeAnnotations=optional` to make it an optional property on the `interface` definition. The latter option may be useful if you want to use the `$type` field for runtime type checking on responses from a server.
440
440
 
@@ -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 (${varName}.value !== undefined ${(0, utils_1.withAndMaybeCheckIsNotNull)(options, `${varName}.value`)}) {
936
+ if (${ifValueCheck}${maybeIfKeyCheck}) {
933
937
  ${initializerSnippet}
934
938
  ${valueSetterSnippet};
935
939
  }
@@ -68,7 +68,7 @@ export type Options = {
68
68
  unrecognizedEnumName: string;
69
69
  unrecognizedEnumValue: number;
70
70
  exportCommonSymbols: boolean;
71
- outputSchema: boolean;
71
+ outputSchema: boolean | "no-file-descriptor";
72
72
  onlyTypes: boolean;
73
73
  emitImportedFiles: boolean;
74
74
  useAbortSignal: boolean;
@@ -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
@@ -103,6 +103,7 @@ function defaultOptions() {
103
103
  disableProto2DefaultValues: false,
104
104
  useNullAsOptional: false,
105
105
  annotateFilesWithVersion: true,
106
+ noDefaultsForOptionals: false,
106
107
  };
107
108
  }
108
109
  exports.defaultOptions = defaultOptions;
package/build/schema.js CHANGED
@@ -28,8 +28,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
28
28
  };
29
29
 
30
30
  export interface ProtoMetadata {
31
- fileDescriptor: ${fileDescriptorProto};
32
- references: { [key: string]: any };
31
+ ${ctx.options.outputSchema !== "no-file-descriptor" ? (0, ts_poet_1.code) `fileDescriptor: ${fileDescriptorProto};\n` : ""}references: { [key: string]: any };
33
32
  dependencies?: ProtoMetadata[];
34
33
  options?: {
35
34
  options?: { [key: string]: any };
@@ -142,8 +141,9 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
142
141
  });
143
142
  chunks.push((0, ts_poet_1.code) `
144
143
  export const ${(0, ts_poet_1.def)("protoMetadata")}: ProtoMetadata = {
145
- fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),
146
- references: { ${(0, ts_poet_1.joinCode)(references, { on: "," })} },
144
+ ${ctx.options.outputSchema !== "no-file-descriptor"
145
+ ? (0, ts_poet_1.code) `fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),\n`
146
+ : ""}references: { ${(0, ts_poet_1.joinCode)(references, { on: "," })} },
147
147
  dependencies: [${(0, ts_poet_1.joinCode)(dependencies, { on: "," })}],
148
148
  ${fileOptions || messagesOptions.length > 0 || servicesOptions.length > 0 || enumsOptions.length > 0
149
149
  ? (0, ts_poet_1.code) `options: {
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 { Options } from "./options";
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 options_1 = require("./options");
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 &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.176.3",
3
+ "version": "1.178.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",