Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return correct buffer object for datastore responses #302

Merged
merged 2 commits into from
Nov 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>

This comment was marked as spam.

This comment was marked as spam.

*/
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