Skip to content

Commit

Permalink
move excluding to entity encodeValue - pass exclude to nested values
Browse files Browse the repository at this point in the history
  • Loading branch information
bedeoverend committed Jan 11, 2017
1 parent ee64b07 commit acf3a8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
13 changes: 10 additions & 3 deletions packages/datastore/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ entity.decodeValueProto = decodeValueProto;
* Convert any native value to a protobuf Value message object.
*
* @param {*} value - Native value.
* @param {?boolean} exclude - Exclude value from index.
* @return {object}
*
* @example
Expand All @@ -222,9 +223,13 @@ entity.decodeValueProto = decodeValueProto;
* // stringValue: 'Hi'
* // }
*/
function encodeValue(value) {
function encodeValue(value, exclude) {
var valueProto = {};

if (is.boolean(exclude)) {
valueProto.excludeFromIndexes = exclude;
}

if (is.boolean(value)) {
valueProto.booleanValue = value;
return valueProto;
Expand Down Expand Up @@ -281,7 +286,9 @@ function encodeValue(value) {

if (is.array(value)) {
valueProto.arrayValue = {
values: value.map(entity.encodeValue)
values: value.map(function(val) {
return entity.encodeValue(val, exclude);
})
};
return valueProto;
}
Expand All @@ -297,7 +304,7 @@ function encodeValue(value) {

for (var prop in value) {
if (value.hasOwnProperty(prop)) {
value[prop] = entity.encodeValue(value[prop]);
value[prop] = entity.encodeValue(value[prop], exclude);
}
}
}
Expand Down
16 changes: 1 addition & 15 deletions packages/datastore/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,21 +923,7 @@ DatastoreRequest.prototype.save = function(entities, callback) {

if (is.array(entityObject.data)) {
entityProto.properties = entityObject.data.reduce(function(acc, data) {
var value = entity.encodeValue(data.value);

if (is.boolean(data.excludeFromIndexes)) {
var excluded = data.excludeFromIndexes;
var values = value.arrayValue && value.arrayValue.values;

if (values) {
values = values.map(propAssign('excludeFromIndexes', excluded));
} else {
value.excludeFromIndexes = data.excludeFromIndexes;
}
}

acc[data.name] = value;

acc[data.name] = entity.encodeValue(data.value, data.excludeFromIndexes);
return acc;
}, {});
} else {
Expand Down

0 comments on commit acf3a8d

Please sign in to comment.