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: added Export to Go support VSCODE-411 #567

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
4 changes: 4 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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also specify that this command is available only when a user is connected and for playground only and not other files opened in vscode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

"title": "MongoDB: Export To Go"
},
{
"command": "mdb.addConnection",
"title": "Add MongoDB Connection",
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 @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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', () => {
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