ts-proto 1.137.0 → 1.137.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/README.markdown +30 -23
- package/build/encode.js +10 -2
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -5,8 +5,6 @@
|
|
|
5
5
|
|
|
6
6
|
> `ts-proto` transforms your `.proto` files into strongly-typed, idiomatic TypeScript files!
|
|
7
7
|
|
|
8
|
-
(Note, if you're a new user of ts-proto and using a modern TS setup with `esModuleInterop`, or want to use ts-proto in ESM / snowpack / vite, you need to also pass that as a `ts_proto_opt`.)
|
|
9
|
-
|
|
10
8
|
## Table of contents
|
|
11
9
|
|
|
12
10
|
- [ts-proto](#ts-proto)
|
|
@@ -14,6 +12,7 @@
|
|
|
14
12
|
- [Overview](#overview)
|
|
15
13
|
- [QuickStart](#quickstart)
|
|
16
14
|
- [Buf](#buf)
|
|
15
|
+
- [ESM](#esm)
|
|
17
16
|
- [Goals](#goals)
|
|
18
17
|
- [Example Types](#example-types)
|
|
19
18
|
- [Highlights](#highlights)
|
|
@@ -118,6 +117,13 @@ plugins:
|
|
|
118
117
|
- useExactTypes=...
|
|
119
118
|
```
|
|
120
119
|
|
|
120
|
+
## ESM
|
|
121
|
+
|
|
122
|
+
If you're using a modern TS setup with either `esModuleInterop` or running in an ESM environment, you'll need to pass `ts_proto_opt`s of:
|
|
123
|
+
|
|
124
|
+
* `esModuleInterop=true` if using `esModuleInterop` in your `tsconfig.json`, and
|
|
125
|
+
* `importSuffix=.js` if executing the generated ts-proto code in an ESM environment
|
|
126
|
+
|
|
121
127
|
# Goals
|
|
122
128
|
|
|
123
129
|
In terms of the code that `ts-proto` generates, the general goals are:
|
|
@@ -283,7 +289,9 @@ Generated code will be placed in the Gradle build directory.
|
|
|
283
289
|
|
|
284
290
|
- With `--ts_proto_opt=forceLong=long`, all 64-bit numbers will be parsed as instances of `Long` (using the [long](https://www.npmjs.com/package/long) library).
|
|
285
291
|
|
|
286
|
-
|
|
292
|
+
With `--ts_proto_opt=forceLong=string`, all 64-bit numbers will be output as strings.
|
|
293
|
+
|
|
294
|
+
With `--ts_proto_opt=forceLong=bigint`, all 64-bit numbers will be output as `BigInt`s. This option still uses the `long` library to encode/decode internally within `protobuf.js`, but then converts to/from `BigInt`s in the ts-proto-generated code.
|
|
287
295
|
|
|
288
296
|
The default behavior is `forceLong=number`, which will internally still use the `long` library to encode/decode values on the wire (so you will still see a `util.Long = Long` line in your output), but will convert the `long` values to `number` automatically for you. Note that a runtime error is thrown if, while doing this conversion, a 64-bit value is larger than can be correctly stored as a `number`.
|
|
289
297
|
|
|
@@ -567,7 +575,7 @@ The commands below assume you have **Docker** installed. To use a **local** copy
|
|
|
567
575
|
|
|
568
576
|
- Check out the [repository]() for the latest code.
|
|
569
577
|
- Run `yarn install` to install the dependencies.
|
|
570
|
-
- Run `yarn build:test`
|
|
578
|
+
- Run `yarn build:test` to generate the test files.
|
|
571
579
|
> _This runs the following commands:_
|
|
572
580
|
>
|
|
573
581
|
> - `proto2bin` — Converts integration test `.proto` files to `.bin`.
|
|
@@ -577,25 +585,24 @@ The commands below assume you have **Docker** installed. To use a **local** copy
|
|
|
577
585
|
|
|
578
586
|
**Workflow**
|
|
579
587
|
|
|
580
|
-
-
|
|
581
|
-
-
|
|
582
|
-
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
-
|
|
593
|
-
- Run `yarn
|
|
594
|
-
- Commit
|
|
595
|
-
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
- Create a pull request
|
|
588
|
+
- Add/update an integration test for your use case
|
|
589
|
+
- Either find an existing `integration/*` test that is close enough to your use case, e.g. has a `parameters.txt` that matches the `ts_proto_opt` params necessary to reproduce your use case
|
|
590
|
+
- If creating a new integration test:
|
|
591
|
+
- Make a new `integration/your-new-test/parameters.txt` with the necessary `ts_proto_opt` params
|
|
592
|
+
- Create a minimal `integration/your-new-test/your-new-test.proto` schema to reproduce your use case
|
|
593
|
+
- After any changes to `your-new-test.proto`, or an existing `integration/*.proto` file, run `yarn proto2bin`
|
|
594
|
+
- You can also leave `yarn watch` running, and it should "just do the right thing"
|
|
595
|
+
- 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
|
|
596
|
+
- Modify the `ts-proto` code generation logic:
|
|
597
|
+
- Most important logic is found in [src/main.ts](src/main.ts).
|
|
598
|
+
- After any changes to `src/*.ts` files, run `yarn bin2ts` to re-codegen all integration tests
|
|
599
|
+
- Or `yarn bin2ts your-new-test` to re-codegen a specific test
|
|
600
|
+
- Again leaving `yarn watch` running should "just do the right thing"
|
|
601
|
+
- Run `yarn test` to verify your changes pass all existing tests
|
|
602
|
+
- Commit and submit a PR
|
|
603
|
+
- Run `yarn format` to format the typescript files.
|
|
604
|
+
- Make sure to `git add` all of the `*.proto`, `*.bin`, and `*.ts` files in `integration/your-new-test`
|
|
605
|
+
- Sometimes checking in generated code is frowned upon, but given ts-proto's main job is to generate code, seeing the codegen diffs in PRs is helpful
|
|
599
606
|
|
|
600
607
|
**Dockerized Protoc**
|
|
601
608
|
|
package/build/encode.js
CHANGED
|
@@ -12,7 +12,11 @@ function generateEncoder(ctx, typeName) {
|
|
|
12
12
|
}
|
|
13
13
|
if (name == "Timestamp") {
|
|
14
14
|
const TimestampValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/timestamp", name);
|
|
15
|
-
|
|
15
|
+
let value = (0, ts_poet_1.code) `value`;
|
|
16
|
+
if (ctx.options.useDate === options_1.DateOption.DATE || ctx.options.useDate === options_1.DateOption.STRING) {
|
|
17
|
+
value = (0, ts_poet_1.code) `${ctx.utils.toTimestamp}(${value})`;
|
|
18
|
+
}
|
|
19
|
+
return (0, ts_poet_1.code) `${TimestampValue}.encode(${value}).finish()`;
|
|
16
20
|
}
|
|
17
21
|
if (name == "Struct") {
|
|
18
22
|
const StructType = (0, utils_1.impProto)(ctx.options, "google/protobuf/struct", name);
|
|
@@ -54,7 +58,11 @@ function generateDecoder(ctx, typeName) {
|
|
|
54
58
|
let TypeValue;
|
|
55
59
|
if (name == "Timestamp") {
|
|
56
60
|
TypeValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/timestamp", name);
|
|
57
|
-
|
|
61
|
+
const decoder = (0, ts_poet_1.code) `${TypeValue}.decode(value)`;
|
|
62
|
+
if (ctx.options.useDate === options_1.DateOption.DATE || ctx.options.useDate === options_1.DateOption.STRING) {
|
|
63
|
+
return (0, ts_poet_1.code) `${ctx.utils.fromTimestamp}(${decoder})`;
|
|
64
|
+
}
|
|
65
|
+
return decoder;
|
|
58
66
|
}
|
|
59
67
|
if (name == "Struct" || name == "ListValue") {
|
|
60
68
|
TypeValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/struct", name);
|