Permalink
Browse files

Handle oneofs in prototype ctor, add non-ES5 fallbacks, test case

  • Loading branch information...
dcodeIO committed Dec 4, 2016
1 parent 09865d0 commit 54283d39c4c955b6a84f7f53d4940eec39e4df5e

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +41 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -167,8 +167,9 @@ inherits.defineProperties = function defineProperties(prototype, type) {
// Define each oneof with a non-enumerable getter and setter for the present field
type.getOneofsArray().forEach(function(oneof) {
prototypeProperties[oneof.resolve().name] = {
get: function() {
oneof.resolve();
prototypeProperties[oneof.name] = {
get: prototype['get' + oneof.ucName] = function() {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i) {
var field = oneof.parent.fields[keys[i]];
@@ -177,7 +178,7 @@ inherits.defineProperties = function defineProperties(prototype, type) {
}
return undefined;
},
set: function(value) {
set: prototype['set' + oneof.ucName] = function(value) {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i) {
if (keys[i] !== value)
@@ -28,6 +28,12 @@ function OneOf(name, fieldNames, options) {
if (fieldNames && !Array.isArray(fieldNames))
throw _TypeError("fieldNames", "an Array");
/**
* Upper cased name for getter/setter calls.
* @type {string}
*/
this.ucName = this.name.substring(0, 1).toUpperCase() + this.name.substring(1);
/**
* Field names that belong to this oneof.
* @type {Array.<string>}
@@ -24,9 +24,13 @@ function Prototype(properties, options) {
var any = !(options && options.fieldsOnly),
fields = this.constructor.$type.fields,
keys = Object.keys(properties);
for (var i = 0; i < keys.length; ++i)
if (fields[keys[i]] || any)
for (var i = 0; i < keys.length; ++i) {
var field = fields[keys[i]];
if (field && field.partOf)
this['set' + field.partOf.ucName](field.name);
if (field || any)
this[keys[i]] = properties[keys[i]];
}
}
}
@@ -0,0 +1,8 @@
syntax = "proto3";
message Message {
oneof kind {
string str = 1;
int32 num = 2;
}
}
@@ -0,0 +1,30 @@
var tape = require("tape");
var protobuf = require("..");
tape.test("oneofs", function(test) {
protobuf.load("tests/data/oneof.proto", function(err, root) {
if (err)
return test.fail(err.message);
var Message = root.lookup("Message");
var message = Message.create({
str: "a",
num: 1
});
test.equal(message.num, 1, "should initialize the last value");
test.equal(message.kind, "num", "should reference the last value");
test.notOk(message.hasOwnProperty('str'), "should not initialize other values");
message.str = "a";
message.setKind('str'); // message.kind = 'str' if IE8 support isn't required
test.notOk(message.hasOwnProperty('num'), "should delete the previous value");
test.equal(message.str, "a", "should set the new value");
test.equal(message.kind, "str", "should reference the new value");
test.end();
});
});

0 comments on commit 54283d3

Please sign in to comment.