Skip to content

Commit 3593f6c

Browse files
feat: added Export to Go support VSCODE-411 (#567)
* 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]>
1 parent cf2fcc3 commit 3593f6c

File tree

7 files changed

+106
-5
lines changed

7 files changed

+106
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Select queries and aggregations within your Playground files and translate them
4545
* C#
4646
* Python 3
4747
* Ruby
48+
* Go
4849

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

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@
243243
"command": "mdb.exportToRuby",
244244
"title": "MongoDB: Export To Ruby"
245245
},
246+
{
247+
"command": "mdb.exportToGo",
248+
"title": "MongoDB: Export To Go"
249+
},
246250
{
247251
"command": "mdb.addConnection",
248252
"title": "Add MongoDB Connection",
@@ -654,6 +658,10 @@
654658
"command": "mdb.exportToNode",
655659
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true"
656660
},
661+
{
662+
"command": "mdb.exportToGo",
663+
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true"
664+
},
657665
{
658666
"command": "mdb.refreshPlaygroundsFromTreeView",
659667
"when": "false"

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum EXTENSION_COMMANDS {
2222
MDB_EXPORT_TO_CSHARP = 'mdb.exportToCsharp',
2323
MDB_EXPORT_TO_NODE = 'mdb.exportToNode',
2424
MDB_EXPORT_TO_RUBY = 'mdb.exportToRuby',
25+
MDB_EXPORT_TO_GO = 'mdb.exportToGo',
2526
MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS = 'mdb.changeExportToLanguageAddons',
2627

2728
MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',

src/editors/playgroundSelectedCodeActionProvider.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ export default class PlaygroundSelectedCodeActionProvider
122122
tooltip: 'Export To Ruby',
123123
};
124124
codeActions.push(exportToRubyCommand);
125+
126+
const exportToGoCommand = new vscode.CodeAction(
127+
'Export To Go',
128+
vscode.CodeActionKind.Empty
129+
);
130+
exportToGoCommand.command = {
131+
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_GO,
132+
title: 'Export To Go',
133+
tooltip: 'Export To Go',
134+
};
135+
codeActions.push(exportToGoCommand);
125136
}
126137

127138
return codeActions;

src/mdbExtensionController.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ export default class MDBExtensionController implements vscode.Disposable {
224224
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_RUBY, () =>
225225
this._playgroundController.exportToLanguage(ExportToLanguages.RUBY)
226226
);
227+
this.registerCommand(EXTENSION_COMMANDS.MDB_EXPORT_TO_GO, () =>
228+
this._playgroundController.exportToLanguage(ExportToLanguages.GO)
229+
);
230+
227231
this.registerCommand(
228232
EXTENSION_COMMANDS.MDB_CHANGE_EXPORT_TO_LANGUAGE_ADDONS,
229233
(exportToLanguageAddons) =>

src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
181181
expect(codeActions).to.exist;
182182

183183
if (codeActions) {
184-
expect(codeActions.length).to.be.equal(6);
184+
expect(codeActions.length).to.be.equal(7);
185185
const actionCommand = codeActions[2].command;
186186

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

219219
if (codeActions) {
220-
expect(codeActions.length).to.be.equal(6);
220+
expect(codeActions.length).to.be.equal(7);
221221
const actionCommand = codeActions[2].command;
222222

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

284284
if (codeActions) {
285-
expect(codeActions.length).to.be.equal(6);
285+
expect(codeActions.length).to.be.equal(7);
286286
const actionCommand = codeActions[3].command;
287287

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

353353
if (codeActions) {
354-
expect(codeActions.length).to.be.equal(6);
354+
expect(codeActions.length).to.be.equal(7);
355355
const actionCommand = codeActions[1].command;
356356

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

428428
if (codeActions) {
429-
expect(codeActions.length).to.be.equal(6);
429+
expect(codeActions.length).to.be.equal(7);
430430
const actionCommand = codeActions[5].command;
431431

432432
if (actionCommand) {
@@ -474,6 +474,81 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
474474
}
475475
}
476476
});
477+
478+
test('exports to go and includes driver syntax', async () => {
479+
const textFromEditor = "use('db'); db.coll.find({ name: '22' })";
480+
const selection = {
481+
start: { line: 0, character: 24 },
482+
end: { line: 0, character: 38 },
483+
} as vscode.Selection;
484+
const mode = ExportToLanguageMode.QUERY;
485+
const activeTextEditor = {
486+
document: { getText: () => textFromEditor },
487+
} as vscode.TextEditor;
488+
489+
mdbTestExtension.testExtensionController._playgroundController._selectedText =
490+
"{ name: '22' }";
491+
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.selection =
492+
selection;
493+
mdbTestExtension.testExtensionController._playgroundController._playgroundSelectedCodeActionProvider.mode =
494+
mode;
495+
mdbTestExtension.testExtensionController._playgroundController._activeTextEditor =
496+
activeTextEditor;
497+
498+
testCodeActionProvider.refresh({ selection, mode });
499+
500+
const codeActions = testCodeActionProvider.provideCodeActions();
501+
expect(codeActions).to.exist;
502+
503+
if (codeActions) {
504+
expect(codeActions.length).to.be.equal(7);
505+
const actionCommand = codeActions[6].command;
506+
507+
if (actionCommand) {
508+
expect(actionCommand.command).to.be.equal('mdb.exportToGo');
509+
expect(actionCommand.title).to.be.equal('Export To Go');
510+
511+
await vscode.commands.executeCommand(actionCommand.command);
512+
513+
let expectedResult: PlaygroundResult = {
514+
namespace: 'DATABASE_NAME.COLLECTION_NAME',
515+
type: null,
516+
content: 'bson.D{{"name", "22"}}',
517+
language: 'go',
518+
};
519+
expect(
520+
mdbTestExtension.testExtensionController._playgroundController
521+
._playgroundResult
522+
).to.be.deep.equal(expectedResult);
523+
524+
const codeLenses =
525+
mdbTestExtension.testExtensionController._playgroundController._exportToLanguageCodeLensProvider.provideCodeLenses();
526+
expect(codeLenses.length).to.be.equal(2);
527+
528+
await vscode.commands.executeCommand(
529+
'mdb.changeExportToLanguageAddons',
530+
{
531+
...mdbTestExtension.testExtensionController._playgroundController
532+
._exportToLanguageCodeLensProvider._exportToLanguageAddons,
533+
driverSyntax: true,
534+
}
535+
);
536+
537+
expectedResult = {
538+
namespace: 'db.coll',
539+
type: null,
540+
content:
541+
'// 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}',
542+
language: 'go',
543+
};
544+
545+
expect(
546+
mdbTestExtension.testExtensionController._playgroundController
547+
._playgroundResult
548+
).to.be.deep.equal(expectedResult);
549+
}
550+
}
551+
});
477552
});
478553

479554
suite('the regular JS file', () => {

src/types/playgroundType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export enum ExportToLanguages {
4646
CSHARP = 'csharp',
4747
JAVASCRIPT = 'javascript',
4848
RUBY = 'ruby',
49+
GO = 'go',
4950
}
5051

5152
export enum ExportToLanguageMode {

0 commit comments

Comments
 (0)