Skip to content

Commit

Permalink
Handle oneofs in prototype ctor, add non-ES5 fallbacks, test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 4, 2016
1 parent 09865d0 commit 54283d3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 16 deletions.
23 changes: 17 additions & 6 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/oneof.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>}
Expand Down
8 changes: 6 additions & 2 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/data/oneof.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

message Message {
oneof kind {
string str = 1;
int32 num = 2;
}
}
30 changes: 30 additions & 0 deletions tests/oneof.js
Original file line number Diff line number Diff line change
@@ -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.