Skip to content

Commit

Permalink
datastore: Avoid empty string if no namespace is set. Fixes #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Jul 9, 2014
1 parent c50be23 commit 2b07ac8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var keyToKeyProto = function(datasetId, key) {
if (key.length < 2) {
throw new Error("A key should contain at least a kind and an idenfier.")
}
var namespace = '', start = 0;
var namespace = null, start = 0;
if (key.length % 2 == 1) {
// the first item is the namepsace
namespace = key[0];
Expand All @@ -93,10 +93,14 @@ var keyToKeyProto = function(datasetId, key) {
}
path.push(p);
}
return {
partitionId: { datasetId: datasetId, namespace: namespace },
var proto = {
partitionId: { datasetId: datasetId },
path: path
};
if (namespace) {
proto.partitionId.namespace = namespace;
}
return proto;
};

module.exports.keyToKeyProto = keyToKeyProto;
Expand Down
4 changes: 2 additions & 2 deletions test/datastore.entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('keyToKeyProto', function() {
var key = ['Kind1', 1, 'Kind2', 'name'];
var proto = entity.keyToKeyProto('datasetId', key);
assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
assert.strictEqual(proto.partitionId.namespace, '');
assert.strictEqual(proto.partitionId.namespace, undefined);
assert.strictEqual(proto.path[0].kind, 'Kind1');
assert.strictEqual(proto.path[0].id, 1);
assert.strictEqual(proto.path[0].name, undefined);
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('keyToKeyProto', function() {
var protoWithNS = entity.keyToKeyProto('datasetId', keyWithNS);

assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
assert.strictEqual(proto.partitionId.namespace, '');
assert.strictEqual(proto.partitionId.namespace, undefined);
assert.strictEqual(proto.path[0].kind, 'Kind1');
assert.strictEqual(proto.path[0].id, undefined);
assert.strictEqual(proto.path[0].name, undefined);
Expand Down
1 change: 0 additions & 1 deletion test/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ describe('Dataset', function() {
assert.deepEqual(proto.keys[0], {
partitionId:{
datasetId: 's~test',
namespace: ''
},
path :[{kind:'Kind'}]
});
Expand Down

0 comments on commit 2b07ac8

Please sign in to comment.