-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support mapping ObjectId message as mongodb.ObjectId (#467)
- Loading branch information
1 parent
a5bb22c
commit 8b23897
Showing
16 changed files
with
601 additions
and
12 deletions.
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.