ts-proto 1.115.1 → 1.115.4

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,24 @@
1
+ ## [1.115.4](https://github.com/stephenh/ts-proto/compare/v1.115.3...v1.115.4) (2022-06-05)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Struct wrap/unwrap snake case [#579](https://github.com/stephenh/ts-proto/issues/579) ([#588](https://github.com/stephenh/ts-proto/issues/588)) ([33f89bf](https://github.com/stephenh/ts-proto/commit/33f89bfcf0d5d9e64a6fd360eaefc140055e9291))
7
+
8
+ ## [1.115.3](https://github.com/stephenh/ts-proto/compare/v1.115.2...v1.115.3) (2022-06-03)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Fix ts-poet typo in package.json. ([#589](https://github.com/stephenh/ts-proto/issues/589)) ([5211347](https://github.com/stephenh/ts-proto/commit/5211347cb21ac7d117349a32a87c0d017999c44a))
14
+
15
+ ## [1.115.2](https://github.com/stephenh/ts-proto/compare/v1.115.1...v1.115.2) (2022-06-03)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * simplify handling useJsonWireFormat=true and fix onlyTypes=true ([#583](https://github.com/stephenh/ts-proto/issues/583)) ([6e7f938](https://github.com/stephenh/ts-proto/commit/6e7f9387d144f506ded982604f32981ac5e327de))
21
+
1
22
  ## [1.115.1](https://github.com/stephenh/ts-proto/compare/v1.115.0...v1.115.1) (2022-06-02)
2
23
 
3
24
 
package/README.markdown CHANGED
@@ -396,7 +396,7 @@ Generated code will be placed in the Gradle build directory.
396
396
 
397
397
  - With `--ts_proto_opt=onlyTypes=true`, only types will be emitted, and imports for `long` and `protobufjs/minimal` will be excluded.
398
398
 
399
- Note: _This is a combination_ of `outputJsonMethods=false,outputEncodeMethods=false,outputClientImpl=false,nestJs=false`
399
+ This is the same as setting `outputJsonMethods=false,outputEncodeMethods=false,outputClientImpl=false,nestJs=false`
400
400
 
401
401
  - With `--ts_proto_opt=usePrototypeForDefaults=true`, the generated code will wrap new objects with `Object.create`.
402
402
 
@@ -411,12 +411,6 @@ Generated code will be placed in the Gradle build directory.
411
411
  Requires `onlyTypes=true`. Implies `useDate=string` and `stringEnums=true`. This option is to generate types that can be directly used with marshalling/unmarshalling Protobuf messages serialized as JSON.
412
412
  You may also want to set `useOptionals=all`, as gRPC gateways are not required to send default value for scalar values.
413
413
 
414
- ### Only Types
415
-
416
- If you're looking for `ts-proto` to generate only types for your Protobuf types then passing all three of `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` as `false` is probably what you want, i.e.:
417
-
418
- `--ts_proto_opt=onlyTypes=true`.
419
-
420
414
  ### NestJS Support
421
415
 
422
416
  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/enums.js CHANGED
@@ -20,16 +20,15 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
20
20
  chunks.push(ts_poet_1.code `export ${options.constEnums ? 'const ' : ''}enum ${ts_poet_1.def(fullName)} {`);
21
21
  }
22
22
  const delimiter = options.enumsAsLiterals ? ':' : '=';
23
- const stringEnums = options.stringEnums || (options.onlyTypes && options.useJsonWireFormat);
24
23
  enumDesc.value.forEach((valueDesc, index) => {
25
24
  var _a;
26
25
  const info = sourceInfo.lookup(sourceInfo_1.Fields.enum.value, index);
27
26
  utils_1.maybeAddComment(info, chunks, (_a = valueDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated, `${valueDesc.name} - `);
28
- chunks.push(ts_poet_1.code `${valueDesc.name} ${delimiter} ${stringEnums ? `"${valueDesc.name}"` : valueDesc.number.toString()},`);
27
+ chunks.push(ts_poet_1.code `${valueDesc.name} ${delimiter} ${options.stringEnums ? `"${valueDesc.name}"` : valueDesc.number.toString()},`);
29
28
  });
30
29
  if (options.unrecognizedEnum)
31
30
  chunks.push(ts_poet_1.code `
32
- ${UNRECOGNIZED_ENUM_NAME} ${delimiter} ${stringEnums ? `"${UNRECOGNIZED_ENUM_NAME}"` : UNRECOGNIZED_ENUM_VALUE.toString()},`);
31
+ ${UNRECOGNIZED_ENUM_NAME} ${delimiter} ${options.stringEnums ? `"${UNRECOGNIZED_ENUM_NAME}"` : UNRECOGNIZED_ENUM_VALUE.toString()},`);
33
32
  if (options.enumsAsLiterals) {
34
33
  chunks.push(ts_poet_1.code `} as const`);
35
34
  chunks.push(ts_poet_1.code `\n`);
@@ -38,7 +37,7 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
38
37
  else {
39
38
  chunks.push(ts_poet_1.code `}`);
40
39
  }
41
- if (options.outputJsonMethods || (stringEnums && options.outputEncodeMethods)) {
40
+ if (options.outputJsonMethods || (options.stringEnums && options.outputEncodeMethods)) {
42
41
  chunks.push(ts_poet_1.code `\n`);
43
42
  chunks.push(generateEnumFromJson(ctx, fullName, enumDesc));
44
43
  }
@@ -46,7 +45,7 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
46
45
  chunks.push(ts_poet_1.code `\n`);
47
46
  chunks.push(generateEnumToJson(ctx, fullName, enumDesc));
48
47
  }
49
- if (stringEnums && options.outputEncodeMethods) {
48
+ if (options.stringEnums && options.outputEncodeMethods) {
50
49
  chunks.push(ts_poet_1.code `\n`);
51
50
  chunks.push(generateEnumToNumber(ctx, fullName, enumDesc));
52
51
  }
package/build/main.js CHANGED
@@ -87,8 +87,16 @@ function generateFile(ctx, fileDesc) {
87
87
  if (options.outputPartialMethods) {
88
88
  staticMembers.push(generateFromPartial(ctx, fullName, message));
89
89
  }
90
- staticMembers.push(...generateWrap(ctx, fullTypeName));
91
- staticMembers.push(...generateUnwrap(ctx, fullTypeName));
90
+ const structFieldNames = {
91
+ nullValue: case_1.maybeSnakeToCamel('null_value', ctx.options),
92
+ numberValue: case_1.maybeSnakeToCamel('number_value', ctx.options),
93
+ stringValue: case_1.maybeSnakeToCamel('string_value', ctx.options),
94
+ boolValue: case_1.maybeSnakeToCamel('bool_value', ctx.options),
95
+ structValue: case_1.maybeSnakeToCamel('struct_value', ctx.options),
96
+ listValue: case_1.maybeSnakeToCamel('list_value', ctx.options),
97
+ };
98
+ staticMembers.push(...generateWrap(ctx, fullTypeName, structFieldNames));
99
+ staticMembers.push(...generateUnwrap(ctx, fullTypeName, structFieldNames));
92
100
  chunks.push(ts_poet_1.code `
93
101
  export const ${ts_poet_1.def(fullName)} = {
94
102
  ${ts_poet_1.joinCode(staticMembers, { on: ',\n\n' })}
@@ -1405,7 +1413,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1405
1413
  chunks.push(ts_poet_1.code `}`);
1406
1414
  return ts_poet_1.joinCode(chunks, { on: '\n' });
1407
1415
  }
1408
- function generateWrap(ctx, fullProtoTypeName) {
1416
+ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
1409
1417
  const chunks = [];
1410
1418
  if (types_1.isStructTypeName(fullProtoTypeName)) {
1411
1419
  chunks.push(ts_poet_1.code `wrap(object: {[key: string]: any} | undefined): Struct {
@@ -1424,17 +1432,17 @@ function generateWrap(ctx, fullProtoTypeName) {
1424
1432
  const result = createBaseValue();
1425
1433
 
1426
1434
  if (value === null) {
1427
- result.kind = {$case: 'nullValue', nullValue: NullValue.NULL_VALUE};
1435
+ result.kind = {$case: '${fieldNames.nullValue}', ${fieldNames.nullValue}: NullValue.NULL_VALUE};
1428
1436
  } else if (typeof value === 'boolean') {
1429
- result.kind = {$case: 'boolValue', boolValue: value};
1437
+ result.kind = {$case: '${fieldNames.boolValue}', ${fieldNames.boolValue}: value};
1430
1438
  } else if (typeof value === 'number') {
1431
- result.kind = {$case: 'numberValue', numberValue: value};
1439
+ result.kind = {$case: '${fieldNames.numberValue}', ${fieldNames.numberValue}: value};
1432
1440
  } else if (typeof value === 'string') {
1433
- result.kind = {$case: 'stringValue', stringValue: value};
1441
+ result.kind = {$case: '${fieldNames.stringValue}', ${fieldNames.stringValue}: value};
1434
1442
  } else if (Array.isArray(value)) {
1435
- result.kind = {$case: 'listValue', listValue: value};
1443
+ result.kind = {$case: '${fieldNames.listValue}', ${fieldNames.listValue}: value};
1436
1444
  } else if (typeof value === 'object') {
1437
- result.kind = {$case: 'structValue', structValue: value};
1445
+ result.kind = {$case: '${fieldNames.structValue}', ${fieldNames.structValue}: value};
1438
1446
  } else if (typeof value !== 'undefined') {
1439
1447
  throw new Error('Unsupported any value type: ' + typeof value);
1440
1448
  }
@@ -1447,17 +1455,17 @@ function generateWrap(ctx, fullProtoTypeName) {
1447
1455
  const result = createBaseValue();
1448
1456
 
1449
1457
  if (value === null) {
1450
- result.nullValue = NullValue.NULL_VALUE;
1458
+ result.${fieldNames.nullValue} = NullValue.NULL_VALUE;
1451
1459
  } else if (typeof value === 'boolean') {
1452
- result.boolValue = value;
1460
+ result.${fieldNames.boolValue} = value;
1453
1461
  } else if (typeof value === 'number') {
1454
- result.numberValue = value;
1462
+ result.${fieldNames.numberValue} = value;
1455
1463
  } else if (typeof value === 'string') {
1456
- result.stringValue = value;
1464
+ result.${fieldNames.stringValue} = value;
1457
1465
  } else if (Array.isArray(value)) {
1458
- result.listValue = value;
1466
+ result.${fieldNames.listValue} = value;
1459
1467
  } else if (typeof value === 'object') {
1460
- result.structValue = value;
1468
+ result.${fieldNames.structValue} = value;
1461
1469
  } else if (typeof value !== 'undefined') {
1462
1470
  throw new Error('Unsupported any value type: ' + typeof value);
1463
1471
  }
@@ -1482,7 +1490,7 @@ function generateWrap(ctx, fullProtoTypeName) {
1482
1490
  }
1483
1491
  return chunks;
1484
1492
  }
1485
- function generateUnwrap(ctx, fullProtoTypeName) {
1493
+ function generateUnwrap(ctx, fullProtoTypeName, fieldNames) {
1486
1494
  const chunks = [];
1487
1495
  if (types_1.isStructTypeName(fullProtoTypeName)) {
1488
1496
  chunks.push(ts_poet_1.code `unwrap(message: Struct): {[key: string]: any} {
@@ -1496,18 +1504,18 @@ function generateUnwrap(ctx, fullProtoTypeName) {
1496
1504
  if (types_1.isAnyValueTypeName(fullProtoTypeName)) {
1497
1505
  if (ctx.options.oneof === options_1.OneofOption.UNIONS) {
1498
1506
  chunks.push(ts_poet_1.code `unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
1499
- if (message.kind?.$case === 'nullValue') {
1507
+ if (message.kind?.$case === '${fieldNames.nullValue}') {
1500
1508
  return null;
1501
- } else if (message.kind?.$case === 'numberValue') {
1502
- return message.kind?.numberValue;
1503
- } else if (message.kind?.$case === 'stringValue') {
1504
- return message.kind?.stringValue;
1505
- } else if (message.kind?.$case === 'boolValue') {
1506
- return message.kind?.boolValue;
1507
- } else if (message.kind?.$case === 'structValue') {
1508
- return message.kind?.structValue;
1509
- } else if (message.kind?.$case === 'listValue') {
1510
- return message.kind?.listValue;
1509
+ } else if (message.kind?.$case === '${fieldNames.numberValue}') {
1510
+ return message.kind?.${fieldNames.numberValue};
1511
+ } else if (message.kind?.$case === '${fieldNames.stringValue}') {
1512
+ return message.kind?.${fieldNames.stringValue};
1513
+ } else if (message.kind?.$case === '${fieldNames.boolValue}') {
1514
+ return message.kind?.${fieldNames.boolValue};
1515
+ } else if (message.kind?.$case === '${fieldNames.structValue}') {
1516
+ return message.kind?.${fieldNames.structValue};
1517
+ } else if (message.kind?.$case === '${fieldNames.listValue}') {
1518
+ return message.kind?.${fieldNames.listValue};
1511
1519
  } else {
1512
1520
  return undefined;
1513
1521
  }
@@ -1515,17 +1523,17 @@ function generateUnwrap(ctx, fullProtoTypeName) {
1515
1523
  }
1516
1524
  else {
1517
1525
  chunks.push(ts_poet_1.code `unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
1518
- if (message?.stringValue !== undefined) {
1519
- return message.stringValue;
1520
- } else if (message?.numberValue !== undefined) {
1521
- return message.numberValue;
1522
- } else if (message?.boolValue !== undefined) {
1523
- return message.boolValue;
1524
- } else if (message?.structValue !== undefined) {
1525
- return message.structValue;
1526
- } else if (message?.listValue !== undefined) {
1527
- return message.listValue;
1528
- } else if (message?.nullValue !== undefined) {
1526
+ if (message?.${fieldNames.stringValue} !== undefined) {
1527
+ return message.${fieldNames.stringValue};
1528
+ } else if (message?.${fieldNames.numberValue} !== undefined) {
1529
+ return message.${fieldNames.numberValue};
1530
+ } else if (message?.${fieldNames.boolValue} !== undefined) {
1531
+ return message.${fieldNames.boolValue};
1532
+ } else if (message?.${fieldNames.structValue} !== undefined) {
1533
+ return message.${fieldNames.structValue};
1534
+ } else if (message?.${fieldNames.listValue} !== undefined) {
1535
+ return message.${fieldNames.listValue};
1536
+ } else if (message?.${fieldNames.nullValue} !== undefined) {
1529
1537
  return null;
1530
1538
  }
1531
1539
  return undefined;
package/build/options.js CHANGED
@@ -88,8 +88,17 @@ function optionsFromParameter(parameter) {
88
88
  }
89
89
  Object.assign(options, parsed);
90
90
  }
91
- // We should promote onlyTypes to its own documented flag, but just an alias for now
92
- if (!options.outputJsonMethods && !options.outputEncodeMethods && !options.outputClientImpl && !options.nestJs) {
91
+ // onlyTypes=true implies outputJsonMethods=false,outputEncodeMethods=false,outputClientImpl=false,nestJs=false
92
+ if (options.onlyTypes) {
93
+ options.outputJsonMethods = false;
94
+ options.outputEncodeMethods = false;
95
+ options.outputClientImpl = false;
96
+ options.nestJs = false;
97
+ }
98
+ else if (!options.outputJsonMethods &&
99
+ !options.outputEncodeMethods &&
100
+ !options.outputClientImpl &&
101
+ !options.nestJs) {
93
102
  options.onlyTypes = true;
94
103
  }
95
104
  // Treat forceLong=true as LONG
@@ -124,6 +133,17 @@ function optionsFromParameter(parameter) {
124
133
  else if (typeof options.snakeToCamel === 'string') {
125
134
  options.snakeToCamel = [options.snakeToCamel];
126
135
  }
136
+ if (options.useJsonWireFormat) {
137
+ if (!options.onlyTypes) {
138
+ // useJsonWireFormat requires onlyTypes=true
139
+ options.useJsonWireFormat = false;
140
+ }
141
+ else {
142
+ // useJsonWireFormat implies stringEnums=true and useDate=string
143
+ options.stringEnums = true;
144
+ options.useDate = DateOption.STRING;
145
+ }
146
+ }
127
147
  return options;
128
148
  }
129
149
  exports.optionsFromParameter = optionsFromParameter;
package/build/types.js CHANGED
@@ -436,7 +436,6 @@ function isEmptyType(typeName) {
436
436
  }
437
437
  exports.isEmptyType = isEmptyType;
438
438
  function valueTypeName(ctx, typeName) {
439
- const useJsonWireFormat = ctx.options.onlyTypes && ctx.options.useJsonWireFormat;
440
439
  switch (typeName) {
441
440
  case '.google.protobuf.StringValue':
442
441
  return ts_poet_1.code `string`;
@@ -452,7 +451,11 @@ function valueTypeName(ctx, typeName) {
452
451
  case '.google.protobuf.BoolValue':
453
452
  return ts_poet_1.code `boolean`;
454
453
  case '.google.protobuf.BytesValue':
455
- return ctx.options.env === options_1.EnvOption.NODE ? ts_poet_1.code `Buffer` : useJsonWireFormat ? ts_poet_1.code `string` : ts_poet_1.code `Uint8Array`;
454
+ return ctx.options.env === options_1.EnvOption.NODE
455
+ ? ts_poet_1.code `Buffer`
456
+ : ctx.options.useJsonWireFormat
457
+ ? ts_poet_1.code `string`
458
+ : ts_poet_1.code `Uint8Array`;
456
459
  case '.google.protobuf.ListValue':
457
460
  return ts_poet_1.code `Array<any>`;
458
461
  case '.google.protobuf.Value':
@@ -460,11 +463,11 @@ function valueTypeName(ctx, typeName) {
460
463
  case '.google.protobuf.Struct':
461
464
  return ts_poet_1.code `{[key: string]: any}`;
462
465
  case '.google.protobuf.FieldMask':
463
- return useJsonWireFormat ? ts_poet_1.code `string` : ts_poet_1.code `string[]`;
466
+ return ctx.options.useJsonWireFormat ? ts_poet_1.code `string` : ts_poet_1.code `string[]`;
464
467
  case '.google.protobuf.Duration':
465
- return useJsonWireFormat ? ts_poet_1.code `string` : undefined;
468
+ return ctx.options.useJsonWireFormat ? ts_poet_1.code `string` : undefined;
466
469
  case '.google.protobuf.Timestamp':
467
- return useJsonWireFormat ? ts_poet_1.code `string` : undefined;
470
+ return ctx.options.useJsonWireFormat ? ts_poet_1.code `string` : undefined;
468
471
  default:
469
472
  return undefined;
470
473
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.115.1",
3
+ "version": "1.115.4",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",
@@ -66,7 +66,7 @@
66
66
  "dataloader": "^1.4.0",
67
67
  "object-hash": "^1.3.1",
68
68
  "protobufjs": "^6.11.3",
69
- "ts-poet": "^t 4.11.0",
69
+ "ts-poet": "^4.11.0",
70
70
  "ts-proto-descriptors": "1.6.0"
71
71
  }
72
72
  }