ts-proto 1.87.0 → 1.87.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.87.1](https://github.com/stephenh/ts-proto/compare/v1.87.0...v1.87.1) (2021-11-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * code generation for int64 map values in fromPartial and fromJson ([#395](https://github.com/stephenh/ts-proto/issues/395)) ([d3ea8eb](https://github.com/stephenh/ts-proto/commit/d3ea8eb69e19a5e45fcc1766c4af1194b17e48fc))
7
+
1
8
  # [1.87.0](https://github.com/stephenh/ts-proto/compare/v1.86.0...v1.87.0) (2021-11-16)
2
9
 
3
10
 
package/README.markdown CHANGED
@@ -70,7 +70,7 @@ It will also generate client implementations of `PingService`; currently [Twirp]
70
70
  - `npm install ts-proto`
71
71
  - `protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=. ./simple.proto`
72
72
  - (Note that the output parameter name, `ts_proto_out`, is named based on the suffix of the plugin's name, i.e. "ts_proto" suffix in the `--plugin=./node_modules/.bin/protoc-gen-ts_proto` parameter becomes the `_out` prefix, per `protoc`'s CLI conventions.)
73
- - On Windows, use `protoc --plugin=protoc-gen-ts_proto=.\node_modules\.bin\protoc-gen-ts_proto.cmd --ts_proto_out=. ./simple.proto`
73
+ - On Windows, use `protoc --plugin=protoc-gen-ts_proto=.\node_modules\.bin\protoc-gen-ts_proto.cmd --ts_proto_out=. ./simple.proto` (see [#93](https://github.com/stephenh/ts-proto/issues/93))
74
74
  - Ensure you're using a modern `protoc`, i.e. the original `protoc` `3.0.0` doesn't support the `_opt` flag
75
75
 
76
76
  This will generate `*.ts` source files for the given `*.proto` types.
package/build/main.js CHANGED
@@ -285,13 +285,15 @@ function makeDeepPartial(options, longs) {
285
285
  `;
286
286
  }
287
287
  const maybeExport = options.exportCommonSymbols ? 'export' : '';
288
- const maybeLong = options.forceLong === options_1.LongOption.LONG ? ts_poet_1.code ` | ${longs.Long}` : '';
288
+ // Allow passing longs as numbers or strings, nad we'll convert them
289
+ const maybeLong = options.forceLong === options_1.LongOption.LONG ? ts_poet_1.code ` : T extends ${longs.Long} ? string | number | Long ` : '';
289
290
  const keys = options.outputTypeRegistry ? ts_poet_1.code `Exclude<keyof T, '$type'>` : ts_poet_1.code `keyof T`;
290
291
  // Based on the type from ts-essentials
291
292
  const DeepPartial = ts_poet_1.conditionalOutput('DeepPartial', ts_poet_1.code `
292
- type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined${maybeLong};
293
- ${maybeExport} type DeepPartial<T> = T extends Builtin
293
+ type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
294
+ ${maybeExport} type DeepPartial<T> = T extends Builtin
294
295
  ? T
296
+ ${maybeLong}
295
297
  : T extends Array<infer U>
296
298
  ? Array<DeepPartial<U>>
297
299
  : T extends ReadonlyArray<infer U>
@@ -798,6 +800,9 @@ function generateFromJson(ctx, fullName, messageDesc) {
798
800
  return ts_poet_1.code `${utils.bytesFromBase64}(${from} as string)`;
799
801
  }
800
802
  }
803
+ else if (types_1.isLong(valueType) && options.forceLong === options_1.LongOption.LONG) {
804
+ return ts_poet_1.code `Long.fromString(${from} as string)`;
805
+ }
801
806
  else if (types_1.isEnum(valueType)) {
802
807
  return ts_poet_1.code `${from} as number`;
803
808
  }
@@ -919,6 +924,9 @@ function generateToJson(ctx, fullName, messageDesc) {
919
924
  else if (types_1.isTimestamp(valueType) && options.useDate === options_1.DateOption.TIMESTAMP) {
920
925
  return ts_poet_1.code `${utils.fromTimestamp}(${from}).toISOString()`;
921
926
  }
927
+ else if (types_1.isLong(valueType) && options.forceLong === options_1.LongOption.LONG) {
928
+ return ts_poet_1.code `${from}.toString()`;
929
+ }
922
930
  else if (types_1.isScalar(valueType) || types_1.isValueType(ctx, valueType)) {
923
931
  return ts_poet_1.code `${from}`;
924
932
  }
@@ -996,7 +1004,10 @@ function generateFromPartial(ctx, fullName, messageDesc) {
996
1004
  messageDesc.field.forEach((field) => {
997
1005
  const fieldName = case_1.maybeSnakeToCamel(field.name, options);
998
1006
  const readSnippet = (from) => {
999
- if (types_1.isPrimitive(field) ||
1007
+ if ((types_1.isLong(field) || types_1.isLongValueType(field)) && options.forceLong === options_1.LongOption.LONG) {
1008
+ return ts_poet_1.code `Long.fromValue(${from})`;
1009
+ }
1010
+ else if (types_1.isPrimitive(field) ||
1000
1011
  (types_1.isTimestamp(field) && (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) ||
1001
1012
  types_1.isValueType(ctx, field)) {
1002
1013
  return ts_poet_1.code `${from}`;
@@ -1011,6 +1022,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1011
1022
  else if (types_1.isEnum(valueType)) {
1012
1023
  return ts_poet_1.code `${from} as number`;
1013
1024
  }
1025
+ else if (types_1.isLong(valueType) && options.forceLong === options_1.LongOption.LONG) {
1026
+ return ts_poet_1.code `Long.fromValue(${from})`;
1027
+ }
1014
1028
  else {
1015
1029
  const cstr = case_1.capitalize(types_1.basicTypeName(ctx, valueType).toCodeString());
1016
1030
  return ts_poet_1.code `${cstr}(${from})`;
@@ -1071,16 +1085,6 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1071
1085
  }
1072
1086
  `);
1073
1087
  }
1074
- else if ((types_1.isLong(field) || types_1.isLongValueType(field)) && options.forceLong === options_1.LongOption.LONG) {
1075
- const v = readSnippet(`object.${fieldName}`);
1076
- const type = types_1.basicTypeName(ctx, field);
1077
- chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1078
- chunks.push(ts_poet_1.code `message.${fieldName} = ${v} as ${type};`);
1079
- chunks.push(ts_poet_1.code `} else {`);
1080
- const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1081
- chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
1082
- chunks.push(ts_poet_1.code `}`);
1083
- }
1084
1088
  else if (readSnippet(`x`).toCodeString() == 'x') {
1085
1089
  // An optimized case of the else below that works when `readSnippet` returns the plain input
1086
1090
  const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.87.0",
3
+ "version": "1.87.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",