Skip to content

Commit

Permalink
Merge pull request #64 from mongodb-js/COMPASS-2790-shell-to-python
Browse files Browse the repository at this point in the history
Compass 2790 shell to python
  • Loading branch information
alenakhineika authored and mcasimir committed Jun 25, 2021
1 parent d9ca13d commit fae90fd
Show file tree
Hide file tree
Showing 21 changed files with 339 additions and 92 deletions.
152 changes: 152 additions & 0 deletions packages/bson-transpilers/codegeneration/python/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,156 @@ module.exports = (superClass) => class ExtendedVisitor extends superClass {

return `Binary(b${bytes}, ${this.binarySubTypes[type]})`;
}

/**
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitBinData(ctx) {
ctx.type = this.Types.BinData;

const argList = ctx.arguments().argumentList();
const args = this.checkArguments(this.Symbols.BinData.args, argList);
const subtype = parseInt(argList.singleExpression()[0].getText(), 10);
const bindata = args[1];

if (!(subtype >= 0 && subtype <= 5 || subtype === 128)) {
throw new SemanticGenericError({message: 'BinData subtype must be a Number between 0-5 or 128'});
}

if (bindata.match(/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/)) {
throw new SemanticGenericError({message: 'invalid base64'});
}

return `Binary(b${bindata}, ${this.binarySubTypes[subtype]})`;
}

/**
* TODO: Maybe move this to javascript/Visitor and use template?
*
* child nodes: arguments
* grandchild nodes: argumentList?
* great-grandchild nodes: singleExpression+
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitLong(ctx) {
ctx.type = this.Types.Long;

let longstr;

try {
longstr = this.executeJavascript(ctx.getText()).toString();
} catch (error) {
throw new SemanticGenericError({message: error.message});
}

return `Int64(${longstr})`;
}

/**
* TODO: Could move this to javascript/Visitor and use template.
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitDecimal128(ctx, str) {
return `Decimal128(${singleQuoteStringify(str)})`;
}
emitNumberDecimal(ctx, str) {
return `Decimal128(${singleQuoteStringify(str)})`;
}

/* ************** Object methods **************** */

/**
* LongfromBits method
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitLongfromBits(ctx) {
return this.emitLong(ctx);
}

/**
* Decimal128toJSON method
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitDecimal128toJSON(ctx) {
ctx.type = this.Types._object;

return `json_util.dumps(${this.visit(ctx.singleExpression().singleExpression())})`;
}

/**
* LongtoString method
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitLongtoString(ctx) {
ctx.type = this.Types._string;

const long = ctx.singleExpression().singleExpression();
let longstr;

try {
longstr = this.executeJavascript(long.getText()).toString();
} catch (error) {
throw new SemanticGenericError({message: error.message});
}

return `str(Int64(${longstr}))`;
}

/**
* DBReftoJSON method
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitDBReftoJSON(ctx) {
ctx.type = this.Types._object;

const argsList = ctx.singleExpression().singleExpression().arguments();
const args = argsList.argumentList().singleExpression();
const ns = this.visit(args[0]);
const oid = this.visit(args[1]);
let db = '""';

if (args.length === 3) {
db = this.visit(args[2]);

return `json_util.dumps(DBRef(${ns}, ${oid}, ${db}))`;
}

return `json_util.dumps(DBRef(${ns}, ${oid}))`;
}

/**
* CodetoJSON method
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
*/
emitCodetoJSON(ctx) {
ctx.type = this.Types._object;

const argsList = ctx.singleExpression().singleExpression().arguments();
const args = argsList.argumentList().singleExpression();
const code = singleQuoteStringify(args[0].getText());
let scope = 'undefined';

if (args.length === 2) {
scope = this.visit(args[1]);

return `json_util.dumps(Code(${code}, ${scope}))`;
}

return `json_util.dumps(Code(${code}))`;
}
};

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltojava.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions packages/bson-transpilers/symbols/csharp/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Templates:
ArrayTypeTemplate: &ArrayTypeTemplate !!js/function >
(literal) => {
if (literal === '') {
return 'new BsonArray()'
return 'new BsonArray()'
}
return `new BsonArray {${literal}}`;
}
Expand Down Expand Up @@ -167,13 +167,20 @@ Templates:
return `${lhs}.ToInt32`;
}
Int32ValueOfArgsTemplate: &Int32ValueOfArgsTemplate null
Int32ToStringTemplate: &Int32ToStringTemplate null
Int32ToStringArgsTemplate: &Int32ToStringArgsTemplate null
LongEqualsTemplate: &LongEqualsTemplate null
LongEqualsArgsTemplate: &LongEqualsArgsTemplate null
LongToIntTemplate: &LongToIntTemplate !!js/function >
(lhs) => {
return `${lhs}.ToInt32`;
}
LongToIntArgsTemplate: &LongToIntArgsTemplate null
LongValueOfTemplate: &LongValueOfTemplate !!js/function >
(lhs) => {
return `${lhs}.ToInt32`;
}
LongValueOfArgsTemplate: &LongValueOfArgsTemplate null
LongToNumberTemplate: &LongToNumberTemplate !!js/function >
(lhs) => {
return `${lhs}.ToDouble`;
Expand Down Expand Up @@ -509,7 +516,7 @@ Templates:
return `("${arg}")`;
}
Int32SymbolTemplate: &Int32SymbolTemplate null # Has emit method
Int32SymbolArgsTemplate: &Int32SymbolArgsTemplate null # Has emit method
Int32SymbolArgsTemplate: &Int32SymbolArgsTemplate null # Has emit method
LongSymbolTemplate: &LongSymbolTemplate !!js/function >
() => {
return 'BsonInt64';
Expand Down Expand Up @@ -540,26 +547,26 @@ Templates:
LongSymbolOneArgsTemplate: &LongSymbolOneArgsTemplate null
LongSymbolNegOneTemplate: &LongSymbolNegOneTemplate !!js/function >
(lhs) => {
return `new ${lhs}.ToInt64(-1)`;
return `new ${lhs}.ToInt64(-1)`;
}
LongSymbolNegOneArgsTemplate: &LongSymbolNegOneArgsTemplate null
LongSymbolFromBitsTemplate: &LongSymbolFromBitsTemplate !!js/function >
(lhs) => {
return '';
return '';
}
LongSymbolFromBitsArgsTemplate: &LongSymbolFromBitsArgsTemplate !!js/function >
(lhs) => {
return `new ${lhs}.ToInt64`;
return `new ${lhs}.ToInt64`;
}
LongSymbolFromIntTemplate: &LongSymbolFromIntTemplate !!js/function >
LongSymbolFromIntTemplate: &LongSymbolFromIntTemplate !!js/function >
(lhs) => {
return `new ${lhs}.ToInt64`;
return `new ${lhs}.ToInt64`;
}
LongSymbolFromIntArgsTemplate: &LongSymbolFromIntArgsTemplate null
LongSymbolFromNumberTemplate: &LongSymbolFromNumberTemplate null
LongSymbolFromNumberArgsTemplate: &LongSymbolFromNumberArgsTemplate !!js/function >
(lhs, arg) => {
return `new ${lhs}.ToInt64(${arg})`;
return `new ${lhs}.ToInt64(${arg})`;
}
LongSymbolFromStringTemplate: &LongSymbolFromStringTemplate !!js/function >
(lhs) => {
Expand Down Expand Up @@ -650,7 +657,7 @@ Templates:
}
DateSymbolTemplate: &DateSymbolTemplate null # Has emit method
DateSymbolArgsTemplate: &DateSymbolArgsTemplate null # Has emit method
DateSymbolNowTemplate: &DateSymbolNowTemplate null # Has emit method
DateSymbolNowTemplate: &DateSymbolNowTemplate null # Has emit method
DateSymbolNowArgsTemplate: &DateSymbolNowArgsTemplate null # Has emit method
RegExpSymbolTemplate: &RegExpSymbolTemplate null
RegExpSymbolArgsTemplate: &RegExpSymbolArgsTemplate null
7 changes: 7 additions & 0 deletions packages/bson-transpilers/symbols/java/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,20 @@ Templates:
return `${lhs}.intValue`;
}
Int32ValueOfArgsTemplate: &Int32ValueOfArgsTemplate null
Int32ToStringTemplate: &Int32ToStringTemplate null
Int32ToStringArgsTemplate: &Int32ToStringArgsTemplate null
LongEqualsTemplate: &LongEqualsTemplate null
LongEqualsArgsTemplate: &LongEqualsArgsTemplate null
LongToIntTemplate: &LongToIntTemplate !!js/function >
(lhs) => {
return `${lhs}.intValue`;
}
LongToIntArgsTemplate: &LongToIntArgsTemplate null
LongValueOfTemplate: &LongValueOfTemplate !!js/function >
(lhs) => {
return `${lhs}.intValue`;
}
LongValueOfArgsTemplate: &LongValueOfArgsTemplate null
LongToNumberTemplate: &LongToNumberTemplate !!js/function >
(lhs) => {
return `${lhs}.floatValue`;
Expand Down
7 changes: 7 additions & 0 deletions packages/bson-transpilers/symbols/javascript/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,20 @@ Templates:
DoubleValueOfArgsTemplate: &DoubleValueOfArgsTemplate null
Int32ValueOfTemplate: &Int32ValueOfTemplate null
Int32ValueOfArgsTemplate: &Int32ValueOfArgsTemplate null
Int32ToStringTemplate: &Int32ToStringTemplate null
Int32ToStringArgsTemplate: &Int32ToStringArgsTemplate null
LongEqualsTemplate: &LongEqualsTemplate null
LongEqualsArgsTemplate: &LongEqualsArgsTemplate null
LongToIntTemplate: &LongToIntTemplate !!js/function >
(lhs) => {
return `${lhs}.toInt`;
}
LongToIntArgsTemplate: &LongToIntArgsTemplate null
LongValueOfTemplate: &LongValueOfTemplate !!js/function >
(lhs) => {
return `${lhs}.toInt`;
}
LongValueOfArgsTemplate: &LongValueOfArgsTemplate null
LongToNumberTemplate: &LongToNumberTemplate null
LongToNumberArgsTemplate: &LongToNumberArgsTemplate null
LongAddTemplate: &LongAddTemplate null
Expand Down
Loading

0 comments on commit fae90fd

Please sign in to comment.