Skip to content

Commit

Permalink
Remove support for Binary/BinData
Browse files Browse the repository at this point in the history
  • Loading branch information
aherlihy committed Jun 15, 2018
1 parent 680891d commit 77a7497
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 286 deletions.
59 changes: 4 additions & 55 deletions packages/bson-transpilers/codegeneration/javascript/Visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {
BsonCompilersInternalError,
BsonCompilersUnimplementedError
} = require('../../helper/error');
const { singleQuoteStringify } = require('../../helper/format');

/**
* This is a Visitor that visits the tree generated by the ECMAScript.g4 grammar.
Expand Down Expand Up @@ -397,7 +396,7 @@ class Visitor extends ECMAScriptVisitor {
const sandbox = {
RegExp: RegExp,
BSONRegExp: bson.BSONRegExp,
Binary: bson.Binary,
// Binary: bson.Binary,
DBRef: bson.DBRef,
Decimal128: bson.Decimal128,
Double: bson.Double,
Expand Down Expand Up @@ -869,60 +868,10 @@ class Visitor extends ECMAScriptVisitor {
* Binary needs preprocessing because it needs to be executed. Manually check
* argument length because 'Buffer' not supported.
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
* TODO: figure out if it ever makes sense to support Binary.
*/
processBinary(ctx) {
ctx.type = this.Types.Binary;
const symbolType = this.Symbols.Binary;
const binaryTypes = {
0: symbolType.attr.SUBTYPE_DEFAULT.template,
1: symbolType.attr.SUBTYPE_FUNCTION.template,
2: symbolType.attr.SUBTYPE_BYTE_ARRAY.template,
3: symbolType.attr.SUBTYPE_UUID_OLD.template,
4: symbolType.attr.SUBTYPE_UUID.template,
5: symbolType.attr.SUBTYPE_MD5.template,
128: symbolType.attr.SUBTYPE_USER_DEFINED.template
};
let type;
let binobj;
const argList = ctx.arguments().argumentList();

if (
!argList ||
!(
argList.singleExpression().length === 1 ||
argList.singleExpression().length === 2
)
) {
throw new BsonCompilersArgumentError(
'Argument count mismatch: Binary requires one or two arguments'
);
}

try {
binobj = this.executeJavascript(ctx.getText());
type = binobj.sub_type;
} catch (error) {
// TODO: this isn't quite right because it catches all type errors.
if (error.name === 'TypeError' || error.code === 'ERR_INVALID_ARG_TYPE') {
throw new BsonCompilersArgumentError(error.message);
}

throw new BsonCompilersRuntimeError(error.message);
}
const bytes = binobj.toString();
const args = argList.singleExpression();
const templatedType = binaryTypes[type] !== null ? binaryTypes[type]() : type;
const typeStr = args.length === 1 ? null : templatedType;

if ('emitBinary' in this) {
return this.emitBinary(bytes, typeStr);
}

const lhs = symbolType.template ? symbolType.template() : 'Binary';
const rhs = symbolType.argsTemplate ? symbolType.argsTemplate(lhs, bytes, typeStr) : `(${singleQuoteStringify(bytes)}${typeStr === null ? '' : ', ' + typeStr})`;
return `${this.new}${lhs}${rhs}`;
processBinary() {
throw new BsonCompilersUnimplementedError('Binary type not supported');
}
}

Expand Down
40 changes: 4 additions & 36 deletions packages/bson-transpilers/codegeneration/shell/Visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const Context = require('context-eval');
const {
BsonCompilersReferenceError,
BsonCompilersRuntimeError,
BsonCompilersRangeError
BsonCompilersUnimplementedError
} = require('../../helper/error');
const { removeQuotes } = require('../../helper/format');

/**
* This is a Visitor superclass where helper methods used by all language
Expand Down Expand Up @@ -100,41 +99,10 @@ class Visitor extends JavascriptVisitor {
* BinData needs extra processing because we need to check that the arg is
* valid base64.
*
* @param {FuncCallExpressionContext} ctx
* @return {String}
* TODO: figure out if it ever makes sense to support Binary.
*/
processBinData(ctx) {
ctx.type = this.Types.BinData;
const symbolType = this.Symbols.BinData;

const binaryTypes = {
0: this.Types.SUBTYPE_DEFAULT.template,
1: this.Types.SUBTYPE_FUNCTION.template,
2: this.Types.SUBTYPE_BYTE_ARRAY.template,
3: this.Types.SUBTYPE_UUID_OLD.template,
4: this.Types.SUBTYPE_UUID.template,
5: this.Types.SUBTYPE_MD5.template,
128: this.Types.SUBTYPE_USER_DEFINED.template
};
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 BsonCompilersRangeError('BinData subtype must be a Number between 0-5 or 128');
}
if (!removeQuotes(bindata).match(/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/)) {
throw new BsonCompilersRuntimeError('Invalid base64 passed to BinData');
}
const typeStr = binaryTypes[subtype] !== null ? binaryTypes[subtype]() : subtype;

if ('emitBinary' in this) {
return this.emitBinData(bindata, typeStr);
}
const lhs = symbolType.template ? symbolType.template() : 'Binary';
const rhs = symbolType.argsTemplate ? symbolType.argsTemplate(lhs, bindata, typeStr) : `(${bindata}, ${typeStr})`;
return `${this.new}${lhs}${rhs}`;
processBinData() {
throw new BsonCompilersUnimplementedError('BinData type not supported');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,6 @@
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
}
],
"Binary": [
{
"description": "Binary without arguments",
"query": "new Binary()",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
},
{
"description": "Binary with extra argument",
"query": "new Binary(Buffer.from(\"1234\"), Binary.SUBTYPE_UUID, 5)",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
},
{
"description": "Binary with wrong buffer",
"query": "new Binary(Buffer.from(1), Binary.SUBTYPE_UUID)",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
},
{
"description": "Binary with empty Buffer.from()",
"query": "new Binary(Buffer.from(), Binary.SUBTYPE_UUID)",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
},
{
"description": "Binary .length() with extra argument",
"query": "Binary(Buffer.from('0404')).length(10)",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
},
{
"description": "Binary .value() with extra argument",
"query": "Binary(Buffer.from('0404')).value(12)",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
},
{
"description": "Binary .toString() with extra argument",
"query": "Binary(Buffer.from('0404')).toString(10)",
"errorCode": "E_BSONCOMPILERS_ARGUMENT"
}
],
"DBRef": [
{
"description": "DBRef without arguments",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@
"errorCode": "E_BSONCOMPILERS_ATTRIBUTE"
}
],
"Binary": [
{
"description": "Binary with invalidMethod",
"query": "Binary(Buffer.from('a string')).invalidMethod()",
"errorCode": "E_BSONCOMPILERS_ATTRIBUTE"
},
{
"description": "Binary with invalidProperty",
"query": "Binary(Buffer.from('a string')).invalidProperty",
"errorCode": "E_BSONCOMPILERS_ATTRIBUTE"
},
{
"description": "Wrong binary subtype",
"query": "Binary.WRONG_SUBTYPE",
"errorCode": "E_BSONCOMPILERS_ATTRIBUTE"
}
],
"DBRef": [
{
"description": "DBRef with invalidMethod",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
"errorCode": "E_BSONCOMPILERS_RUNTIME"
}
],
"Binary": [
{
"description": "Invalid Binary",
"query": "Binary(x)",
"errorCode": "E_BSONCOMPILERS_RUNTIME"
}
],
"Date": [
{
"description": "Invalid Date",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
"errorCode": "E_BSONCOMPILERS_TYPE"
}
],
"Binary": [
{
"description": "Binary with callable sub_type",
"query": "Binary(Buffer.from('0001')).sub_type()",
"errorCode": "E_BSONCOMPILERS_TYPE"
}
],
"DBRef": [
{
"description": "DBRef with callable db",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"tests": {
"Binary": [
{
"description": "Binary",
"query": "Binary(1)",
"errorCode": "E_BSONCOMPILERS_UNIMPLEMENTED",
"message": "Binary"
}
],
"this": [
{
"description": "this",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
"tests": {
"BinData": [
{
"description": "BinData with unsupported subtype",
"description": "BinData",
"query": "new BinData(400, '1234')",
"errorCode": "E_BSONCOMPILERS_RANGE"
},
{
"description": "BinData with invalid hex",
"query": "new BinData(1, 'a string')",
"errorCode": "E_BSONCOMPILERS_RUNTIME"
"errorCode": "E_BSONCOMPILERS_UNIMPLEMENTED"
}
],
"Identifier": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,48 +68,6 @@
"shell": "ObjectId('5a7382114ec1f67ae445f778')"
}
],
"Binary": [
{
"description": "Binary with ascii buffer arg",
"javascript": "Binary(Buffer.from('1234'))",
"python": "Binary(b'1234')",
"java": "new Binary(\"1234\".getBytes(\"UTF-8\"))",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"1234\"))",
"shell": "BinData(0, '1234')"
},
{
"description": "new Binary with (ascii buffer, UUID subtype enum) args",
"javascript": "new Binary(Buffer.from(\"1234\"), Binary.SUBTYPE_UUID)",
"python": "Binary(b'1234', binary.UUID_SUBTYPE)",
"java": "new Binary(BsonBinarySubType.UUID_STANDARD, \"1234\".getBytes(\"UTF-8\"))",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"1234\"), BsonBinarySubType.UuidStandard)",
"shell": "new BinData(4, '1234')"
},
{
"description": "Binary with (ascii buffer, number) args",
"javascript": "Binary(Buffer.from('1234'), '1')",
"python": "Binary(b'1234', binary.FUNCTION_SUBTYPE)",
"java": "new Binary(BsonBinarySubType.FUNCTION, \"1234\".getBytes(\"UTF-8\"))",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"1234\"), BsonBinarySubType.Function)",
"shell": "BinData(1, '1234')"
},
{
"description": "Binary with string args",
"javascript": "Binary('1234')",
"python": "Binary(b'1234')",
"java": "new Binary(\"1234\".getBytes(\"UTF-8\"))",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"1234\"))",
"shell": "BinData(0, '1234')"
},
{
"description": "Binary with (string, string) args",
"javascript": "Binary('1234', '1')",
"python": "Binary(b'1234', binary.FUNCTION_SUBTYPE)",
"java": "new Binary(BsonBinarySubType.FUNCTION, \"1234\".getBytes(\"UTF-8\"))",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"1234\"), BsonBinarySubType.Function)",
"shell": "BinData(1, '1234')"
}
],
"DBRef": [
{
"description": "new DBRef with (string, ObjectID) args",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,6 @@
"shell": "ObjectId().equals(ObjectId())"
}
],
"Binary": [
{
"description": "length",
"javascript": "Binary(Buffer.from('0404')).length()",
"python": "len(Binary(b'0404'))",
"java": "new Binary(\"0404\".getBytes(\"UTF-8\")).length()",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"0404\")).Bytes.Length",
"shell": "BinData(0, '0404').length()"
},
{
"description": "subtype",
"javascript": "Binary(Buffer.from('0001')).sub_type",
"python": "Binary(b'0001').subtype",
"java": "new Binary(\"0001\".getBytes(\"UTF-8\")).getType()",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"0001\")).SubType",
"shell": "BinData(0, '0001').subtype()"
},
{
"description": "value",
"javascript": "Binary(Buffer.from('0404')).value()",
"python": "str(Binary(b'0404'))",
"java": "new Binary(\"0404\".getBytes(\"UTF-8\")).getData()",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"0404\")).ToString()",
"shell": "BinData(0, '0404').hex()"
},
{
"description": "toString",
"javascript": "Binary(Buffer.from('0404')).toString()",
"python": "str(Binary(b'0404'))",
"java": "new Binary(\"0404\".getBytes(\"UTF-8\")).toString()",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"0404\")).ToString()",
"shell": "BinData(0, '0404').toString()"
}
],
"DBRef": [
{
"description": "toString",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,6 @@
"csharp": "BsonJavaScript",
"shell": "Code"
},
{
"description": "Binary without args",
"javascript": "Binary",
"python": "Binary",
"java": "Binary",
"csharp": "BsonBinaryData",
"shell": "BinData"
},
{
"description": "DBRef without args",
"javascript": "DBRef",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@
"shell": "ObjectId('5a7382114ec1f67ae445f778')"
}
],
"BinData": [
{
"description": "new Binary with (ascii buffer, UUID subtype enum) args",
"javascript": "new Binary('1234', '4')",
"python": "Binary(b'1234', binary.UUID_SUBTYPE)",
"java": "new Binary(BsonBinarySubType.UUID_STANDARD, \"1234\".getBytes(\"UTF-8\"))",
"csharp": "new BsonBinaryData(System.Text.Encoding.ASCII.GetBytes(\"1234\"), BsonBinarySubType.UuidStandard)",
"shell": "new BinData(4, '1234')"
}
],
"DBRef": [
{
"description": "new DBRef with (string, ObjectID) args",
Expand Down
Loading

0 comments on commit 77a7497

Please sign in to comment.