Skip to content

Commit

Permalink
Fixed: Post-merge, also tackles #683
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Feb 23, 2017
1 parent a6563fe commit 7c3506b
Show file tree
Hide file tree
Showing 28 changed files with 681 additions and 517 deletions.
8 changes: 7 additions & 1 deletion cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,15 @@ function buildEnum(ref, enm) {
push(name(ref) + "." + name(enm.name) + " = (function() {");
++indent;
push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
var aliased = [];
Object.keys(enm.values).forEach(function(key) {
var val = enm.values[key];
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");
if (aliased.indexOf(val) >= -1)
push("values[" + JSON.stringify(key) + "] = " + val + ";");
else {
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");
aliased.push(val);
}
});
push("return values;");
--indent;
Expand Down
16 changes: 11 additions & 5 deletions dist/light/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/light/protobuf.js.map

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/minimal/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/minimal/protobuf.min.js

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

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
22 changes: 15 additions & 7 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.

8 changes: 4 additions & 4 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.

12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class Field extends ReflectionObject {
* Field rule, if any.
* @type {string|undefined}
*/
rule: (string|undefined);
rule?: string;

/**
* Field type.
Expand All @@ -283,7 +283,7 @@ export class Field extends ReflectionObject {
* Extended type if different from parent.
* @type {string|undefined}
*/
extend: (string|undefined);
extend?: string;

/**
* Whether this field is required.
Expand Down Expand Up @@ -711,7 +711,7 @@ export class Method extends ReflectionObject {
* Whether requests are streamed or not.
* @type {boolean|undefined}
*/
requestStream: (boolean|undefined);
requestStream?: boolean;

/**
* Response type.
Expand All @@ -723,7 +723,7 @@ export class Method extends ReflectionObject {
* Whether responses are streamed or not.
* @type {boolean|undefined}
*/
responseStream: (boolean|undefined);
responseStream?: boolean;

/**
* Resolved request type.
Expand Down Expand Up @@ -806,7 +806,7 @@ export abstract class NamespaceBase extends ReflectionObject {
* Nested objects by name.
* @type {Object.<string,ReflectionObject>|undefined}
*/
nested: ({ [k: string]: ReflectionObject }|undefined);
nested?: { [k: string]: ReflectionObject };

/**
* Nested objects of this namespace as an array for iteration.
Expand Down Expand Up @@ -933,7 +933,7 @@ export abstract class ReflectionObject {
* Options.
* @type {Object.<string,*>|undefined}
*/
options: ({ [k: string]: any }|undefined);
options?: { [k: string]: any };

/**
* Unique name within its namespace.
Expand Down
10 changes: 6 additions & 4 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ Enum.prototype.add = function(name, id, comment) {
if (this.values[name] !== undefined)
throw Error("duplicate name");

if (this.valuesById[id] !== undefined && !(this.options && this.options.allow_alias))
throw Error("duplicate id");
if (this.valuesById[id] !== undefined) {
if (!(this.options && this.options.allow_alias))
throw Error("duplicate id");
this.values[name] = id;
} else
this.valuesById[this.values[name] = id] = name;

this.valuesById[this.values[name] = id] = name;
this.comments[name] = comment || null;

return this;
};

Expand Down
4 changes: 4 additions & 0 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ Field.prototype.resolve = function resolve() {
this.typeDefault = this.resolvedType.values[this.typeDefault];
}

// remove unnecessary packed option (parser adds this) if not referencing an enum
if (this.options && this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
delete this.options.packed;

// convert to internal data type if necesssary
if (this.long) {
this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
Expand Down
6 changes: 4 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ function parse(source, root, options) {
if (!field.comment)
field.comment = cmnt(trailingLine);
// JSON defaults to packed=true if not set so we have to set packed=false explicity when
// parsing proto2 descriptors without the option, where applicable.
if (field.repeated && types.packed[type] !== undefined && !isProto3)
// parsing proto2 descriptors without the option, where applicable. This must be done for
// any type (not just packable types) because enums also use varint encoding and it is not
// yet known whether a type is an enum or not.
if (!isProto3 && field.repeated)
field.setOption("packed", false, /* ifNotSet */ true);
parent.add(field);
}
Expand Down
2 changes: 1 addition & 1 deletion src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Root.prototype.load = function load(filename, options, callback) {
if (err) {
if (!weak)
finish(err);
else if (!queued)
else /* istanbul ignore next */ if (!queued) // can't be covered reliably
finish(null, self);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Type.prototype.add = function add(object) {
// type instead.

// avoids calling the getter if not absolutely necessary because it's called quite frequently
if (this._fieldsById ? this._fieldsById[object.id] : this.fieldsById[object.id])
if (this._fieldsById ? /* istanbul ignore next */ this._fieldsById[object.id] : this.fieldsById[object.id])
throw Error("duplicate id " + object.id + " in " + this);

if (object.parent)
Expand Down
18 changes: 11 additions & 7 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ $root.Test1 = (function() {

/**
* Field with a comment.
* @type {string}
* @type {string|undefined}
*/
Test1.prototype.field1 = "";

/**
* Test1 field2.
* @type {number}
* @type {number|undefined}
*/
Test1.prototype.field2 = 0;

/**
* Field with a comment and a <a href="http://example.com/foo/">link</a>
* @type {boolean}
* @type {boolean|undefined}
*/
Test1.prototype.field3 = false;

Expand Down Expand Up @@ -132,6 +132,8 @@ $root.Test1 = (function() {
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Test1.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.field1 !== undefined)
if (!$util.isString(message.field1))
return "field1: string expected";
Expand Down Expand Up @@ -297,7 +299,9 @@ $root.Test2 = (function() {
* @param {Test2|Object} message Test2 message or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Test2.verify = function verify() {
Test2.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
return null;
};

Expand Down Expand Up @@ -361,9 +365,9 @@ $root.Test2 = (function() {
*/
$root.Test3 = (function() {
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "ONE"] = 1;
values[valuesById[2] = "TWO"] = 2;
values[valuesById[3] = "THREE"] = 3;
values["ONE"] = 1;
values["TWO"] = 2;
values["THREE"] = 3;
return values;
})();

Expand Down
24 changes: 13 additions & 11 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,55 @@ $root.Message = (function() {

/**
* Message stringVal.
* @type {string}
* @type {string|undefined}
*/
Message.prototype.stringVal = "";

/**
* Message stringRepeated.
* @type {Array.<string>}
* @type {Array.<string>|undefined}
*/
Message.prototype.stringRepeated = $util.emptyArray;

/**
* Message uint64Val.
* @type {number|$protobuf.Long}
* @type {number|$protobuf.Long|undefined}
*/
Message.prototype.uint64Val = $util.Long ? $util.Long.fromBits(0,0,true) : 0;

/**
* Message uint64Repeated.
* @type {Array.<number|$protobuf.Long>}
* @type {Array.<number|$protobuf.Long>|undefined}
*/
Message.prototype.uint64Repeated = $util.emptyArray;

/**
* Message bytesVal.
* @type {Uint8Array}
* @type {Uint8Array|undefined}
*/
Message.prototype.bytesVal = $util.newBuffer([]);

/**
* Message bytesRepeated.
* @type {Array.<Uint8Array>}
* @type {Array.<Uint8Array>|undefined}
*/
Message.prototype.bytesRepeated = $util.emptyArray;

/**
* Message enumVal.
* @type {number}
* @type {number|undefined}
*/
Message.prototype.enumVal = 1;

/**
* Message enumRepeated.
* @type {Array.<number>}
* @type {Array.<number>|undefined}
*/
Message.prototype.enumRepeated = $util.emptyArray;

/**
* Message int64Map.
* @type {Object.<string,number|$protobuf.Long>}
* @type {Object.<string,number|$protobuf.Long>|undefined}
*/
Message.prototype.int64Map = $util.emptyObject;

Expand Down Expand Up @@ -234,6 +234,8 @@ $root.Message = (function() {
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
Message.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.stringVal !== undefined)
if (!$util.isString(message.stringVal))
return "stringVal: string expected";
Expand Down Expand Up @@ -504,8 +506,8 @@ $root.Message = (function() {
*/
Message.SomeEnum = (function() {
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "ONE"] = 1;
values[valuesById[2] = "TWO"] = 2;
values["ONE"] = 1;
values["TWO"] = 2;
return values;
})();

Expand Down
Loading

0 comments on commit 7c3506b

Please sign in to comment.