Skip to content

Commit

Permalink
Remove the number of calls to toString in finding the label
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashanth committed Apr 28, 2018
1 parent 91633c1 commit 614ccef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/docdb/tree/DocDBDocumentTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import { IAzureNode, IAzureTreeItem, UserCancelledError, DialogResponses } from 'vscode-azureextensionui';
import { RetrievedDocument, DocumentClient } from 'documentdb';
import { DocDBCollectionTreeItem } from './DocDBCollectionTreeItem';
import { documentDefaultFields } from '../../constants';
import { getDocumentTreeItemLabel } from '../../utils/vscodeUtils';

/**
* Represents a Cosmos DB DocumentDB (SQL) document
Expand All @@ -19,26 +19,22 @@ export class DocDBDocumentTreeItem implements IAzureTreeItem {
public readonly commandId: string = 'cosmosDB.openDocument';

public readonly partitionKeyValue: string | undefined;
public label: string;

private _document: RetrievedDocument;
private _collection: DocDBCollectionTreeItem;

constructor(collection: DocDBCollectionTreeItem, document: RetrievedDocument) {
this._collection = collection;
this._document = document;
this.label = getDocumentTreeItemLabel(this._document);
this.partitionKeyValue = this.getPartitionKeyValue();
}

public get id(): string {
return this.document.id;
}

public get label(): string {
const presentFields = documentDefaultFields.filter(element => this._document.hasOwnProperty(element));
const canonicalField = presentFields.find((element) => this._document[element] && this._document[element].toString() && !this._document[element].toString().startsWith("[object"));
return this._document[canonicalField].toString();
}

public get link(): string {
return this.document._self;
}
Expand Down Expand Up @@ -90,6 +86,7 @@ export class DocDBDocumentTreeItem implements IAzureTreeItem {
}
});
});
this.label = getDocumentTreeItemLabel(this._document);
return this.document;
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/mongo/tree/MongoDocumentTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from 'vscode';
import * as path from 'path';
import { Collection, ObjectID, DeleteWriteOpResultObject, UpdateWriteOpResult } from 'mongodb';
import { IAzureTreeItem, IAzureNode, UserCancelledError, DialogResponses } from 'vscode-azureextensionui';
import { documentDefaultFields } from '../../constants';
import { getDocumentTreeItemLabel } from '../../utils/vscodeUtils';

export interface IMongoDocument {
_id: string | ObjectID;
Expand All @@ -23,23 +23,20 @@ export class MongoDocumentTreeItem implements IAzureTreeItem {
public readonly contextValue: string = MongoDocumentTreeItem.contextValue;
public readonly commandId: string = 'cosmosDB.openDocument';
public document: IMongoDocument;
public label;

private _collection: Collection;

constructor(document: IMongoDocument, collection: Collection) {
this.document = document;
this.label = getDocumentTreeItemLabel(this.document);
this._collection = collection;
}

get id(): string {
return this.document._id.toString();
}

get label(): string {
const presentFields = documentDefaultFields.filter(element => this.document.hasOwnProperty(element));
const canonicalField = presentFields.find((element) => this.document[element] && this.document[element].toString() && !this.document[element].toString().startsWith("[object"));
return this.document[canonicalField].toString();
}

public get iconPath(): string | vscode.Uri | { light: string | vscode.Uri; dark: string | vscode.Uri } {
return {
Expand All @@ -63,6 +60,7 @@ export class MongoDocumentTreeItem implements IAzureTreeItem {

public async update(newDocument: IMongoDocument): Promise<IMongoDocument> {
this.document = await MongoDocumentTreeItem.update(this._collection, newDocument);
this.label = getDocumentTreeItemLabel(this.document);
return this.document;
}

Expand Down
22 changes: 22 additions & 0 deletions src/utils/vscodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import * as vscode from 'vscode';
import { IAzureNode, IAzureTreeItem } from 'vscode-azureextensionui';
import { MongoAccountTreeItem } from '../mongo/tree/MongoAccountTreeItem';
import { DocDBAccountTreeItemBase } from '../docdb/tree/DocDBAccountTreeItemBase';
import { IMongoDocument } from '../mongo/tree/MongoDocumentTreeItem';
import { RetrievedDocument } from 'documentdb';
import { documentDefaultFields } from '../constants';

const outputChannel = vscode.window.createOutputChannel("Azure CosmosDB");

Expand Down Expand Up @@ -101,3 +104,22 @@ export function getNodeEditorLabel(node: IAzureNode): string {
function isAccountTreeItem(treeItem: IAzureTreeItem): boolean {
return (treeItem instanceof MongoAccountTreeItem) || (treeItem instanceof DocDBAccountTreeItemBase);
}

export function getDocumentTreeItemLabel(document: IMongoDocument | RetrievedDocument): string {
for (let field of documentDefaultFields) {
if (document.hasOwnProperty(field)) {
if (document[field]) { //ignore if false-y value: null, undefined, "".
if (typeof document[field] === "string") {
return document[field];
} else { // A simple instanceOf check doesn't seem to work. I run into this issue detailed here :
//https://libertyseeds.ca/2015/01/03/Mongo-ObjectID-fails-instanceof-type-test-in-nodeunit/
const result = document[field].toString();
if (!result.startsWith("[object ")) {
return result;
}
}
}
}
}
return document["_id"].toString();
}

0 comments on commit 614ccef

Please sign in to comment.