ts-proto 1.176.2 → 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 +8 -4
- package/build/main.js +9 -3
- 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 +4 -6
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).
|
|
@@ -687,8 +692,7 @@ The commands below assume you have **Docker** installed. If you are using OS X,
|
|
|
687
692
|
- Run `yarn build:test` to generate the test files.
|
|
688
693
|
> _This runs the following commands:_
|
|
689
694
|
>
|
|
690
|
-
> - `
|
|
691
|
-
> - `bin2ts` — Runs `ts-proto` on the `.bin` files to generate `.ts` files.
|
|
695
|
+
> - `proto2ts` — Runs `ts-proto` on the `integration/**/*.proto` files to generate `.ts` files.
|
|
692
696
|
> - `proto2pbjs` — Generates a reference implementation using `pbjs` for testing compatibility.
|
|
693
697
|
- Run `yarn test`
|
|
694
698
|
|
|
@@ -704,8 +708,8 @@ The commands below assume you have **Docker** installed. If you are using OS X,
|
|
|
704
708
|
- Add/update a `integration/your-new-test/some-test.ts` unit test, even if it's as trivial as just making sure the generated code compiles
|
|
705
709
|
- Modify the `ts-proto` code generation logic:
|
|
706
710
|
- Most important logic is found in [src/main.ts](src/main.ts).
|
|
707
|
-
- After any changes to `src/*.ts` files, run `yarn
|
|
708
|
-
- Or `yarn
|
|
711
|
+
- After any changes to `src/*.ts` files, run `yarn proto2ts` to re-codegen all integration tests
|
|
712
|
+
- Or `yarn proto2ts your-new-test` to re-codegen a specific test
|
|
709
713
|
- Again leaving `yarn watch` running should "just do the right thing"
|
|
710
714
|
- Run `yarn test` to verify your changes pass all existing tests
|
|
711
715
|
- Commit and submit a PR
|
package/build/main.js
CHANGED
|
@@ -334,7 +334,6 @@ function makeLongUtils(options, bytes) {
|
|
|
334
334
|
${configure}();
|
|
335
335
|
}
|
|
336
336
|
`);
|
|
337
|
-
// TODO This is unused?
|
|
338
337
|
const numberToLong = (0, ts_poet_1.conditionalOutput)("numberToLong", (0, ts_poet_1.code) `
|
|
339
338
|
function numberToLong(number: number) {
|
|
340
339
|
return ${Long}.fromNumber(number);
|
|
@@ -355,6 +354,9 @@ function makeLongUtils(options, bytes) {
|
|
|
355
354
|
if (long.gt(${bytes.globalThis}.Number.MAX_SAFE_INTEGER)) {
|
|
356
355
|
throw new ${bytes.globalThis}.Error("Value is larger than Number.MAX_SAFE_INTEGER")
|
|
357
356
|
}
|
|
357
|
+
if (long.lt(${bytes.globalThis}.Number.MIN_SAFE_INTEGER)) {
|
|
358
|
+
throw new ${bytes.globalThis}.Error("Value is smaller than Number.MIN_SAFE_INTEGER")
|
|
359
|
+
}
|
|
358
360
|
return long.toNumber();
|
|
359
361
|
}
|
|
360
362
|
`);
|
|
@@ -507,7 +509,7 @@ function makeTimestampMethods(options, longs, bytes) {
|
|
|
507
509
|
const NanoDate = (0, ts_poet_1.imp)("NanoDate=nano-date");
|
|
508
510
|
let seconds = "Math.trunc(date.getTime() / 1_000)";
|
|
509
511
|
let toNumberCode = "t.seconds";
|
|
510
|
-
const makeToNumberCode = (methodCall) => `t.seconds${options.useOptionals === "all" ? "?" : ""}.${methodCall}`;
|
|
512
|
+
const makeToNumberCode = (methodCall) => `t.seconds${options.useOptionals === "all" || options.noDefaultsForOptionals ? "?" : ""}.${methodCall}`;
|
|
511
513
|
if (options.forceLong === options_1.LongOption.LONG) {
|
|
512
514
|
toNumberCode = makeToNumberCode("toNumber()");
|
|
513
515
|
seconds = (0, ts_poet_1.code) `${longs.numberToLong}(${seconds})`;
|
|
@@ -924,10 +926,14 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
924
926
|
${messageProperty} = ${generateMapType ? "new Map()" : "{}"};
|
|
925
927
|
}`
|
|
926
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
|
+
: ""}`;
|
|
927
933
|
chunks.push((0, ts_poet_1.code) `
|
|
928
934
|
${tagCheck}
|
|
929
935
|
const ${varName} = ${readSnippet};
|
|
930
|
-
if (${
|
|
936
|
+
if (${ifValueCheck}${maybeIfKeyCheck}) {
|
|
931
937
|
${initializerSnippet}
|
|
932
938
|
${valueSetterSnippet};
|
|
933
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 &&
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-proto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.177.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/plugin.js",
|
|
6
6
|
"repository": "github:stephenh/ts-proto",
|
|
@@ -10,12 +10,10 @@
|
|
|
10
10
|
"types": "build/index.d.ts",
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "yarn tsc",
|
|
13
|
-
"build:test": "yarn
|
|
13
|
+
"build:test": "yarn proto2ts && yarn proto2pbjs",
|
|
14
14
|
"prepare": "yarn build",
|
|
15
|
-
"
|
|
16
|
-
"proto2bin-node": "docker compose run --rm node update-bins.sh",
|
|
15
|
+
"proto2ts": "docker compose run --rm protoc update-code.sh",
|
|
17
16
|
"proto2pbjs": "docker compose run --rm protoc pbjs.sh",
|
|
18
|
-
"bin2ts": "docker compose run --rm protoc codegen.sh",
|
|
19
17
|
"test": "yarn jest -c jest.config.js",
|
|
20
18
|
"tsc:check": "./tsc-check.sh tsconfig.json tests/tsconfig.json integration/tsconfig.json integration/tsconfig.proto.json protos/tsconfig.json",
|
|
21
19
|
"format": "prettier --write {src,tests}/**/*.ts integration/*.ts",
|
|
@@ -62,7 +60,7 @@
|
|
|
62
60
|
"rxjs": "^7.8.1",
|
|
63
61
|
"semantic-release": "^21.0.7",
|
|
64
62
|
"ts-jest": "^29.1.1",
|
|
65
|
-
"tsx": "^
|
|
63
|
+
"tsx": "^4.11.2",
|
|
66
64
|
"typescript": "^5.1.6",
|
|
67
65
|
"uglify-js": "^3.17.4"
|
|
68
66
|
},
|