Skip to content

Commit 54283d3

Browse files
committed
Handle oneofs in prototype ctor, add non-ES5 fallbacks, test case
1 parent 09865d0 commit 54283d3

File tree

10 files changed

+76
-16
lines changed

10 files changed

+76
-16
lines changed

dist/protobuf.js

Lines changed: 17 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

41 Bytes
Binary file not shown.

dist/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/inherits.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ inherits.defineProperties = function defineProperties(prototype, type) {
167167

168168
// Define each oneof with a non-enumerable getter and setter for the present field
169169
type.getOneofsArray().forEach(function(oneof) {
170-
prototypeProperties[oneof.resolve().name] = {
171-
get: function() {
170+
oneof.resolve();
171+
prototypeProperties[oneof.name] = {
172+
get: prototype['get' + oneof.ucName] = function() {
172173
var keys = oneof.oneof;
173174
for (var i = 0; i < keys.length; ++i) {
174175
var field = oneof.parent.fields[keys[i]];
@@ -177,7 +178,7 @@ inherits.defineProperties = function defineProperties(prototype, type) {
177178
}
178179
return undefined;
179180
},
180-
set: function(value) {
181+
set: prototype['set' + oneof.ucName] = function(value) {
181182
var keys = oneof.oneof;
182183
for (var i = 0; i < keys.length; ++i) {
183184
if (keys[i] !== value)

src/oneof.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ function OneOf(name, fieldNames, options) {
2828
if (fieldNames && !Array.isArray(fieldNames))
2929
throw _TypeError("fieldNames", "an Array");
3030

31+
/**
32+
* Upper cased name for getter/setter calls.
33+
* @type {string}
34+
*/
35+
this.ucName = this.name.substring(0, 1).toUpperCase() + this.name.substring(1);
36+
3137
/**
3238
* Field names that belong to this oneof.
3339
* @type {Array.<string>}

src/prototype.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ function Prototype(properties, options) {
2424
var any = !(options && options.fieldsOnly),
2525
fields = this.constructor.$type.fields,
2626
keys = Object.keys(properties);
27-
for (var i = 0; i < keys.length; ++i)
28-
if (fields[keys[i]] || any)
27+
for (var i = 0; i < keys.length; ++i) {
28+
var field = fields[keys[i]];
29+
if (field && field.partOf)
30+
this['set' + field.partOf.ucName](field.name);
31+
if (field || any)
2932
this[keys[i]] = properties[keys[i]];
33+
}
3034
}
3135
}
3236

tests/data/oneof.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
3+
message Message {
4+
oneof kind {
5+
string str = 1;
6+
int32 num = 2;
7+
}
8+
}

tests/oneof.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var tape = require("tape");
2+
3+
var protobuf = require("..");
4+
5+
tape.test("oneofs", function(test) {
6+
7+
protobuf.load("tests/data/oneof.proto", function(err, root) {
8+
if (err)
9+
return test.fail(err.message);
10+
11+
var Message = root.lookup("Message");
12+
var message = Message.create({
13+
str: "a",
14+
num: 1
15+
});
16+
test.equal(message.num, 1, "should initialize the last value");
17+
test.equal(message.kind, "num", "should reference the last value");
18+
test.notOk(message.hasOwnProperty('str'), "should not initialize other values");
19+
20+
message.str = "a";
21+
message.setKind('str'); // message.kind = 'str' if IE8 support isn't required
22+
23+
test.notOk(message.hasOwnProperty('num'), "should delete the previous value");
24+
test.equal(message.str, "a", "should set the new value");
25+
test.equal(message.kind, "str", "should reference the new value");
26+
27+
test.end();
28+
});
29+
30+
});

0 commit comments

Comments
 (0)