Skip to content

Commit

Permalink
Mimic spec-compliant behaviour in oneof getVirtual, see #523
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 7, 2016
1 parent 38d867f commit ebae1e1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,18 @@ inherits.defineProperties = function defineProperties(prototype, type) {
type.getOneofsArray().forEach(function(oneof) {
util.prop(prototype, oneof.resolve().name, {
get: function getVirtual() {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i) {
var field = oneof.parent.fields[keys[i]];
if (this[keys[i]] != field.defaultValue) // eslint-disable-line eqeqeq
// > If the parser encounters multiple members of the same oneof on the wire, only the last member seen is used in the parsed message.
var keys = Object.keys(this);
for (var i = keys.length - 1; i > -1; --i)
if (oneof.oneof.indexOf(keys[i]) > -1)
return keys[i];
}
return undefined;
},
set: function setVirtual(value) {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i) {
for (var i = 0; i < keys.length; ++i)
if (keys[i] !== value)
delete this[keys[i]];
}
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/util/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ function pool(alloc, slice, size) {
offset = (offset | 7) + 1;
return buf;
};
};
}

0 comments on commit ebae1e1

Please sign in to comment.