Skip to content

Commit

Permalink
Fixed: An enum's default value present as a string looks up using typ…
Browse files Browse the repository at this point in the history
…eDefault, not defaultValue which is an array if repeated
  • Loading branch information
dcodeIO committed Jan 16, 2017
1 parent b5daa27 commit 08cda24
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 53 deletions.
16 changes: 6 additions & 10 deletions dist/noparse/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/noparse/protobuf.js.map

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

16 changes: 6 additions & 10 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.

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

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

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
12 changes: 4 additions & 8 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,30 +192,26 @@ converter.toObject = function toObject(mtype) {
("if(!o)")
("o={}")
("var d={}");
var repeatedFields = fields.filter(function(field) { return field.repeated; });
var repeatedFields = fields.filter(function(field) { return field.resolve().repeated; });
if (repeatedFields.length) { gen
("if(o.arrays||o.defaults){");
fields.forEach(function(field) {
if (field.resolve().repeated) gen
repeatedFields.forEach(function(field) { gen
("d%s=[]", field._prop);
}); gen
("}");
}
var mapFields = fields.filter(function(field) { return field.map; });
if (mapFields.length) { gen
("if(o.objects||o.defaults){");
fields.forEach(function(field) {
if (field.map) gen
mapFields.forEach(function(field) { gen
("d%s={}", field._prop);
}); gen
("}");
}
var otherFields = fields.filter(function(field) { return !(field.repeated || field.map); });
if (otherFields.length) { gen
("if(o.defaults){");
fields.forEach(function(field) {
if (field.repeated || field.map)
return;
otherFields.forEach(function(field) {
if (field.resolvedType instanceof Enum) gen
("d%s=o.enums===String?%j:%j", field._prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault);
else if (field.long) gen
Expand Down
2 changes: 1 addition & 1 deletion src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ FieldPrototype.resolve = function resolve() {
if (this.options && this.options["default"] !== undefined) {
this.typeDefault = this.options["default"];
if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
this.typeDefault = this.resolvedType.values[this.defaultValue];
this.typeDefault = this.resolvedType.values[this.typeDefault];
}

// convert to internal data type if necesssary
Expand Down
4 changes: 2 additions & 2 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ $root.vector_tile = (function() {
* Feature type.
* @type {number}
*/
Feature.prototype.type = undefined;
Feature.prototype.type = 0;

/**
* Feature geometry.
Expand Down Expand Up @@ -796,7 +796,7 @@ $root.vector_tile = (function() {
object.id = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long;
} else
object.id = options.longs === String ? "0" : 0;
object.type = options.enums === String ? undefined : undefined;
object.type = options.enums === String ? "UNKNOWN" : 0;
}
if (message.hasOwnProperty("id") && message.id !== undefined && message.id !== null)
if (typeof message.id === "number")
Expand Down
20 changes: 10 additions & 10 deletions tests/data/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@ $root.jspb = (function() {
* DefaultValues enumField.
* @type {number}
*/
DefaultValues.prototype.enumField = undefined;
DefaultValues.prototype.enumField = 13;

/**
* DefaultValues emptyField.
Expand Down Expand Up @@ -3028,7 +3028,7 @@ $root.jspb = (function() {
object.intField = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long;
} else
object.intField = options.longs === String ? "11" : 11;
object.enumField = options.enums === String ? undefined : undefined;
object.enumField = options.enums === String ? "E1" : 13;
object.emptyField = "";
object.bytesField = options.bytes === String ? "moo" : [
109,
Expand Down Expand Up @@ -10137,7 +10137,7 @@ $root.google = (function() {
* FileOptions optimizeFor.
* @type {number}
*/
FileOptions.prototype.optimizeFor = undefined;
FileOptions.prototype.optimizeFor = 1;

/**
* FileOptions goPackage.
Expand Down Expand Up @@ -10487,7 +10487,7 @@ $root.google = (function() {
object.javaMultipleFiles = false;
object.javaGenerateEqualsAndHash = false;
object.javaStringCheckUtf8 = false;
object.optimizeFor = options.enums === String ? undefined : undefined;
object.optimizeFor = options.enums === String ? "SPEED" : 1;
object.goPackage = "";
object.ccGenericServices = false;
object.javaGenericServices = false;
Expand Down Expand Up @@ -10848,7 +10848,7 @@ $root.google = (function() {
* FieldOptions ctype.
* @type {number}
*/
FieldOptions.prototype.ctype = undefined;
FieldOptions.prototype.ctype = 0;

/**
* FieldOptions packed.
Expand All @@ -10860,7 +10860,7 @@ $root.google = (function() {
* FieldOptions jstype.
* @type {number}
*/
FieldOptions.prototype.jstype = undefined;
FieldOptions.prototype.jstype = 0;

/**
* FieldOptions lazy.
Expand Down Expand Up @@ -11114,9 +11114,9 @@ $root.google = (function() {
if (options.arrays || options.defaults)
object.uninterpretedOption = [];
if (options.defaults) {
object.ctype = options.enums === String ? undefined : undefined;
object.ctype = options.enums === String ? "STRING" : 0;
object.packed = false;
object.jstype = options.enums === String ? undefined : undefined;
object.jstype = options.enums === String ? "JS_NORMAL" : 0;
object.lazy = false;
object.deprecated = false;
object.weak = false;
Expand Down Expand Up @@ -12039,7 +12039,7 @@ $root.google = (function() {
* MethodOptions idempotencyLevel.
* @type {number}
*/
MethodOptions.prototype.idempotencyLevel = undefined;
MethodOptions.prototype.idempotencyLevel = 0;

/**
* MethodOptions uninterpretedOption.
Expand Down Expand Up @@ -12217,7 +12217,7 @@ $root.google = (function() {
object.uninterpretedOption = [];
if (options.defaults) {
object.deprecated = false;
object.idempotencyLevel = options.enums === String ? undefined : undefined;
object.idempotencyLevel = options.enums === String ? "IDEMPOTENCY_UNKNOWN" : 0;
}
if (message.hasOwnProperty("deprecated") && message.deprecated !== undefined && message.deprecated !== null)
object.deprecated = message.deprecated;
Expand Down

0 comments on commit 08cda24

Please sign in to comment.