Skip to content

Commit 9035d48

Browse files
committed
Added more default value checks to converter, fixes #616
1 parent 62eef58 commit 9035d48

File tree

6 files changed

+70
-76
lines changed

6 files changed

+70
-76
lines changed

src/converter.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ function converter(mtype) {
6565

6666
// non-repeated
6767
} else if (convert = genConvert(field, i, prop)) {
68-
if (field.long || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
69-
("if(s%s!==undefined&&s%s!==null||o.defaults)", prop, prop);
68+
if (field.long) gen
69+
("if(o.defaults||s%s!==undefined&&s%s!==null&&util.longNe(s%s,%d,%d))", prop, prop, prop, field.typeDefault.low, field.typeDefault.high);
70+
else if (field.resolvedType && !(field.resolvedType instanceof Enum)) gen
71+
("if(o.defaults||s%s!==undefined&&s%s!==null)", prop, prop);
7072
else gen
71-
("if(s%s!==undefined||o.defaults)", prop);
73+
("if(o.defaults||s%s!==undefined&&s%s!==%j)", prop, prop, field.typeDefault);
7274
gen
7375
("d%s=%s", prop, convert);
7476
} else gen

src/converters.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,22 @@ var util = require("./util/runtime");
2727
*/
2828
converters.json = {
2929
create: function(value, typeOrCtor, options) {
30-
if (!value)
30+
if (!value) // inner messages
3131
return null;
3232
return options.fieldsOnly
3333
? {}
3434
: util.merge({}, value);
3535
},
3636
enums: function(value, defaultValue, values, options) {
37-
if (!options.defaults) {
38-
if (value === undefined || value === defaultValue)
39-
return undefined;
40-
} else if (value === undefined)
37+
if (value === undefined)
4138
value = defaultValue;
4239
return options.enums === String && typeof value === "number"
4340
? values[value]
4441
: value;
4542
},
4643
longs: function(value, defaultLow, defaultHigh, unsigned, options) {
47-
if (!value) {
48-
if (options.defaults)
49-
value = { low: defaultLow, high: defaultHigh };
50-
else
51-
return undefined;
52-
} else if (!util.longNe(value, defaultLow, defaultHigh) && !options.defaults)
53-
return undefined;
44+
if (value === undefined || value === null)
45+
value = { low: defaultLow, high: defaultHigh };
5446
if (options.longs === Number)
5547
return typeof value === "number"
5648
? value
@@ -66,10 +58,7 @@ converters.json = {
6658
},
6759
bytes: function(value, defaultValue, options) {
6860
if (!value) {
69-
if (options.defaults)
70-
value = defaultValue;
71-
else
72-
return undefined;
61+
value = defaultValue;
7362
} else if (!value.length && !options.defaults)
7463
return undefined;
7564
return options.bytes === String
@@ -105,7 +94,7 @@ converters.message = {
10594
enums: function(value, defaultValue, values) {
10695
if (typeof value === "string")
10796
return values[value];
108-
return value | 0;
97+
return value;
10998
},
11099
longs: function(value, defaultLow, defaultHigh, unsigned) {
111100
if (typeof value === "string")

tests/data/ambiguous-names.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ $root.B = (function() {
296296
}
297297
var dst = impl.create(src, this, options);
298298
if (dst) {
299-
if (src.A !== undefined && src.A !== null || options.defaults) {
299+
if (options.defaults || src.A !== undefined && src.A !== null) {
300300
dst.A = types[0].convert(src.A, impl, options);
301301
}
302302
}

tests/data/mapbox/vector_tile.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ $root.vector_tile = (function() {
441441
* @param {Object.<string,*>} [options] Conversion options
442442
* @returns {vector_tile.Tile.Value|Object} Converted message
443443
*/
444-
Value.convert = (function() { return function convert(src, impl, options) {
444+
Value.convert = (function(util) { return function convert(src, impl, options) {
445445
if (!options) {
446446
options = {};
447447
}
@@ -456,21 +456,21 @@ $root.vector_tile = (function() {
456456
if (dst.doubleValue === undefined && options.defaults) {
457457
dst.doubleValue = 0;
458458
}
459-
if (src.intValue !== undefined && src.intValue !== null || options.defaults) {
459+
if (options.defaults || src.intValue !== undefined && src.intValue !== null && util.longNe(src.intValue, 0, 0)) {
460460
dst.intValue = impl.longs(src.intValue, 0, 0, false, options);
461461
}
462-
if (src.uintValue !== undefined && src.uintValue !== null || options.defaults) {
462+
if (options.defaults || src.uintValue !== undefined && src.uintValue !== null && util.longNe(src.uintValue, 0, 0)) {
463463
dst.uintValue = impl.longs(src.uintValue, 0, 0, true, options);
464464
}
465-
if (src.sintValue !== undefined && src.sintValue !== null || options.defaults) {
465+
if (options.defaults || src.sintValue !== undefined && src.sintValue !== null && util.longNe(src.sintValue, 0, 0)) {
466466
dst.sintValue = impl.longs(src.sintValue, 0, 0, false, options);
467467
}
468468
if (dst.boolValue === undefined && options.defaults) {
469469
dst.boolValue = false;
470470
}
471471
}
472472
return dst;
473-
};})();
473+
};})($protobuf.util);
474474

475475
/**
476476
* Creates a Value message from JSON.
@@ -717,13 +717,13 @@ $root.vector_tile = (function() {
717717
* @param {Object.<string,*>} [options] Conversion options
718718
* @returns {vector_tile.Tile.Feature|Object} Converted message
719719
*/
720-
Feature.convert = (function(types) { return function convert(src, impl, options) {
720+
Feature.convert = (function(util, types) { return function convert(src, impl, options) {
721721
if (!options) {
722722
options = {};
723723
}
724724
var dst = impl.create(src, this, options);
725725
if (dst) {
726-
if (src.id !== undefined && src.id !== null || options.defaults) {
726+
if (options.defaults || src.id !== undefined && src.id !== null && util.longNe(src.id, 0, 0)) {
727727
dst.id = impl.longs(src.id, 0, 0, true, options);
728728
}
729729
if (src.tags && src.tags.length) {
@@ -736,7 +736,7 @@ $root.vector_tile = (function() {
736736
dst.tags = [];
737737
}
738738
}
739-
if (src.type !== undefined || options.defaults) {
739+
if (options.defaults || src.type !== undefined && src.type !== undefined) {
740740
dst.type = impl.enums(src.type, undefined, types[2], options);
741741
}
742742
if (src.geometry && src.geometry.length) {
@@ -751,7 +751,7 @@ $root.vector_tile = (function() {
751751
}
752752
}
753753
return dst;
754-
};})($types);
754+
};})($protobuf.util, $types);
755755

756756
/**
757757
* Creates a Feature message from JSON.

tests/data/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ $root.Package = (function() {
520520
if (dst.license === undefined && options.defaults) {
521521
dst.license = "";
522522
}
523-
if (src.repository !== undefined && src.repository !== null || options.defaults) {
523+
if (options.defaults || src.repository !== undefined && src.repository !== null) {
524524
dst.repository = types[5].convert(src.repository, impl, options);
525525
}
526526
if (dst.bugs === undefined && options.defaults) {

0 commit comments

Comments
 (0)