Skip to content

Commit

Permalink
datastore: Expose entity identification functions. (#2645)
Browse files Browse the repository at this point in the history
  • Loading branch information
beaulac authored and stephenplusplus committed Oct 10, 2017
1 parent d403aa3 commit 9ecca41
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 9 deletions.
62 changes: 55 additions & 7 deletions packages/datastore/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -286,7 +334,7 @@ function encodeValue(value) {
return valueProto;
}

if (value instanceof entity.Key) {
if (isDsKey(value)) {
valueProto.keyValue = entity.keyToKeyProto(value);
return valueProto;
}
Expand Down
65 changes: 63 additions & 2 deletions packages/datastore/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {*} value
* @return {boolean}
*
* @example
* datastore.isDouble(0.42); // false
* datastore.isDouble(datastore.double(0.42)); // true
*/
Datastore.prototype.isDouble = Datastore.isDouble = function(value) {
return entity.isDsDouble(value);
};

/**
* Helper function to get a Datastore Geo Point object.
*
Expand All @@ -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 {*} value
* @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(value) {
return entity.isDsGeoPoint(value);
};

/**
Expand All @@ -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 {*} value
* @return {boolean}
*
* @example
* datastore.isInt(42); // false
* datastore.isInt(datastore.int(42)); // true
*/
Datastore.prototype.isInt = Datastore.isInt = function(value) {
return entity.isDsInt(value);
};

/**
* Access the Key from an Entity object.
*
Expand Down Expand Up @@ -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 {*} value
* @return {boolean}
*
* @example
* datastore.isKey({path: ['Company', 123]}); // false
* datastore.isKey(datastore.key(['Company', 123])); // true
*/
Datastore.prototype.isKey = Datastore.isKey = function(value) {
return entity.isDsKey(value);
};

/**
* Create a new Transaction object.
*
Expand Down
59 changes: 59 additions & 0 deletions packages/datastore/test/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {
Expand All @@ -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';
Expand Down Expand Up @@ -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 = [{}];
Expand Down
Loading

0 comments on commit 9ecca41

Please sign in to comment.