Skip to content

Commit

Permalink
feat: added Export to Go support VSCODE-411 (#567)
Browse files Browse the repository at this point in the history
* Added export to Go support

* specified export to go is only available when user is connected an in playground

---------

Co-authored-by: Alena Khineika <[email protected]>
  • Loading branch information
GaurabAryal and alenakhineika authored Jul 27, 2023
1 parent cf2fcc3 commit 3593f6c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Select queries and aggregations within your Playground files and translate them
* C#
* Python 3
* Ruby
* Go

![Export to language](resources/screenshots/export-to-language.gif)

Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@
"command": "mdb.exportToRuby",
"title": "MongoDB: Export To Ruby"
},
{
"command": "mdb.exportToGo",
"title": "MongoDB: Export To Go"
},
{
"command": "mdb.addConnection",
"title": "Add MongoDB Connection",
Expand Down Expand Up @@ -654,6 +658,10 @@
"command": "mdb.exportToNode",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true"
},
{
"command": "mdb.exportToGo",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true"
},
{
"command": "mdb.refreshPlaygroundsFromTreeView",
"when": "false"
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum EXTENSION_COMMANDS {
MDB_EXPORT_TO_CSHARP = 'mdb.exportToCsharp',
MDB_EXPORT_TO_NODE = 'mdb.exportToNode',
MDB_EXPORT_TO_RUBY = 'mdb.exportToRuby',
MDB_EXPORT_TO_GO = 'mdb.exportToGo',
MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS = 'mdb.changeExportToLanguageAddons',

MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',
Expand Down
11 changes: 11 additions & 0 deletions src/editors/playgroundSelectedCodeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ export default class PlaygroundSelectedCodeActionProvider
tooltip: 'Export To Ruby',
};
codeActions.push(exportToRubyCommand);

const exportToGoCommand = new vscode.CodeAction(
'Export To Go',
vscode.CodeActionKind.Empty
);
exportToGoCommand.command = {
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_GO,
title: 'Export To Go',
tooltip: 'Export To Go',
};
codeActions.push(exportToGoCommand);
}

return codeActions;
Expand Down
4 changes: 4 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ export default class MDBExtensionController implements vscode.Disposable {
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_RUBY, () =>
this._playgroundController.exportToLanguage(ExportToLanguages.RUBY)
);
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_GO, () =>
this._playgroundController.exportToLanguage(ExportToLanguages.GO)
);

this.registerCommand(
EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
(exportToLanguageAddons) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(6);
expect(codeActions.length).to.be.equal(7);
const actionCommand = codeActions[2].command;

if (actionCommand) {
Expand Down Expand Up @@ -217,7 +217,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(6);
expect(codeActions.length).to.be.equal(7);
const actionCommand = codeActions[2].command;

if (actionCommand) {
Expand Down Expand Up @@ -282,7 +282,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(6);
expect(codeActions.length).to.be.equal(7);
const actionCommand = codeActions[3].command;

if (actionCommand) {
Expand Down Expand Up @@ -351,7 +351,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(6);
expect(codeActions.length).to.be.equal(7);
const actionCommand = codeActions[1].command;

if (actionCommand) {
Expand Down Expand Up @@ -426,7 +426,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(6);
expect(codeActions.length).to.be.equal(7);
const actionCommand = codeActions[5].command;

if (actionCommand) {
Expand Down Expand Up @@ -474,6 +474,81 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
}
}
});

test('exports to go and includes driver syntax', async () => {
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
const selection = {
start: { line: 0, character: 24 },
end: { line: 0, character: 38 },
} as vscode.Selection;
const mode = ExportToLanguageMode.QUERY;
const activeTextEditor = {
document: { getText: () => textFromEditor },
} as vscode.TextEditor;

mdbTestExtension.testExtensionController._playgroundController._selectedText =
"{ name: '22' }";
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.selection =
selection;
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.mode =
mode;
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor =
activeTextEditor;

testCodeActionProvider.refresh({ selection, mode });

const codeActions = testCodeActionProvider.provideCodeActions();
expect(codeActions).to.exist;

if (codeActions) {
expect(codeActions.length).to.be.equal(7);
const actionCommand = codeActions[6].command;

if (actionCommand) {
expect(actionCommand.command).to.be.equal('mdb.exportToGo');
expect(actionCommand.title).to.be.equal('Export To Go');

await vscode.commands.executeCommand(actionCommand.command);

let expectedResult: PlaygroundResult = {
namespace: 'DATABASE_NAME.COLLECTION_NAME',
type: null,
content: 'bson.D{{"name", "22"}}',
language: 'go',
};
expect(
mdbTestExtension.testExtensionController._playgroundController
._playgroundResult
).to.be.deep.equal(expectedResult);

const codeLenses =
mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
expect(codeLenses.length).to.be.equal(2);

await vscode.commands.executeCommand(
'mdb.changeExportToLanguageAddons',
{
...mdbTestExtension.testExtensionController._playgroundController
._exportToLanguageCodeLensProvider._exportToLanguageAddons,
driverSyntax: true,
}
);

expectedResult = {
namespace: 'db.coll',
type: null,
content:
'// Requires the MongoDB Go Driver\n// https://go.mongodb.org/mongo-driver\nctx := context.TODO()\n\n// Set client options\nclientOptions := options.Client().ApplyURI("mongodb://localhost:27018/?appname=mongodb-vscode+0.0.0-dev.0")\n\n// Connect to MongoDB\nclient, err := mongo.Connect(ctx, clientOptions)\nif err != nil {\n log.Fatal(err)\n}\ndefer func() {\n if err := client.Disconnect(ctx); err != nil {\n log.Fatal(err)\n }\n}()\n\n// Find data\ncoll := client.Database("db").Collection("coll")\n_, err = coll.Find(ctx, bson.D{{"name", "22"}})\nif err != nil {\n log.Fatal(err)\n}',
language: 'go',
};

expect(
mdbTestExtension.testExtensionController._playgroundController
._playgroundResult
).to.be.deep.equal(expectedResult);
}
}
});
});

suite('the regular JS file', () => {
Expand Down
1 change: 1 addition & 0 deletions src/types/playgroundType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export enum ExportToLanguages {
CSHARP = 'csharp',
JAVASCRIPT = 'javascript',
RUBY = 'ruby',
GO = 'go',
}

export enum ExportToLanguageMode {
Expand Down

0 comments on commit 3593f6c

Please sign in to comment.