-
Notifications
You must be signed in to change notification settings - Fork 343
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+228 Bytes
integration/use-objectid-true-external-import/objectid/objectid.bin
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
integration/use-objectid-true-external-import/objectid/objectid.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
integration/use-objectid-true-external-import/objectid/objectid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
useMongoObjectId=true |
51 changes: 51 additions & 0 deletions
51
integration/use-objectid-true-external-import/use-objectid-true-external-import-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+1014 Bytes
integration/use-objectid-true-external-import/use-objectid-true.bin
Binary file not shown.
13 changes: 13 additions & 0 deletions
13
integration/use-objectid-true-external-import/use-objectid-true.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 singlevalue: 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 tomongodb.ObjectId
in the generated types."There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 theisObjectId
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 & likemessage.fields.length === 1 & message.fields[0].name === value && message.fields[0].type === string
.