ts-proto 1.137.1 → 1.138.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
@@ -167,6 +167,10 @@ Along with `encode`/`decode` factory methods:
167
167
 
168
168
  ```typescript
169
169
  export const Simple = {
170
+ create(baseObject?: DeepPartial<Simple>): Simple {
171
+ ...
172
+ },
173
+
170
174
  encode(message: Simple, writer: Writer = Writer.create()): Writer {
171
175
  ...
172
176
  },
@@ -332,7 +336,7 @@ Generated code will be placed in the Gradle build directory.
332
336
 
333
337
  So, between typo-prevention, reader inconsistency, and proper initialization, ts-proto recommends using `useOptionals=none` as the "most safe" option.
334
338
 
335
- All that said, this approach does require writers/creators to set every field (although `fromPartial` is meant to address this), so if you still want to have optional fields, you can set `useOptionals=messages` or `useOptionals=all`.
339
+ All that said, this approach does require writers/creators to set every field (although `fromPartial` and `create` are meant to address this), so if you still want to have optional fields, you can set `useOptionals=messages` or `useOptionals=all`.
336
340
 
337
341
  (See [this issue](https://github.com/stephenh/ts-proto/issues/120#issuecomment-678375833) and [this issue](https://github.com/stephenh/ts-proto/issues/397#issuecomment-977259118) for discussions on `useOptional`.)
338
342
 
@@ -368,7 +372,7 @@ Generated code will be placed in the Gradle build directory.
368
372
 
369
373
  This is also useful if you want "only types".
370
374
 
371
- - With `--ts_proto_opt=outputPartialMethods=false`, the `Message.fromPartial` methods for accepting partially-formed objects/object literals will not be output.
375
+ - With `--ts_proto_opt=outputPartialMethods=false`, the `Message.fromPartial` and `Message.create` methods for accepting partially-formed objects/object literals will not be output.
372
376
 
373
377
  - With `--ts_proto_opt=stringEnums=true`, the generated enum types will be string-based instead of int-based.
374
378
 
@@ -433,9 +437,9 @@ Generated code will be placed in the Gradle build directory.
433
437
 
434
438
  - With `--ts_proto_opt=enumsAsLiterals=true`, the generated enum types will be enum-ish object with `as const`.
435
439
 
436
- - With `--ts_proto_opt=useExactTypes=false`, the generated `fromPartial` method will not use Exact types.
440
+ - With `--ts_proto_opt=useExactTypes=false`, the generated `fromPartial` and `create` methods will not use Exact types.
437
441
 
438
- The default behavior is `useExactTypes=true`, which makes `fromPartial` use Exact type for its argument to make TypeScript reject any unknown properties.
442
+ The default behavior is `useExactTypes=true`, which makes `fromPartial` and `create` use Exact type for its argument to make TypeScript reject any unknown properties.
439
443
 
440
444
  - With `--ts_proto_opt=unknownFields=true`, all unknown fields will be parsed and output as arrays of buffers.
441
445
 
package/build/main.js CHANGED
@@ -904,10 +904,11 @@ function generateEncode(ctx, fullName, messageDesc) {
904
904
  else {
905
905
  // Ideally we'd reuse `writeSnippet` but it has tagging embedded inside of it.
906
906
  const tag = ((field.number << 3) | 2) >>> 0;
907
+ const rhs = (x) => ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.BIGINT ? `${x}.toString()` : x);
907
908
  const listWriteSnippet = (0, ts_poet_1.code) `
908
909
  writer.uint32(${tag}).fork();
909
910
  for (const v of message.${fieldName}) {
910
- writer.${(0, types_1.toReaderCall)(field)}(v);
911
+ writer.${(0, types_1.toReaderCall)(field)}(${rhs("v")});
911
912
  }
912
913
  writer.ldelim();
913
914
  `;
@@ -1388,9 +1389,24 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
1388
1389
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
1389
1390
  }
1390
1391
  function generateFromPartial(ctx, fullName, messageDesc) {
1391
- const { options, utils, typeMap } = ctx;
1392
+ const { options, utils } = ctx;
1392
1393
  const chunks = [];
1393
- // create the basic function declaration
1394
+ // create the create function definition
1395
+ if (ctx.options.useExactTypes) {
1396
+ chunks.push((0, ts_poet_1.code) `
1397
+ create<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(base?: I): ${fullName} {
1398
+ `);
1399
+ }
1400
+ else {
1401
+ chunks.push((0, ts_poet_1.code) `
1402
+ create(base?: ${utils.DeepPartial}<${fullName}>): ${fullName} {
1403
+ `);
1404
+ }
1405
+ chunks.push((0, ts_poet_1.code) `
1406
+ return ${fullName}.fromPartial(base ?? {})
1407
+ `);
1408
+ chunks.push((0, ts_poet_1.code) `},`, (0, ts_poet_1.code) ``);
1409
+ // create the fromPartial function declaration
1394
1410
  const paramName = messageDesc.field.length > 0 ? "object" : "_";
1395
1411
  if (ctx.options.useExactTypes) {
1396
1412
  chunks.push((0, ts_poet_1.code) `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.137.1",
3
+ "version": "1.138.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",