From ca63bcb678bdfac7e8ebc65ca57d70cdcf2a04e0 Mon Sep 17 00:00:00 2001 From: Gaurab Aryal Date: Thu, 20 Jul 2023 20:53:11 -0400 Subject: [PATCH 1/2] Added export to Go support --- README.md | 1 + package.json | 4 + src/commands/index.ts | 1 + .../playgroundSelectedCodeActionProvider.ts | 11 +++ src/mdbExtensionController.ts | 4 + ...aygroundSelectedCodeActionProvider.test.ts | 85 +++++++++++++++++-- src/types/playgroundType.ts | 1 + 7 files changed, 102 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9c9fbede1..50b0cfe91 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index e82408462..3e17c2a15 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/index.ts b/src/commands/index.ts index b6eccf45a..0b8dcbc89 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -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', diff --git a/src/editors/playgroundSelectedCodeActionProvider.ts b/src/editors/playgroundSelectedCodeActionProvider.ts index ec412b3b9..19ccb25fb 100644 --- a/src/editors/playgroundSelectedCodeActionProvider.ts +++ b/src/editors/playgroundSelectedCodeActionProvider.ts @@ -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; diff --git a/src/mdbExtensionController.ts b/src/mdbExtensionController.ts index 246a70add..e14e512f5 100644 --- a/src/mdbExtensionController.ts +++ b/src/mdbExtensionController.ts @@ -229,6 +229,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) => diff --git a/src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts b/src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts index e3260b534..209fa906f 100644 --- a/src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts +++ b/src/test/suite/editors/playgroundSelectedCodeActionProvider.test.ts @@ -177,7 +177,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) { @@ -213,7 +213,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) { @@ -278,7 +278,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) { @@ -347,7 +347,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) { @@ -422,7 +422,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) { @@ -470,6 +470,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', () => { diff --git a/src/types/playgroundType.ts b/src/types/playgroundType.ts index aa0673467..6e7810c5f 100644 --- a/src/types/playgroundType.ts +++ b/src/types/playgroundType.ts @@ -46,6 +46,7 @@ export enum ExportToLanguages { CSHARP = 'csharp', JAVASCRIPT = 'javascript', RUBY = 'ruby', + GO = 'go', } export enum ExportToLanguageMode { From a69654967405c62e55919dab9a40e82b8010b28f Mon Sep 17 00:00:00 2001 From: Gaurab Aryal Date: Fri, 21 Jul 2023 11:22:03 -0400 Subject: [PATCH 2/2] specified export to go is only available when user is connected an in playground --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 3e17c2a15..1e93926fc 100644 --- a/package.json +++ b/package.json @@ -658,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"