From f9b8f4a7d37bbcb0e165bf64437f1040eef5057a Mon Sep 17 00:00:00 2001 From: Antoine Beauvais-Lacasse Date: Thu, 21 Sep 2017 18:36:46 -0400 Subject: [PATCH 1/2] datastore: Expose entity identification functions. --- packages/datastore/src/entity.js | 62 +++++++++++++++++++++++++---- packages/datastore/src/index.js | 65 ++++++++++++++++++++++++++++++- packages/datastore/test/entity.js | 59 ++++++++++++++++++++++++++++ packages/datastore/test/index.js | 60 ++++++++++++++++++++++++++++ 4 files changed, 237 insertions(+), 9 deletions(-) diff --git a/packages/datastore/src/entity.js b/packages/datastore/src/entity.js index 728c496de10..f575fd37b9e 100644 --- a/packages/datastore/src/entity.js +++ b/packages/datastore/src/entity.js @@ -59,6 +59,18 @@ function Double(value) { entity.Double = Double; +/** + * Check if something is a Datastore Double object. + * + * @param {*} value + * @return {boolean} + */ +function isDsDouble(value) { + return value instanceof entity.Double; +} + +entity.isDsDouble = isDsDouble; + /** * Build a Datastore Int object. For long integers, a string can be provided. * @@ -74,6 +86,18 @@ function Int(value) { entity.Int = Int; +/** + * Check if something is a Datastore Int object. + * + * @param {*} value + * @return {boolean} + */ +function isDsInt(value) { + return value instanceof entity.Int; +} + +entity.isDsInt = isDsInt; + /** * Build a Datastore Geo Point object. * @@ -90,12 +114,24 @@ entity.Int = Int; * * var geoPoint = new GeoPoint(coordinates); */ -function GeoPoint(coordindates) { - this.value = coordindates; +function GeoPoint(coordinates) { + this.value = coordinates; } entity.GeoPoint = GeoPoint; +/** + * Check if something is a Datastore Geo Point object. + * + * @param {*} value + * @return {boolean} + */ +function isDsGeoPoint(value) { + return value instanceof entity.GeoPoint; +} + +entity.isDsGeoPoint = isDsGeoPoint; + /** * Build a Datastore Key object. * @@ -116,7 +152,7 @@ function Key(options) { if (options.path.length % 2 === 0) { var identifier = options.path.pop(); - if (is.number(identifier) || identifier instanceof entity.Int) { + if (is.number(identifier) || isDsInt(identifier)) { this.id = identifier.value || identifier; } else if (is.string(identifier)) { this.name = identifier; @@ -142,6 +178,18 @@ function Key(options) { entity.Key = Key; +/** + * Check if something is a Datastore Key object. + * + * @param {*} value + * @return {boolean} + */ +function isDsKey(value) { + return value instanceof entity.Key; +} + +entity.isDsKey = isDsKey; + /** * Convert a protobuf Value message to its native value. * @@ -243,17 +291,17 @@ function encodeValue(value) { } } - if (value instanceof entity.Int) { + if (isDsInt(value)) { valueProto.integerValue = value.value; return valueProto; } - if (value instanceof entity.Double) { + if (isDsDouble(value)) { valueProto.doubleValue = value.value; return valueProto; } - if (value instanceof entity.GeoPoint) { + if (isDsGeoPoint(value)) { valueProto.geoPointValue = value.value; return valueProto; } @@ -286,7 +334,7 @@ function encodeValue(value) { return valueProto; } - if (value instanceof entity.Key) { + if (isDsKey(value)) { valueProto.keyValue = entity.keyToKeyProto(value); return valueProto; } diff --git a/packages/datastore/src/index.js b/packages/datastore/src/index.js index cdc3d312171..4e3f922bddc 100644 --- a/packages/datastore/src/index.js +++ b/packages/datastore/src/index.js @@ -343,6 +343,20 @@ Datastore.prototype.double = Datastore.double = function(value) { return new entity.Double(value); }; +/** + * Helper function to check if something is a Datastore Double object. + * + * @param {*} something + * @return {boolean} + * + * @example + * datastore.isDouble(0.42); // false + * datastore.isDouble(datastore.double(0.42)); // true + */ +Datastore.prototype.isDouble = Datastore.isDouble = function(something) { + return entity.isDsDouble(something); +}; + /** * Helper function to get a Datastore Geo Point object. * @@ -359,8 +373,27 @@ Datastore.prototype.double = Datastore.double = function(value) { * * var geoPoint = datastore.geoPoint(coordinates); */ -Datastore.prototype.geoPoint = Datastore.geoPoint = function(coordindates) { - return new entity.GeoPoint(coordindates); +Datastore.prototype.geoPoint = Datastore.geoPoint = function(coordinates) { + return new entity.GeoPoint(coordinates); +}; + +/** + * Helper function to check if something is a Datastore Geo Point object. + * + * @param {*} something + * @return {boolean} + * + * @example + * var coordinates = { + * latitude: 0, + * longitude: 0 + * }; + * + * datastore.isGeoPoint(coordinates); // false + * datastore.isGeoPoint(datastore.geoPoint(coordinates)); // true + */ +Datastore.prototype.isGeoPoint = Datastore.isGeoPoint = function(something) { + return entity.isDsGeoPoint(something); }; /** @@ -387,6 +420,20 @@ Datastore.prototype.int = Datastore.int = function(value) { return new entity.Int(value); }; +/** + * Helper function to check if something is a Datastore Integer object. + * + * @param {*} something + * @return {boolean} + * + * @example + * datastore.isInt(42); // false + * datastore.isInt(datastore.int(42)); // true + */ +Datastore.prototype.isInt = Datastore.isInt = function(something) { + return entity.isDsInt(something); +}; + /** * Access the Key from an Entity object. * @@ -509,6 +556,20 @@ Datastore.prototype.key = function(options) { return new entity.Key(options); }; +/** + * Helper function to check if something is a Datastore Key object. + * + * @param {*} something + * @return {boolean} + * + * @example + * datastore.isKey({path: ['Company', 123]}); // false + * datastore.isKey(datastore.key(['Company', 123])); // true + */ +Datastore.prototype.isKey = Datastore.isKey = function(something) { + return entity.isDsKey(something); +}; + /** * Create a new Transaction object. * diff --git a/packages/datastore/test/entity.js b/packages/datastore/test/entity.js index 048d643e547..02406962b5e 100644 --- a/packages/datastore/test/entity.js +++ b/packages/datastore/test/entity.js @@ -48,6 +48,23 @@ describe('entity', function() { }); }); + describe('isDsDouble', function() { + it('should correctly identify a Double', function() { + var double = new entity.Double(0.42); + assert.strictEqual(entity.isDsDouble(double), true); + }); + + it('should correctly identify a homomorphic non-Double', function() { + var nonDouble = Object.assign({}, new entity.Double(42)); + assert.strictEqual(entity.isDsDouble(nonDouble), false); + }); + + it('should correctly identify a primitive', function() { + var primitiveDouble = 0.42; + assert.strictEqual(entity.isDsDouble(primitiveDouble), false); + }); + }); + describe('Int', function() { it('should store the stringified value', function() { var value = 8; @@ -57,6 +74,23 @@ describe('entity', function() { }); }); + describe('isDsInt', function() { + it('should correctly identify an Int', function() { + var int = new entity.Int(42); + assert.strictEqual(entity.isDsInt(int), true); + }); + + it('should correctly identify homomorphic non-Int', function() { + var nonInt = Object.assign({}, new entity.Int(42)); + assert.strictEqual(entity.isDsInt(nonInt), false); + }); + + it('should correctly identify a primitive', function() { + var primitiveInt = 42; + assert.strictEqual(entity.isDsInt(primitiveInt), false); + }); + }); + describe('GeoPoint', function() { it('should store the value', function() { var value = { @@ -69,6 +103,19 @@ describe('entity', function() { }); }); + describe('isDsGeoPoint', function() { + it('should correctly identify a GeoPoint', function() { + var geoPoint = new entity.GeoPoint({latitude: 24, longitude: 88}); + assert.strictEqual(entity.isDsGeoPoint(geoPoint), true); + }); + + it('should correctly identify a homomorphic non-GeoPoint', function() { + var geoPoint = new entity.GeoPoint({latitude: 24, longitude: 88}); + var nonGeoPoint = Object.assign({}, geoPoint); + assert.strictEqual(entity.isDsGeoPoint(nonGeoPoint), false); + }); + }); + describe('Key', function() { it('should assign the namespace', function() { var namespace = 'NS'; @@ -116,6 +163,18 @@ describe('entity', function() { }); }); + describe('isDsKey', function() { + it('should correctly identify a Key', function() { + var key = new entity.Key({path: ['Kind', 1]}); + assert.strictEqual(entity.isDsKey(key), true); + }); + + it('should correctly identify a homomorphic non-Key', function() { + var notKey = Object.assign({}, new entity.Key({path: ['Kind', 1]})); + assert.strictEqual(entity.isDsKey(notKey), false); + }); + }); + describe('decodeValueProto', function() { it('should decode arrays', function() { var expectedValue = [{}]; diff --git a/packages/datastore/test/index.js b/packages/datastore/test/index.js index 6bcee3b1a71..b7dc9393618 100644 --- a/packages/datastore/test/index.js +++ b/packages/datastore/test/index.js @@ -27,14 +27,26 @@ var fakeEntity = { Int: function(value) { this.value = value; }, + isDsInt: function() { + this.calledWith_ = arguments; + }, Double: function(value) { this.value = value; }, + isDsDouble: function() { + this.calledWith_ = arguments; + }, GeoPoint: function(value) { this.value = value; }, + isDsGeoPoint: function() { + this.calledWith_ = arguments; + }, Key: function() { this.calledWith_ = arguments; + }, + isDsKey: function() { + this.calledWith_ = arguments; } }; @@ -194,6 +206,18 @@ describe('Datastore', function() { }); }); + describe('isDouble', function() { + it('should expose Double identifier', function() { + var something = {}; + Datastore.isDouble(something); + assert.strictEqual(fakeEntity.calledWith_[0], something); + }); + + it('should also be on the prototype', function() { + assert.strictEqual(datastore.isDouble, Datastore.isDouble); + }); + }); + describe('geoPoint', function() { it('should expose GeoPoint builder', function() { var aGeoPoint = { latitude: 24, longitude: 88 }; @@ -206,6 +230,18 @@ describe('Datastore', function() { }); }); + describe('isGeoPoint', function() { + it('should expose GeoPoint identifier', function() { + var something = {}; + Datastore.isGeoPoint(something); + assert.strictEqual(fakeEntity.calledWith_[0], something); + }); + + it('should also be on the prototype', function() { + assert.strictEqual(datastore.isGeoPoint, Datastore.isGeoPoint); + }); + }); + describe('int', function() { it('should expose Int builder', function() { var anInt = 7; @@ -218,6 +254,18 @@ describe('Datastore', function() { }); }); + describe('isInt', function() { + it('should expose Int identifier', function() { + var something = {}; + Datastore.isInt(something); + assert.strictEqual(fakeEntity.calledWith_[0], something); + }); + + it('should also be on the prototype', function() { + assert.strictEqual(datastore.isInt, Datastore.isInt); + }); + }); + describe('KEY', function() { it('should expose the KEY symbol', function() { assert.strictEqual(Datastore.KEY, fakeEntity.KEY_SYMBOL); @@ -318,6 +366,18 @@ describe('Datastore', function() { }); }); + describe('isKey', function() { + it('should expose Key identifier', function() { + var something = {}; + datastore.isKey(something); + assert.strictEqual(fakeEntity.calledWith_[0], something); + }); + + it('should also be on the namespace', function() { + assert.strictEqual(datastore.isKey, Datastore.isKey); + }); + }); + describe('transaction', function() { it('should return a Transaction object', function() { var transaction = datastore.transaction(); From 0c285a3908705bee72def87f9097c90f1be696b4 Mon Sep 17 00:00:00 2001 From: Antoine Beauvais-Lacasse Date: Tue, 10 Oct 2017 17:46:52 -0400 Subject: [PATCH 2/2] datastore: Change entity check argument name to be consistent --- packages/datastore/src/index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/datastore/src/index.js b/packages/datastore/src/index.js index 4e3f922bddc..2d6d2b93bc6 100644 --- a/packages/datastore/src/index.js +++ b/packages/datastore/src/index.js @@ -346,15 +346,15 @@ Datastore.prototype.double = Datastore.double = function(value) { /** * Helper function to check if something is a Datastore Double object. * - * @param {*} something + * @param {*} value * @return {boolean} * * @example * datastore.isDouble(0.42); // false * datastore.isDouble(datastore.double(0.42)); // true */ -Datastore.prototype.isDouble = Datastore.isDouble = function(something) { - return entity.isDsDouble(something); +Datastore.prototype.isDouble = Datastore.isDouble = function(value) { + return entity.isDsDouble(value); }; /** @@ -380,7 +380,7 @@ Datastore.prototype.geoPoint = Datastore.geoPoint = function(coordinates) { /** * Helper function to check if something is a Datastore Geo Point object. * - * @param {*} something + * @param {*} value * @return {boolean} * * @example @@ -392,8 +392,8 @@ Datastore.prototype.geoPoint = Datastore.geoPoint = function(coordinates) { * datastore.isGeoPoint(coordinates); // false * datastore.isGeoPoint(datastore.geoPoint(coordinates)); // true */ -Datastore.prototype.isGeoPoint = Datastore.isGeoPoint = function(something) { - return entity.isDsGeoPoint(something); +Datastore.prototype.isGeoPoint = Datastore.isGeoPoint = function(value) { + return entity.isDsGeoPoint(value); }; /** @@ -423,15 +423,15 @@ Datastore.prototype.int = Datastore.int = function(value) { /** * Helper function to check if something is a Datastore Integer object. * - * @param {*} something + * @param {*} value * @return {boolean} * * @example * datastore.isInt(42); // false * datastore.isInt(datastore.int(42)); // true */ -Datastore.prototype.isInt = Datastore.isInt = function(something) { - return entity.isDsInt(something); +Datastore.prototype.isInt = Datastore.isInt = function(value) { + return entity.isDsInt(value); }; /** @@ -559,15 +559,15 @@ Datastore.prototype.key = function(options) { /** * Helper function to check if something is a Datastore Key object. * - * @param {*} something + * @param {*} value * @return {boolean} * * @example * datastore.isKey({path: ['Company', 123]}); // false * datastore.isKey(datastore.key(['Company', 123])); // true */ -Datastore.prototype.isKey = Datastore.isKey = function(something) { - return entity.isDsKey(something); +Datastore.prototype.isKey = Datastore.isKey = function(value) { + return entity.isDsKey(value); }; /**