Skip to content

Commit

Permalink
Merge pull request #190 from stephenplusplus/private-key
Browse files Browse the repository at this point in the history
datastore: refactor privatizing of Key values.
  • Loading branch information
silvolu committed Sep 8, 2014
2 parents d527fb7 + df599b1 commit 994da4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
18 changes: 13 additions & 5 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ var SIGN_TO_ORDER = {
* });
*/
function Key(options) {
this.namespace_ = options.namespace;
this.path_ = options.path;
Object.defineProperties(this, {
namespace: {
enumerable: true,
value: options.namespace
},
path: {
enumerable: true,
value: options.path
}
});
}

module.exports.Key = Key;
Expand Down Expand Up @@ -219,7 +227,7 @@ module.exports.keyFromKeyProto = keyFromKeyProto;
* // }
*/
function keyToKeyProto(key) {
var keyPath = key.path_;
var keyPath = key.path;
if (keyPath.length < 2) {
throw new Error('A key should contain at least a kind and an identifier.');
}
Expand All @@ -240,9 +248,9 @@ function keyToKeyProto(key) {
var proto = {
path_element: path
};
if (key.namespace_) {
if (key.namespace) {
proto.partition_id = {
namespace: key.namespace_
namespace: key.namespace
};
}
return proto;
Expand Down
10 changes: 5 additions & 5 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('datastore', function() {
var postKey = ds.key('Post', 'post1');
ds.save({ key: postKey, data: post }, function(err, key) {
assert.ifError(err);
assert.equal(key.path_[1], 'post1');
assert.equal(key.path[1], 'post1');
ds.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
Expand All @@ -70,7 +70,7 @@ describe('datastore', function() {
data: post
}, function(err, key) {
assert.ifError(err);
assert.equal(key.path_[1], 123456789);
assert.equal(key.path[1], 123456789);
ds.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
Expand All @@ -88,7 +88,7 @@ describe('datastore', function() {
data: post
}, function(err, key) {
assert.ifError(err);
var assignedId = key.path_[1];
var assignedId = key.path[1];
assert(assignedId);
ds.get(ds.key('Post', assignedId), function(err, entity) {
assert.ifError(err);
Expand Down Expand Up @@ -118,8 +118,8 @@ describe('datastore', function() {
], function(err, keys) {
assert.ifError(err);
assert.equal(keys.length,2);
var firstKey = ds.key('Post', keys[0].path_[1]);
var secondKey = ds.key('Post', keys[1].path_[1]);
var firstKey = ds.key('Post', keys[0].path[1]);
var secondKey = ds.key('Post', keys[1].path[1]);
ds.get([firstKey, secondKey], function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 2);
Expand Down
14 changes: 7 additions & 7 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('Dataset', function() {
it('should return a key scoped by namespace', function() {
var ds = new datastore.Dataset({ projectId: 'test', namespace: 'my-ns' });
var key = ds.key('Company', 1);
assert.equal(key.namespace_, 'my-ns');
assert.deepEqual(key.path_, ['Company', 1]);
assert.equal(key.namespace, 'my-ns');
assert.deepEqual(key.path, ['Company', 1]);
});

it('should allow namespace specification when creating a key', function() {
Expand All @@ -39,8 +39,8 @@ describe('Dataset', function() {
namespace: 'custom-ns',
path: ['Company', 1]
});
assert.equal(key.namespace_, 'custom-ns');
assert.deepEqual(key.path_, ['Company', 1]);
assert.equal(key.namespace, 'custom-ns');
assert.deepEqual(key.path, ['Company', 1]);
});

it('should get by key', function(done) {
Expand All @@ -52,7 +52,7 @@ describe('Dataset', function() {
};
ds.get(ds.key('Kind', 123), function(err, entity) {
var data = entity.data;
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.deepEqual(entity.key.path, ['Kind', 5732568548769792]);
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
Expand All @@ -71,7 +71,7 @@ describe('Dataset', function() {
ds.get([key], function(err, entities) {
var entity = entities[0];
var data = entity.data;
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.deepEqual(entity.key.path, ['Kind', 5732568548769792]);
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('Dataset', function() {

ds.runQuery(query, function (err, entities) {
assert.ifError(err);
assert.deepEqual(entities[0].key.path_, ['Kind', 5732568548769792]);
assert.deepEqual(entities[0].key.path, ['Kind', 5732568548769792]);

var data = entities[0].data;
assert.strictEqual(data.author, 'Silvano');
Expand Down

0 comments on commit 994da4e

Please sign in to comment.