From fed292eace0bc3abfb3c15a77e240b23ca1c6daf Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Mon, 21 Sep 2015 15:32:32 -0400 Subject: [PATCH] datastore: assign appropriate ID or name attribute --- lib/datastore/entity.js | 9 +++++---- test/datastore/entity.js | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/datastore/entity.js b/lib/datastore/entity.js index a3538e4a16a..0ce93095404 100644 --- a/lib/datastore/entity.js +++ b/lib/datastore/entity.js @@ -21,6 +21,8 @@ 'use strict'; +var is = require('is'); + /** @const {object} Map for query operation -> operation protocol value. */ var OP_TO_OPERATOR = { '=': 'EQUAL', @@ -223,11 +225,10 @@ function keyToKeyProto(key) { var p = { kind: keyPath[i] }; var val = keyPath[i + 1]; if (val) { - // if not numeric, set key name. - if (isNaN(val)) { - p.name = val; - } else { + if (is.number(val)) { p.id = val; + } else { + p.name = val; } } else if (i < keyPath.length - 2) { // i is second last path item throw new Error('Invalid key. Ancestor keys require an id or name.'); diff --git a/test/datastore/entity.js b/test/datastore/entity.js index 151ad57f9e8..edb2e3d63a7 100644 --- a/test/datastore/entity.js +++ b/test/datastore/entity.js @@ -192,12 +192,20 @@ describe('keyToKeyProto', function() { it('should handle incomplete keys with & without namespaces', function() { var key = new entity.Key({ path: [ 'Kind1' ] }); var keyWithNS = new entity.Key({ - namespace: 'Namespace', - path: [ 'Kind1' ] - }); + namespace: 'Namespace', + path: [ 'Kind1' ] + }); + var keyWithNumericID = new entity.Key({ + path: [ 'Kind1', 234 ] + }); + var keyWithStringID = new entity.Key({ + path: [ 'Kind1', 'StringId' ] + }); var proto = entity.keyToKeyProto(key); var protoWithNS = entity.keyToKeyProto(keyWithNS); + var protoWithNumericID = entity.keyToKeyProto(keyWithNumericID); + var protoWithStringID = entity.keyToKeyProto(keyWithStringID); assert.strictEqual(proto.partition_id, undefined); assert.strictEqual(proto.path_element[0].kind, 'Kind1'); @@ -208,6 +216,9 @@ describe('keyToKeyProto', function() { assert.strictEqual(protoWithNS.path_element[0].kind, 'Kind1'); assert.strictEqual(protoWithNS.path_element[0].id, undefined); assert.strictEqual(protoWithNS.path_element[0].name, undefined); + + assert.strictEqual(protoWithNumericID.path_element[0].id, 234); + assert.strictEqual(protoWithStringID.path_element[0].name, 'StringId'); }); it('should throw if key contains 0 items', function() {