Skip to content

Commit f9b8f4a

Browse files
committed
datastore: Expose entity identification functions.
1 parent c48d27f commit f9b8f4a

4 files changed

Lines changed: 237 additions & 9 deletions

File tree

packages/datastore/src/entity.js

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ function Double(value) {
5959

6060
entity.Double = Double;
6161

62+
/**
63+
* Check if something is a Datastore Double object.
64+
*
65+
* @param {*} value
66+
* @return {boolean}
67+
*/
68+
function isDsDouble(value) {
69+
return value instanceof entity.Double;
70+
}
71+
72+
entity.isDsDouble = isDsDouble;
73+
6274
/**
6375
* Build a Datastore Int object. For long integers, a string can be provided.
6476
*
@@ -74,6 +86,18 @@ function Int(value) {
7486

7587
entity.Int = Int;
7688

89+
/**
90+
* Check if something is a Datastore Int object.
91+
*
92+
* @param {*} value
93+
* @return {boolean}
94+
*/
95+
function isDsInt(value) {
96+
return value instanceof entity.Int;
97+
}
98+
99+
entity.isDsInt = isDsInt;
100+
77101
/**
78102
* Build a Datastore Geo Point object.
79103
*
@@ -90,12 +114,24 @@ entity.Int = Int;
90114
*
91115
* var geoPoint = new GeoPoint(coordinates);
92116
*/
93-
function GeoPoint(coordindates) {
94-
this.value = coordindates;
117+
function GeoPoint(coordinates) {
118+
this.value = coordinates;
95119
}
96120

97121
entity.GeoPoint = GeoPoint;
98122

123+
/**
124+
* Check if something is a Datastore Geo Point object.
125+
*
126+
* @param {*} value
127+
* @return {boolean}
128+
*/
129+
function isDsGeoPoint(value) {
130+
return value instanceof entity.GeoPoint;
131+
}
132+
133+
entity.isDsGeoPoint = isDsGeoPoint;
134+
99135
/**
100136
* Build a Datastore Key object.
101137
*
@@ -116,7 +152,7 @@ function Key(options) {
116152
if (options.path.length % 2 === 0) {
117153
var identifier = options.path.pop();
118154

119-
if (is.number(identifier) || identifier instanceof entity.Int) {
155+
if (is.number(identifier) || isDsInt(identifier)) {
120156
this.id = identifier.value || identifier;
121157
} else if (is.string(identifier)) {
122158
this.name = identifier;
@@ -142,6 +178,18 @@ function Key(options) {
142178

143179
entity.Key = Key;
144180

181+
/**
182+
* Check if something is a Datastore Key object.
183+
*
184+
* @param {*} value
185+
* @return {boolean}
186+
*/
187+
function isDsKey(value) {
188+
return value instanceof entity.Key;
189+
}
190+
191+
entity.isDsKey = isDsKey;
192+
145193
/**
146194
* Convert a protobuf Value message to its native value.
147195
*
@@ -243,17 +291,17 @@ function encodeValue(value) {
243291
}
244292
}
245293

246-
if (value instanceof entity.Int) {
294+
if (isDsInt(value)) {
247295
valueProto.integerValue = value.value;
248296
return valueProto;
249297
}
250298

251-
if (value instanceof entity.Double) {
299+
if (isDsDouble(value)) {
252300
valueProto.doubleValue = value.value;
253301
return valueProto;
254302
}
255303

256-
if (value instanceof entity.GeoPoint) {
304+
if (isDsGeoPoint(value)) {
257305
valueProto.geoPointValue = value.value;
258306
return valueProto;
259307
}
@@ -286,7 +334,7 @@ function encodeValue(value) {
286334
return valueProto;
287335
}
288336

289-
if (value instanceof entity.Key) {
337+
if (isDsKey(value)) {
290338
valueProto.keyValue = entity.keyToKeyProto(value);
291339
return valueProto;
292340
}

packages/datastore/src/index.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,20 @@ Datastore.prototype.double = Datastore.double = function(value) {
343343
return new entity.Double(value);
344344
};
345345

346+
/**
347+
* Helper function to check if something is a Datastore Double object.
348+
*
349+
* @param {*} something
350+
* @return {boolean}
351+
*
352+
* @example
353+
* datastore.isDouble(0.42); // false
354+
* datastore.isDouble(datastore.double(0.42)); // true
355+
*/
356+
Datastore.prototype.isDouble = Datastore.isDouble = function(something) {
357+
return entity.isDsDouble(something);
358+
};
359+
346360
/**
347361
* Helper function to get a Datastore Geo Point object.
348362
*
@@ -359,8 +373,27 @@ Datastore.prototype.double = Datastore.double = function(value) {
359373
*
360374
* var geoPoint = datastore.geoPoint(coordinates);
361375
*/
362-
Datastore.prototype.geoPoint = Datastore.geoPoint = function(coordindates) {
363-
return new entity.GeoPoint(coordindates);
376+
Datastore.prototype.geoPoint = Datastore.geoPoint = function(coordinates) {
377+
return new entity.GeoPoint(coordinates);
378+
};
379+
380+
/**
381+
* Helper function to check if something is a Datastore Geo Point object.
382+
*
383+
* @param {*} something
384+
* @return {boolean}
385+
*
386+
* @example
387+
* var coordinates = {
388+
* latitude: 0,
389+
* longitude: 0
390+
* };
391+
*
392+
* datastore.isGeoPoint(coordinates); // false
393+
* datastore.isGeoPoint(datastore.geoPoint(coordinates)); // true
394+
*/
395+
Datastore.prototype.isGeoPoint = Datastore.isGeoPoint = function(something) {
396+
return entity.isDsGeoPoint(something);
364397
};
365398

366399
/**
@@ -387,6 +420,20 @@ Datastore.prototype.int = Datastore.int = function(value) {
387420
return new entity.Int(value);
388421
};
389422

423+
/**
424+
* Helper function to check if something is a Datastore Integer object.
425+
*
426+
* @param {*} something
427+
* @return {boolean}
428+
*
429+
* @example
430+
* datastore.isInt(42); // false
431+
* datastore.isInt(datastore.int(42)); // true
432+
*/
433+
Datastore.prototype.isInt = Datastore.isInt = function(something) {
434+
return entity.isDsInt(something);
435+
};
436+
390437
/**
391438
* Access the Key from an Entity object.
392439
*
@@ -509,6 +556,20 @@ Datastore.prototype.key = function(options) {
509556
return new entity.Key(options);
510557
};
511558

559+
/**
560+
* Helper function to check if something is a Datastore Key object.
561+
*
562+
* @param {*} something
563+
* @return {boolean}
564+
*
565+
* @example
566+
* datastore.isKey({path: ['Company', 123]}); // false
567+
* datastore.isKey(datastore.key(['Company', 123])); // true
568+
*/
569+
Datastore.prototype.isKey = Datastore.isKey = function(something) {
570+
return entity.isDsKey(something);
571+
};
572+
512573
/**
513574
* Create a new Transaction object.
514575
*

packages/datastore/test/entity.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ describe('entity', function() {
4848
});
4949
});
5050

51+
describe('isDsDouble', function() {
52+
it('should correctly identify a Double', function() {
53+
var double = new entity.Double(0.42);
54+
assert.strictEqual(entity.isDsDouble(double), true);
55+
});
56+
57+
it('should correctly identify a homomorphic non-Double', function() {
58+
var nonDouble = Object.assign({}, new entity.Double(42));
59+
assert.strictEqual(entity.isDsDouble(nonDouble), false);
60+
});
61+
62+
it('should correctly identify a primitive', function() {
63+
var primitiveDouble = 0.42;
64+
assert.strictEqual(entity.isDsDouble(primitiveDouble), false);
65+
});
66+
});
67+
5168
describe('Int', function() {
5269
it('should store the stringified value', function() {
5370
var value = 8;
@@ -57,6 +74,23 @@ describe('entity', function() {
5774
});
5875
});
5976

77+
describe('isDsInt', function() {
78+
it('should correctly identify an Int', function() {
79+
var int = new entity.Int(42);
80+
assert.strictEqual(entity.isDsInt(int), true);
81+
});
82+
83+
it('should correctly identify homomorphic non-Int', function() {
84+
var nonInt = Object.assign({}, new entity.Int(42));
85+
assert.strictEqual(entity.isDsInt(nonInt), false);
86+
});
87+
88+
it('should correctly identify a primitive', function() {
89+
var primitiveInt = 42;
90+
assert.strictEqual(entity.isDsInt(primitiveInt), false);
91+
});
92+
});
93+
6094
describe('GeoPoint', function() {
6195
it('should store the value', function() {
6296
var value = {
@@ -69,6 +103,19 @@ describe('entity', function() {
69103
});
70104
});
71105

106+
describe('isDsGeoPoint', function() {
107+
it('should correctly identify a GeoPoint', function() {
108+
var geoPoint = new entity.GeoPoint({latitude: 24, longitude: 88});
109+
assert.strictEqual(entity.isDsGeoPoint(geoPoint), true);
110+
});
111+
112+
it('should correctly identify a homomorphic non-GeoPoint', function() {
113+
var geoPoint = new entity.GeoPoint({latitude: 24, longitude: 88});
114+
var nonGeoPoint = Object.assign({}, geoPoint);
115+
assert.strictEqual(entity.isDsGeoPoint(nonGeoPoint), false);
116+
});
117+
});
118+
72119
describe('Key', function() {
73120
it('should assign the namespace', function() {
74121
var namespace = 'NS';
@@ -116,6 +163,18 @@ describe('entity', function() {
116163
});
117164
});
118165

166+
describe('isDsKey', function() {
167+
it('should correctly identify a Key', function() {
168+
var key = new entity.Key({path: ['Kind', 1]});
169+
assert.strictEqual(entity.isDsKey(key), true);
170+
});
171+
172+
it('should correctly identify a homomorphic non-Key', function() {
173+
var notKey = Object.assign({}, new entity.Key({path: ['Kind', 1]}));
174+
assert.strictEqual(entity.isDsKey(notKey), false);
175+
});
176+
});
177+
119178
describe('decodeValueProto', function() {
120179
it('should decode arrays', function() {
121180
var expectedValue = [{}];

0 commit comments

Comments
 (0)