ts-proto 1.132.0 → 1.133.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.133.0](https://github.com/stephenh/ts-proto/compare/v1.132.1...v1.133.0) (2022-11-20)
2
+
3
+
4
+ ### Features
5
+
6
+ * optional one of properties ([#705](https://github.com/stephenh/ts-proto/issues/705)) ([4c6cbb0](https://github.com/stephenh/ts-proto/commit/4c6cbb0ff71e053c634732e295ed812901d368ae))
7
+
8
+ ## [1.132.1](https://github.com/stephenh/ts-proto/compare/v1.132.0...v1.132.1) (2022-11-15)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * use-readonly-types for oneof unions ([#706](https://github.com/stephenh/ts-proto/issues/706)) ([bc854ba](https://github.com/stephenh/ts-proto/commit/bc854bac083b8818d5fad77bc2e7995cfb01798e))
14
+
1
15
  # [1.132.0](https://github.com/stephenh/ts-proto/compare/v1.131.2...v1.132.0) (2022-11-15)
2
16
 
3
17
 
package/build/main.js CHANGED
@@ -342,8 +342,8 @@ function makeDeepPartial(options, longs) {
342
342
  let oneofCase = "";
343
343
  if (options.oneof === options_1.OneofOption.UNIONS) {
344
344
  oneofCase = `
345
- : T extends { $case: string }
346
- ? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & { $case: T['$case'] }
345
+ : T extends { ${maybeReadonly(options)}$case: string }
346
+ ? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & { ${maybeReadonly(options)}$case: T['$case'] }
347
347
  `;
348
348
  }
349
349
  const maybeExport = options.exportCommonSymbols ? "export" : "";
@@ -530,7 +530,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
530
530
  const name = (0, case_1.maybeSnakeToCamel)(fieldDesc.name, options);
531
531
  const type = (0, types_1.toTypeName)(ctx, messageDesc, fieldDesc);
532
532
  const q = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options) ? "?" : "";
533
- chunks.push((0, ts_poet_1.code) `${ctx.options.useReadonlyTypes ? "readonly " : ""}${name}${q}: ${type}, `);
533
+ chunks.push((0, ts_poet_1.code) `${maybeReadonly(options)}${name}${q}: ${type}, `);
534
534
  });
535
535
  chunks.push((0, ts_poet_1.code) `}`);
536
536
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
@@ -541,10 +541,10 @@ function generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo) {
541
541
  const unionType = (0, ts_poet_1.joinCode)(fields.map((f) => {
542
542
  let fieldName = (0, case_1.maybeSnakeToCamel)(f.name, options);
543
543
  let typeName = (0, types_1.toTypeName)(ctx, messageDesc, f);
544
- return (0, ts_poet_1.code) `{ $case: '${fieldName}', ${fieldName}: ${typeName} }`;
544
+ return (0, ts_poet_1.code) `{ ${maybeReadonly(options)}$case: '${fieldName}', ${maybeReadonly(options)}${fieldName}: ${typeName} }`;
545
545
  }), { on: " | " });
546
546
  const name = (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[oneofIndex].name, options);
547
- return (0, ts_poet_1.code) `${name}?: ${unionType},`;
547
+ return (0, ts_poet_1.code) `${maybeReadonly(options)}${name}?: ${unionType},`;
548
548
  /*
549
549
  // Ideally we'd put the comments for each oneof field next to the anonymous
550
550
  // type we've created in the type union above, but ts-poet currently lacks
@@ -624,7 +624,7 @@ function generateDecode(ctx, fullName, messageDesc) {
624
624
  const reader = input instanceof ${Reader} ? input : new ${Reader}(input);
625
625
  let end = length === undefined ? reader.len : reader.pos + length;
626
626
  `);
627
- chunks.push((0, ts_poet_1.code) `const message = ${createBase}${options.useReadonlyTypes ? " as any" : ""};`);
627
+ chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
628
628
  if (options.unknownFields) {
629
629
  chunks.push((0, ts_poet_1.code) `(message as any)._unknownFields = {}`);
630
630
  }
@@ -1377,7 +1377,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1377
1377
  if (options.usePrototypeForDefaults) {
1378
1378
  createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
1379
1379
  }
1380
- chunks.push((0, ts_poet_1.code) `const message = ${createBase}${options.useReadonlyTypes ? " as any" : ""};`);
1380
+ chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
1381
1381
  // add a check for each incoming field
1382
1382
  messageDesc.field.forEach((field) => {
1383
1383
  const fieldName = (0, case_1.maybeSnakeToCamel)(field.name, options);
@@ -1524,7 +1524,7 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
1524
1524
  if ((0, types_1.isAnyValueTypeName)(fullProtoTypeName)) {
1525
1525
  if (ctx.options.oneof === options_1.OneofOption.UNIONS) {
1526
1526
  chunks.push((0, ts_poet_1.code) `wrap(value: any): Value {
1527
- const result = createBaseValue()${ctx.options.useReadonlyTypes ? " as any" : ""};
1527
+ const result = createBaseValue()${maybeAsAny(ctx.options)};
1528
1528
 
1529
1529
  if (value === null) {
1530
1530
  result.kind = {$case: '${fieldNames.nullValue}', ${fieldNames.nullValue}: NullValue.NULL_VALUE};
@@ -1547,7 +1547,7 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
1547
1547
  }
1548
1548
  else {
1549
1549
  chunks.push((0, ts_poet_1.code) `wrap(value: any): Value {
1550
- const result = createBaseValue()${ctx.options.useReadonlyTypes ? " as any" : ""};
1550
+ const result = createBaseValue()${maybeAsAny(ctx.options)};
1551
1551
 
1552
1552
  if (value === null) {
1553
1553
  result.${fieldNames.nullValue} = NullValue.NULL_VALUE;
@@ -1571,7 +1571,7 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
1571
1571
  }
1572
1572
  if ((0, types_1.isListValueTypeName)(fullProtoTypeName)) {
1573
1573
  chunks.push((0, ts_poet_1.code) `wrap(value: ${ctx.options.useReadonlyTypes ? "ReadonlyArray<any>" : "Array<any>"} | undefined): ListValue {
1574
- const result = createBaseListValue()${ctx.options.useReadonlyTypes ? " as any" : ""};
1574
+ const result = createBaseListValue()${maybeAsAny(ctx.options)};
1575
1575
 
1576
1576
  result.values = value ?? [];
1577
1577
 
@@ -1579,8 +1579,8 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
1579
1579
  }`);
1580
1580
  }
1581
1581
  if ((0, types_1.isFieldMaskTypeName)(fullProtoTypeName)) {
1582
- chunks.push((0, ts_poet_1.code) `wrap(paths: ${ctx.options.useReadonlyTypes ? "readonly " : ""} string[]): FieldMask {
1583
- const result = createBaseFieldMask()${ctx.options.useReadonlyTypes ? " as any" : ""};
1582
+ chunks.push((0, ts_poet_1.code) `wrap(paths: ${maybeReadonly(ctx.options)} string[]): FieldMask {
1583
+ const result = createBaseFieldMask()${maybeAsAny(ctx.options)};
1584
1584
 
1585
1585
  result.paths = paths;
1586
1586
 
@@ -1661,3 +1661,9 @@ function maybeCastToNumber(ctx, messageDesc, field, variableName) {
1661
1661
  return `Number(${variableName})`;
1662
1662
  }
1663
1663
  }
1664
+ function maybeReadonly(options) {
1665
+ return options.useReadonlyTypes ? "readonly " : "";
1666
+ }
1667
+ function maybeAsAny(options) {
1668
+ return options.useReadonlyTypes ? " as any" : "";
1669
+ }
package/build/types.js CHANGED
@@ -320,11 +320,14 @@ exports.isScalar = isScalar;
320
320
  // properties. When useOptionals='all', all fields are translated into
321
321
  // optional properties, with the exception of map Entry key/values, which must
322
322
  // always be present.
323
+ // OneOf fields are always optional, whenever oneof=unions option not in use.
323
324
  function isOptionalProperty(field, messageOptions, options) {
324
325
  const optionalMessages = options.useOptionals === true || options.useOptionals === "messages" || options.useOptionals === "all";
325
326
  const optionalAll = options.useOptionals === "all";
326
327
  return ((optionalMessages && isMessage(field) && !isRepeated(field)) ||
327
328
  (optionalAll && !(messageOptions === null || messageOptions === void 0 ? void 0 : messageOptions.mapEntry)) ||
329
+ // don't bother verifying that oneof is not union. union oneofs generate their own properties.
330
+ isWithinOneOf(field) ||
328
331
  field.proto3Optional);
329
332
  }
330
333
  exports.isOptionalProperty = isOptionalProperty;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.132.0",
3
+ "version": "1.133.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",