Skip to content

Commit

Permalink
Merge pull request #268 from dolittle/remove-mongo-extension
Browse files Browse the repository at this point in the history
Remove the Binary.toGuid() to make TS builds work
  • Loading branch information
jakhog committed Feb 22, 2022
2 parents 349b3fe + 0b282eb commit 7610edf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
31 changes: 11 additions & 20 deletions Source/typescript/backend/graphql/GuidExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,20 @@ Guid.prototype.toMUUID = function (): MUUID {
return parseMUUID(uuid);
};

declare module 'mongodb' {
interface Binary {
/**
* Converts a {@link MUUID} to a {@link Guid}
*/
toGuid(): Guid;
}
}
export function binaryToGuid(binary: Binary): Guid {
const rearrangedBytes = [...binary.buffer];

Binary.prototype.toGuid = function (): Guid {
const rearrangedBytes = [...this.buffer];
rearrangedBytes[0] = binary.buffer[3];
rearrangedBytes[1] = binary.buffer[2];
rearrangedBytes[2] = binary.buffer[1];
rearrangedBytes[3] = binary.buffer[0];

rearrangedBytes[0] = this.buffer[3];
rearrangedBytes[1] = this.buffer[2];
rearrangedBytes[2] = this.buffer[1];
rearrangedBytes[3] = this.buffer[0];
rearrangedBytes[4] = binary.buffer[5];
rearrangedBytes[5] = binary.buffer[4];

rearrangedBytes[4] = this.buffer[5];
rearrangedBytes[5] = this.buffer[4];

rearrangedBytes[6] = this.buffer[7];
rearrangedBytes[7] = this.buffer[6];
rearrangedBytes[6] = binary.buffer[7];
rearrangedBytes[7] = binary.buffer[6];

const guid = new Guid(rearrangedBytes);
return guid;
};
}
6 changes: 3 additions & 3 deletions Source/typescript/backend/mongodb/GuidCustomType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Binary } from 'mongodb';
import { Guid } from '@dolittle/rudiments';
import { CustomType } from './index';
import '../graphql/GuidExtensions';
import { binaryToGuid } from '../graphql/GuidExtensions';


/**
Expand All @@ -23,9 +23,9 @@ export class GuidCustomType extends CustomType<Guid> {

/** @inheritdoc */
fromBSON(value: Binary): Guid {
if (!value || !(value as any).toGuid) {
if (!value || !binaryToGuid(value)) {
return Guid.empty;
}
return value.toGuid();
return binaryToGuid(value);
}
}

0 comments on commit 7610edf

Please sign in to comment.