ts-proto 1.99.0 → 1.100.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,10 @@
1
+ # [1.100.0](https://github.com/stephenh/ts-proto/compare/v1.99.0...v1.100.0) (2022-01-09)
2
+
3
+
4
+ ### Features
5
+
6
+ * support mapping ObjectId message as mongodb.ObjectId ([#467](https://github.com/stephenh/ts-proto/issues/467)) ([8b23897](https://github.com/stephenh/ts-proto/commit/8b2389715ecfd5d51b1b24f5a9332e4ff9f09a27))
7
+
1
8
  # [1.99.0](https://github.com/stephenh/ts-proto/compare/v1.98.0...v1.99.0) (2022-01-07)
2
9
 
3
10
 
package/README.markdown CHANGED
@@ -175,6 +175,10 @@ creating a class and calling the right getters/setters.
175
175
 
176
176
  - `fromJSON`/`toJSON` use the [proto3 canonical JSON encoding format](https://developers.google.com/protocol-buffers/docs/proto3#json) (e.g. timestamps are ISO strings), unlike [`protobufjs`](https://github.com/protobufjs/protobuf.js/issues/1304).
177
177
 
178
+ - ObjectIds can be mapped as `mongodb.ObjectId`
179
+
180
+ (Configurable with the `useObjectId` parameter.)
181
+
178
182
  # Auto-Batching / N+1 Prevention
179
183
 
180
184
  (Note: this is currently only supported by the Twirp clients.)
@@ -347,6 +351,8 @@ Generated code will be placed in the Gradle build directory.
347
351
 
348
352
  - With `--ts_proto_opt=useDate=false`, fields of type `google.protobuf.Timestamp` will not be mapped to type `Date` in the generated types. See [Timestamp](#timestamp) for more details.
349
353
 
354
+ - With `--ts_proto_opt=useObjectId=true`, fields of a type called ObjectId where the message is constructed to have on field called value that is a string will be mapped to type `mongodb.ObjectId` in the generated types. This will require your project to install the mongodb npm package. See [ObjectId](#objectid) for more details.
355
+
350
356
  - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
351
357
 
352
358
  - With `--ts_proto_opt=outputTypeRegistry=true`, the type registry will be generated that can be used to resolve message types by fully-qualified name. Also, each message will get extra `$type` field containing fully-qualified name.
@@ -685,7 +691,6 @@ The representation of `google.protobuf.Timestamp` is configurable by the `useDat
685
691
  | --------------------------- | ---------------------- | ------------------------------------ | ---------------- |
686
692
  | `google.protobuf.Timestamp` | `Date` | `{ seconds: number, nanos: number }` | `string` |
687
693
 
688
-
689
694
  # Number Types
690
695
 
691
696
  Numbers are by default assumed to be plain JavaScript `number`s.
package/build/main.js CHANGED
@@ -193,6 +193,7 @@ function makeUtils(options) {
193
193
  return {
194
194
  ...bytes,
195
195
  ...makeDeepPartial(options, longs),
196
+ ...makeObjectIdMethods(options),
196
197
  ...makeTimestampMethods(options, longs),
197
198
  ...longs,
198
199
  ...makeComparisonUtils(),
@@ -325,6 +326,32 @@ function makeDeepPartial(options, longs) {
325
326
  `);
326
327
  return { Builtin, DeepPartial, Exact };
327
328
  }
329
+ function makeObjectIdMethods(options) {
330
+ const mongodb = ts_poet_1.imp('mongodb*mongodb');
331
+ const fromProtoObjectId = ts_poet_1.conditionalOutput('fromProtoObjectId', ts_poet_1.code `
332
+ function fromProtoObjectId(oid: ObjectId): ${mongodb}.ObjectId {
333
+ return new ${mongodb}.ObjectId(oid.value);
334
+ }
335
+ `);
336
+ const fromJsonObjectId = ts_poet_1.conditionalOutput('fromJsonObjectId', ts_poet_1.code `
337
+ function fromJsonObjectId(o: any): ${mongodb}.ObjectId {
338
+ if (o instanceof ${mongodb}.ObjectId) {
339
+ return o;
340
+ } else if (typeof o === "string") {
341
+ return new ${mongodb}.ObjectId(o);
342
+ } else {
343
+ return ${fromProtoObjectId}(ObjectId.fromJSON(o));
344
+ }
345
+ }
346
+ `);
347
+ const toProtoObjectId = ts_poet_1.conditionalOutput('toProtoObjectId', ts_poet_1.code `
348
+ function toProtoObjectId(oid: ${mongodb}.ObjectId): ObjectId {
349
+ const value = oid.toString();
350
+ return { value };
351
+ }
352
+ `);
353
+ return { fromJsonObjectId, fromProtoObjectId, toProtoObjectId };
354
+ }
328
355
  function makeTimestampMethods(options, longs) {
329
356
  const Timestamp = utils_1.impProto(options, 'google/protobuf/timestamp', 'Timestamp');
330
357
  let seconds = 'date.getTime() / 1_000';
@@ -575,6 +602,10 @@ function generateDecode(ctx, fullName, messageDesc) {
575
602
  const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
576
603
  readSnippet = ts_poet_1.code `${utils.fromTimestamp}(${type}.decode(reader, reader.uint32()))`;
577
604
  }
605
+ else if (types_1.isObjectId(field) && options.useMongoObjectId) {
606
+ const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
607
+ readSnippet = ts_poet_1.code `${utils.fromProtoObjectId}(${type}.decode(reader, reader.uint32()))`;
608
+ }
578
609
  else if (types_1.isMessage(field)) {
579
610
  const type = types_1.basicTypeName(ctx, field);
580
611
  readSnippet = ts_poet_1.code `${type}.decode(reader, reader.uint32())`;
@@ -659,6 +690,11 @@ function generateEncode(ctx, fullName, messageDesc) {
659
690
  const tag = ((field.number << 3) | types_1.basicWireType(field.type)) >>> 0;
660
691
  writeSnippet = (place) => ts_poet_1.code `writer.uint32(${tag}).${types_1.toReaderCall(field)}(${place})`;
661
692
  }
693
+ else if (types_1.isObjectId(field) && options.useMongoObjectId) {
694
+ const tag = ((field.number << 3) | 2) >>> 0;
695
+ const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
696
+ writeSnippet = (place) => ts_poet_1.code `${type}.encode(${utils.toProtoObjectId}(${place}), writer.uint32(${tag}).fork()).ldelim()`;
697
+ }
662
698
  else if (types_1.isTimestamp(field) && (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) {
663
699
  const tag = ((field.number << 3) | 2) >>> 0;
664
700
  const type = types_1.basicTypeName(ctx, field, { keepValueType: true });
@@ -852,6 +888,9 @@ function generateFromJson(ctx, fullName, messageDesc) {
852
888
  return ts_poet_1.code `${cstr}(${from})`;
853
889
  }
854
890
  }
891
+ else if (types_1.isObjectId(field) && options.useMongoObjectId) {
892
+ return ts_poet_1.code `${utils.fromJsonObjectId}(${from})`;
893
+ }
855
894
  else if (types_1.isTimestamp(field) && options.useDate === options_1.DateOption.STRING) {
856
895
  return ts_poet_1.code `String(${from})`;
857
896
  }
@@ -901,6 +940,9 @@ function generateFromJson(ctx, fullName, messageDesc) {
901
940
  return ts_poet_1.code `${cstr}(${from})`;
902
941
  }
903
942
  }
943
+ else if (types_1.isObjectId(valueType) && options.useMongoObjectId) {
944
+ return ts_poet_1.code `${utils.fromJsonObjectId}(${from})`;
945
+ }
904
946
  else if (types_1.isTimestamp(valueType) && options.useDate === options_1.DateOption.STRING) {
905
947
  return ts_poet_1.code `String(${from})`;
906
948
  }
@@ -1022,6 +1064,9 @@ function generateToJson(ctx, fullName, messageDesc) {
1022
1064
  ? ts_poet_1.code `${from} !== undefined ? ${toJson}(${from}) : undefined`
1023
1065
  : ts_poet_1.code `${toJson}(${from})`;
1024
1066
  }
1067
+ else if (types_1.isObjectId(field) && options.useMongoObjectId) {
1068
+ return ts_poet_1.code `${from}.toString()`;
1069
+ }
1025
1070
  else if (types_1.isTimestamp(field) && options.useDate === options_1.DateOption.DATE) {
1026
1071
  return ts_poet_1.code `${from}.toISOString()`;
1027
1072
  }
@@ -1041,6 +1086,9 @@ function generateToJson(ctx, fullName, messageDesc) {
1041
1086
  else if (types_1.isBytes(valueType)) {
1042
1087
  return ts_poet_1.code `${utils.base64FromBytes}(${from})`;
1043
1088
  }
1089
+ else if (types_1.isObjectId(valueType) && options.useMongoObjectId) {
1090
+ return ts_poet_1.code `${from}.toString()`;
1091
+ }
1044
1092
  else if (types_1.isTimestamp(valueType) && options.useDate === options_1.DateOption.DATE) {
1045
1093
  return ts_poet_1.code `${from}.toISOString()`;
1046
1094
  }
@@ -1152,6 +1200,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1152
1200
  if ((types_1.isLong(field) || types_1.isLongValueType(field)) && options.forceLong === options_1.LongOption.LONG) {
1153
1201
  return ts_poet_1.code `Long.fromValue(${from})`;
1154
1202
  }
1203
+ else if (types_1.isObjectId(field) && options.useMongoObjectId) {
1204
+ return ts_poet_1.code `${from} as mongodb.ObjectId`;
1205
+ }
1155
1206
  else if (types_1.isPrimitive(field) ||
1156
1207
  (types_1.isTimestamp(field) && (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) ||
1157
1208
  types_1.isValueType(ctx, field)) {
@@ -1178,6 +1229,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1178
1229
  else if (types_1.isAnyValueType(valueType)) {
1179
1230
  return ts_poet_1.code `${from}`;
1180
1231
  }
1232
+ else if (types_1.isObjectId(valueType) && options.useMongoObjectId) {
1233
+ return ts_poet_1.code `${from} as mongodb.ObjectId`;
1234
+ }
1181
1235
  else if (types_1.isTimestamp(valueType) &&
1182
1236
  (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) {
1183
1237
  return ts_poet_1.code `${from}`;
package/build/options.js CHANGED
@@ -38,6 +38,7 @@ function defaultOptions() {
38
38
  forceLong: LongOption.NUMBER,
39
39
  useOptionals: 'none',
40
40
  useDate: DateOption.DATE,
41
+ useMongoObjectId: false,
41
42
  oneof: OneofOption.PROPERTIES,
42
43
  esModuleInterop: false,
43
44
  fileSuffix: '',
package/build/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isMapType = exports.isWholeNumber = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isOptionalProperty = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
3
+ exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isObjectId = exports.isMapType = exports.isWholeNumber = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isOptionalProperty = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
4
4
  const ts_proto_descriptors_1 = require("ts-proto-descriptors");
5
5
  const ts_poet_1 = require("ts-poet");
6
6
  const options_1 = require("./options");
@@ -378,6 +378,11 @@ function isMapType(ctx, messageDesc, field) {
378
378
  return detectMapType(ctx, messageDesc, field) !== undefined;
379
379
  }
380
380
  exports.isMapType = isMapType;
381
+ function isObjectId(field) {
382
+ // need to use endsWith instead of === because objectid could be imported from an external proto file
383
+ return field.typeName.endsWith('.ObjectId');
384
+ }
385
+ exports.isObjectId = isObjectId;
381
386
  function isTimestamp(field) {
382
387
  return field.typeName === '.google.protobuf.Timestamp';
383
388
  }
@@ -510,6 +515,10 @@ function messageToTypeName(ctx, protoType, typeOptions = {}) {
510
515
  return ts_poet_1.code `string`;
511
516
  }
512
517
  }
518
+ // need to use endsWith instead of === because objectid could be imported from an external proto file
519
+ if (!typeOptions.keepValueType && options.useMongoObjectId && protoType.endsWith('.ObjectId')) {
520
+ return ts_poet_1.code `mongodb.ObjectId`;
521
+ }
513
522
  const [module, type] = toModuleAndType(typeMap, protoType);
514
523
  return ts_poet_1.code `${utils_1.impProto(options, module, type)}`;
515
524
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.99.0",
3
+ "version": "1.100.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",
@@ -48,6 +48,7 @@
48
48
  "@types/node": "^14.14.37",
49
49
  "chokidar": "^3.5.2",
50
50
  "jest": "^26.6.3",
51
+ "mongodb": "^4.3.0",
51
52
  "prettier": "^2.2.1",
52
53
  "reflect-metadata": "^0.1.13",
53
54
  "rxjs": "^7.4.0",