Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support mapping ObjectId message as mongodb.ObjectId #467

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ creating a class and calling the right getters/setters.

- `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).

- ObjectIds can be mapped as `mongodb.ObjectId`

(Configurable with the `useObjectId` parameter.)

# Auto-Batching / N+1 Prevention

(Note: this is currently only supported by the Twirp clients.)
Expand Down Expand Up @@ -333,6 +337,8 @@ Generated code will be placed in the Gradle build directory.

- 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.

- 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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pedantically, afaiu we don't actually look inside of the ObjectId protobuf type to see that it only has a single value: string field; currently we're just matching on the type name and calling that good enough.

Which is fine, but let's update the docs to match that, and just say "protobuf fields of a type ObjectId will be mapped to mongodb.ObjectId in the generated types."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code relies on the stucture of the objectid protobuf message has that field value that's type string. that's how we convert between a mongodb.ObjectId and a protoObjectId

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, agreed we rely on .value but technically the isObjectId function is only checking the name of the message type:

https://github.com/stephenh/ts-proto/pull/467/files#diff-c54113cf61ec99691748a3890bfbeb00e10efb3f0a76f03a0fd9ec49072e410aR407

Just the way this is worded, "With ...this setting..., fields called ObjectId where the message ... has field called value" made me think that isObjectId really was going to check both message type name & like message.fields.length === 1 & message.fields[0].name === value && message.fields[0].type === string.


- With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.

- 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.
Expand Down Expand Up @@ -671,7 +677,6 @@ The representation of `google.protobuf.Timestamp` is configurable by the `useDat
| --------------------------- | ---------------------- | ------------------------------------ | ---------------- |
| `google.protobuf.Timestamp` | `Date` | `{ seconds: number, nanos: number }` | `string` |


# Number Types

Numbers are by default assumed to be plain JavaScript `number`s.
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package foo.objectid;

message ObjectId {
string value = 1;
}
86 changes: 86 additions & 0 deletions integration/use-objectid-true-external-import/objectid/objectid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable */
import { util, configure, Writer, Reader } from 'protobufjs/minimal';
import * as Long from 'long';

export const protobufPackage = 'foo.objectid';

export interface ObjectId {
value: string;
}

function createBaseObjectId(): ObjectId {
return { value: '' };
}

export const ObjectId = {
encode(message: ObjectId, writer: Writer = Writer.create()): Writer {
if (message.value !== '') {
writer.uint32(10).string(message.value);
}
return writer;
},

decode(input: Reader | Uint8Array, length?: number): ObjectId {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseObjectId();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.value = reader.string();
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): ObjectId {
return {
value: isSet(object.value) ? String(object.value) : '',
};
},

toJSON(message: ObjectId): unknown {
const obj: any = {};
message.value !== undefined && (obj.value = message.value);
return obj;
},

fromPartial<I extends Exact<DeepPartial<ObjectId>, I>>(object: I): ObjectId {
const message = createBaseObjectId();
message.value = object.value ?? '';
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
? T
: T extends Array<infer U>
? Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin
? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & Record<Exclude<keyof I, KeysOfUnion<P>>, never>;

// If you get a compile-error about 'Constructor<Long> and ... have no overlap',
// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'.
if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
useMongoObjectId=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Todo } from './use-objectid-true';

import * as mongodb from 'mongodb';

const id1 = new mongodb.ObjectId();
const id2 = new mongodb.ObjectId();

describe('useMongoObjectId=true External Import', () => {
it('generates types that compile and encode', () => {
const output = Todo.encode({
id: '6883ed6e-bd0d-4817-ba58-c2a53c73edc2',
oid: id1,
repeatedOid: [id1, id2],
mapOfOids: {
id1,
id2,
},
}).finish();

expect(Todo.decode(output)).toMatchInlineSnapshot(`
Object {
"id": "6883ed6e-bd0d-4817-ba58-c2a53c73edc2",
"mapOfOids": Object {
"id1": "${id1.toString()}",
"id2": "${id2.toString()}",
},
"oid": "${id1.toString()}",
"optionalOid": undefined,
"repeatedOid": Array [
"${id1.toString()}",
"${id2.toString()}",
],
}
`);

expect(Todo.toJSON(Todo.decode(output))).toMatchInlineSnapshot(`
Object {
"id": "6883ed6e-bd0d-4817-ba58-c2a53c73edc2",
"mapOfOids": Object {
"id1": "${id1.toString()}",
"id2": "${id2.toString()}",
},
"oid": "${id1.toString()}",
"repeatedOid": Array [
"${id1.toString()}",
"${id2.toString()}",
],
}
`);
});
});
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package foo;

import "objectid/objectid.proto";

message Todo {
string id = 1;
foo.objectid.ObjectId oid = 2;
repeated foo.objectid.ObjectId repeated_oid = 3;
optional foo.objectid.ObjectId optional_oid = 4;
map<string, foo.objectid.ObjectId> map_of_oids = 5;
}
Loading