diff --git a/lib/datastore/entity.js b/lib/datastore/entity.js index e3e3c806034..7f77958bf13 100644 --- a/lib/datastore/entity.js +++ b/lib/datastore/entity.js @@ -334,17 +334,17 @@ module.exports.isKeyComplete = function(key) { * * @example * propertyToValue({ - * booleanValue: false + * boolean_value: false * }); * // false * * propertyToValue({ - * stringValue: 'Hi' + * string_value: 'Hi' * }); * // 'Hi' * * propertyToValue({ - * blobValue: 'aGVsbG8=' + * blob_value: new Buffer('68656c6c6f') * }); * // */ @@ -359,7 +359,7 @@ function propertyToValue(property) { return property.string_value; } if (exists(property.blob_value)) { - return new Buffer(property.blob_value, 'base64'); + return property.blob_value.toBuffer(); } if (exists(property.timestamp_microseconds_value)) { var microSecs = parseInt( @@ -384,6 +384,8 @@ function propertyToValue(property) { } } +module.exports.propertyToValue = propertyToValue; + /** * Convert any native value to a property object. * diff --git a/regression/datastore.js b/regression/datastore.js index 8dfe75a141f..ee0c4d46c6b 100644 --- a/regression/datastore.js +++ b/regression/datastore.js @@ -81,6 +81,28 @@ describe('datastore', function() { }); }); + it('should save/get/delete a buffer', function(done) { + var data = { + buf: new Buffer('010100000000000000000059400000000000006940', 'hex') + }; + ds.save({ + key: ds.key('Post'), + data: data + }, function (err, key) { + assert.ifError(err); + var assignedId = key.path[1]; + assert(assignedId); + ds.get(key, function (err, entity) { + assert.ifError(err); + assert.deepEqual(entity.data, data); + ds.delete(ds.key(['Post', assignedId]), function(err) { + assert.ifError(err); + done(); + }); + }); + }); + }); + it('should save/get/delete with a generated key id', function(done) { ds.save({ key: ds.key('Post'), diff --git a/test/datastore/entity.js b/test/datastore/entity.js index fffed3c4606..e3bb9c2250e 100644 --- a/test/datastore/entity.js +++ b/test/datastore/entity.js @@ -21,6 +21,7 @@ var assert = require('assert'); var entity = require('../../lib/datastore/entity.js'); var datastore = require('../../lib/datastore'); +var ByteBuffer = require('bytebuffer'); var blogPostMetadata = { title: { kind: String, indexed: true }, @@ -334,6 +335,17 @@ describe('queryToQueryProto', function() { }); }); +describe('propertyToValue', function() { + it('should translate a buffer', function() { + var buffer = new Buffer('010159406940'); + var property = { + blob_value: ByteBuffer.wrap(buffer) + }; + var returnedbuffer = entity.propertyToValue(property); + assert.deepEqual(buffer, returnedbuffer); + }); +}); + describe('valueToProperty', function() { it('should translate a boolean', function() { var val = entity.valueToProperty(true);