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: show simpler uuid format VSCODE-470 #701

Merged
merged 9 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
32 changes: 31 additions & 1 deletion src/editors/mongoDBDocumentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const DOCUMENT_SOURCE_URI_IDENTIFIER = 'source';

export const VIEW_DOCUMENT_SCHEME = 'VIEW_DOCUMENT_SCHEME';

const isObject = (value: unknown) =>
value !== null && typeof value === 'object' && !Array.isArray(value);

export default class MongoDBDocumentService {
_context: vscode.ExtensionContext;
_connectionController: ConnectionController;
Expand Down Expand Up @@ -108,6 +111,32 @@ export default class MongoDBDocumentService {
}
}

_simplifyEJSON(document: Document): Document {
for (const [key, item] of Object.entries(document)) {
// UUIDs might be represented as {"$uuid": <canonical textual representation of a UUID>} in EJSON
// Binary subtypes 3 or 4 are used to represent UUIDs in BSON
// But, parsers MUST interpret the $uuid key as BSON Binary subtype 4
// For this reason, we are applying this representation for subtype 4 only
// see https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#special-rules-for-parsing-uuid-fields
if (
isObject(item) &&
item.hasOwnProperty('$binary') &&
item.$binary.subType === '04'
paula-stacho marked this conversation as resolved.
Show resolved Hide resolved
) {
const hexString = Buffer.from(item.$binary.base64, 'base64').toString(
'hex'
);
const match = /^(.{8})(.{4})(.{4})(.{4})(.{12})$/.exec(hexString);
if (!match) continue;
const asUUID = match.slice(1, 6).join('-');
document[key] = {
$uuid: asUUID,
};
}
}
return document;
}

async fetchDocument(data: EditDocumentInfo): Promise<Document | void> {
log.info('Fetch document from MongoDB', data);

Expand Down Expand Up @@ -147,7 +176,8 @@ export default class MongoDBDocumentService {
return;
}

return JSON.parse(EJSON.stringify(documents[0]));
const ejson = JSON.parse(EJSON.stringify(documents[0]));
return this._simplifyEJSON(ejson);
paula-stacho marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
this._statusView.hideMessage();

Expand Down
106 changes: 105 additions & 1 deletion src/test/suite/editors/mongoDBDocumentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,57 @@ suite('MongoDB Document Service Test Suite', () => {
expect(document).to.be.deep.equal(newDocument);
});

test('replaceDocument calls findOneAndReplace and saves a document when connected - extending the uuid type', async () => {
const namespace = 'waffle.house';
const connectionId = 'tasty_sandwhich';
const documentId = '93333a0d-83f6-4e6f-a575-af7ea6187a4a';
const document: { _id: string; myUuid?: { $uuid: string } } = {
_id: '123',
};
const newDocument = {
_id: '123',
myUuid: {
$binary: {
base64: 'yO2rw/c4TKO2jauSqRR4ow==',
subType: '04',
},
},
};
const source = DocumentSource.DOCUMENT_SOURCE_TREEVIEW;

const fakeActiveConnectionId = sandbox.fake.returns('tasty_sandwhich');
sandbox.replace(
testConnectionController,
'getActiveConnectionId',
fakeActiveConnectionId
);

const fakeGetActiveDataService = sandbox.fake.returns({
findOneAndReplace: () => {
document.myUuid = { $uuid: 'c8edabc3-f738-4ca3-b68d-ab92a91478a3' };

return Promise.resolve(document);
},
});
sandbox.replace(
testConnectionController,
'getActiveDataService',
fakeGetActiveDataService
);
sandbox.stub(testStatusView, 'showMessage');
sandbox.stub(testStatusView, 'hideMessage');

await testMongoDBDocumentService.replaceDocument({
namespace,
documentId,
connectionId,
newDocument,
source,
});

expect(document).to.be.deep.equal(document);
});

test('fetchDocument calls find and returns a single document when connected', async () => {
const namespace = 'waffle.house';
const connectionId = 'tasty_sandwhich';
Expand All @@ -97,7 +148,7 @@ suite('MongoDB Document Service Test Suite', () => {

const fakeGetActiveDataService = sandbox.fake.returns({
find: () => {
return Promise.resolve([{ _id: '123' }]);
return Promise.resolve(documents);
},
});
sandbox.replace(
Expand Down Expand Up @@ -127,6 +178,59 @@ suite('MongoDB Document Service Test Suite', () => {
expect(result).to.be.deep.equal(JSON.parse(EJSON.stringify(documents[0])));
});

test('fetchDocument calls find and returns a single document when connected - simplifying the uuid type', async () => {
const namespace = 'waffle.house';
const connectionId = 'tasty_sandwhich';
const documentId = '93333a0d-83f6-4e6f-a575-af7ea6187a4a';
const line = 1;
const documents = [
{
_id: '123',
myUuid: {
$binary: {
base64: 'yO2rw/c4TKO2jauSqRR4ow==',
subType: '04',
},
},
},
];
const source = DocumentSource.DOCUMENT_SOURCE_PLAYGROUND;

const fakeGetActiveDataService = sandbox.fake.returns({
find: () => {
return Promise.resolve(documents);
},
});
sandbox.replace(
testConnectionController,
'getActiveDataService',
fakeGetActiveDataService
);

const fakeGetActiveConnectionId = sandbox.fake.returns(connectionId);
sandbox.replace(
testConnectionController,
'getActiveConnectionId',
fakeGetActiveConnectionId
);

sandbox.stub(testStatusView, 'showMessage');
sandbox.stub(testStatusView, 'hideMessage');

const result = await testMongoDBDocumentService.fetchDocument({
namespace,
documentId,
line,
connectionId,
source,
});

expect(result).to.be.deep.equal({
_id: '123',
myUuid: { $uuid: 'c8edabc3-f738-4ca3-b68d-ab92a91478a3' },
});
});

test("if a user is not connected, documents won't be saved to MongoDB", async () => {
const namespace = 'waffle.house';
const connectionId = 'tasty_sandwhich';
Expand Down
Loading