Skip to content

Commit

Permalink
Merge pull request #302 from ryanseys/buffer-fix
Browse files Browse the repository at this point in the history
fix: return correct buffer object for datastore responses
  • Loading branch information
stephenplusplus committed Nov 17, 2014
2 parents 46036ae + c218eb8 commit d04c190
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
* });
* // <Buffer 68 65 6c 6c 6f>
*/
Expand All @@ -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(
Expand All @@ -384,6 +384,8 @@ function propertyToValue(property) {
}
}

module.exports.propertyToValue = propertyToValue;

/**
* Convert any native value to a property object.
*
Expand Down
22 changes: 22 additions & 0 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
12 changes: 12 additions & 0 deletions test/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d04c190

Please sign in to comment.