Permalink
Browse files

Mimic spec-compliant behaviour in oneof getVirtual, see #523

  • Loading branch information...
dcodeIO committed Dec 7, 2016
1 parent 38d867f commit ebae1e18152617f11ac07827828f5740d4f2eb7e
Showing with 6 additions and 8 deletions.
  1. +5 −7 src/inherits.js
  2. +1 −1 src/util/pool.js
@@ -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]];
}
}
});
});
@@ -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.