Permalink
Browse files

Added more default value checks to converter, fixes #616

  • Loading branch information...
dcodeIO committed Jan 4, 2017
1 parent 62eef58 commit 9035d4872e32d6402c8e4d8c915d4f24d5192ea9
Showing with 70 additions and 76 deletions.
  1. +5 −3 src/converter.js
  2. +6 −17 src/converters.js
  3. +1 −1 tests/data/ambiguous-names.js
  4. +9 −9 tests/data/mapbox/vector_tile.js
  5. +1 −1 tests/data/package.js
  6. +48 −45 tests/data/test.js
@@ -65,10 +65,12 @@ function converter(mtype) {
// non-repeated
} else if (convert = genConvert(field, i, prop)) {
if (field.long || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
("if(s%s!==undefined&&s%s!==null||o.defaults)", prop, prop);
if (field.long) gen
("if(o.defaults||s%s!==undefined&&s%s!==null&&util.longNe(s%s,%d,%d))", prop, prop, prop, field.typeDefault.low, field.typeDefault.high);
else if (field.resolvedType && !(field.resolvedType instanceof Enum)) gen
("if(o.defaults||s%s!==undefined&&s%s!==null)", prop, prop);
else gen
("if(s%s!==undefined||o.defaults)", prop);
("if(o.defaults||s%s!==undefined&&s%s!==%j)", prop, prop, field.typeDefault);
gen
("d%s=%s", prop, convert);
} else gen
@@ -27,30 +27,22 @@ var util = require("./util/runtime");
*/
converters.json = {
create: function(value, typeOrCtor, options) {
if (!value)
if (!value) // inner messages
return null;
return options.fieldsOnly
? {}
: util.merge({}, value);
},
enums: function(value, defaultValue, values, options) {
if (!options.defaults) {
if (value === undefined || value === defaultValue)
return undefined;
} else if (value === undefined)
if (value === undefined)
value = defaultValue;
return options.enums === String && typeof value === "number"
? values[value]
: value;
},
longs: function(value, defaultLow, defaultHigh, unsigned, options) {
if (!value) {
if (options.defaults)
value = { low: defaultLow, high: defaultHigh };
else
return undefined;
} else if (!util.longNe(value, defaultLow, defaultHigh) && !options.defaults)
return undefined;
if (value === undefined || value === null)
value = { low: defaultLow, high: defaultHigh };
if (options.longs === Number)
return typeof value === "number"
? value
@@ -66,10 +58,7 @@ converters.json = {
},
bytes: function(value, defaultValue, options) {
if (!value) {
if (options.defaults)
value = defaultValue;
else
return undefined;
value = defaultValue;
} else if (!value.length && !options.defaults)
return undefined;
return options.bytes === String
@@ -105,7 +94,7 @@ converters.message = {
enums: function(value, defaultValue, values) {
if (typeof value === "string")
return values[value];
return value | 0;
return value;
},
longs: function(value, defaultLow, defaultHigh, unsigned) {
if (typeof value === "string")
@@ -296,7 +296,7 @@ $root.B = (function() {
}
var dst = impl.create(src, this, options);
if (dst) {
if (src.A !== undefined && src.A !== null || options.defaults) {
if (options.defaults || src.A !== undefined && src.A !== null) {
dst.A = types[0].convert(src.A, impl, options);
}
}
@@ -441,7 +441,7 @@ $root.vector_tile = (function() {
* @param {Object.<string,*>} [options] Conversion options
* @returns {vector_tile.Tile.Value|Object} Converted message
*/
Value.convert = (function() { return function convert(src, impl, options) {
Value.convert = (function(util) { return function convert(src, impl, options) {
if (!options) {
options = {};
}
@@ -456,21 +456,21 @@ $root.vector_tile = (function() {
if (dst.doubleValue === undefined && options.defaults) {
dst.doubleValue = 0;
}
if (src.intValue !== undefined && src.intValue !== null || options.defaults) {
if (options.defaults || src.intValue !== undefined && src.intValue !== null && util.longNe(src.intValue, 0, 0)) {
dst.intValue = impl.longs(src.intValue, 0, 0, false, options);
}
if (src.uintValue !== undefined && src.uintValue !== null || options.defaults) {
if (options.defaults || src.uintValue !== undefined && src.uintValue !== null && util.longNe(src.uintValue, 0, 0)) {
dst.uintValue = impl.longs(src.uintValue, 0, 0, true, options);
}
if (src.sintValue !== undefined && src.sintValue !== null || options.defaults) {
if (options.defaults || src.sintValue !== undefined && src.sintValue !== null && util.longNe(src.sintValue, 0, 0)) {
dst.sintValue = impl.longs(src.sintValue, 0, 0, false, options);
}
if (dst.boolValue === undefined && options.defaults) {
dst.boolValue = false;
}
}
return dst;
};})();
};})($protobuf.util);
/**
* Creates a Value message from JSON.
@@ -717,13 +717,13 @@ $root.vector_tile = (function() {
* @param {Object.<string,*>} [options] Conversion options
* @returns {vector_tile.Tile.Feature|Object} Converted message
*/
Feature.convert = (function(types) { return function convert(src, impl, options) {
Feature.convert = (function(util, types) { return function convert(src, impl, options) {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
if (dst) {
if (src.id !== undefined && src.id !== null || options.defaults) {
if (options.defaults || src.id !== undefined && src.id !== null && util.longNe(src.id, 0, 0)) {
dst.id = impl.longs(src.id, 0, 0, true, options);
}
if (src.tags && src.tags.length) {
@@ -736,7 +736,7 @@ $root.vector_tile = (function() {
dst.tags = [];
}
}
if (src.type !== undefined || options.defaults) {
if (options.defaults || src.type !== undefined && src.type !== undefined) {
dst.type = impl.enums(src.type, undefined, types[2], options);
}
if (src.geometry && src.geometry.length) {
@@ -751,7 +751,7 @@ $root.vector_tile = (function() {
}
}
return dst;
};})($types);
};})($protobuf.util, $types);
/**
* Creates a Feature message from JSON.
@@ -520,7 +520,7 @@ $root.Package = (function() {
if (dst.license === undefined && options.defaults) {
dst.license = "";
}
if (src.repository !== undefined && src.repository !== null || options.defaults) {
if (options.defaults || src.repository !== undefined && src.repository !== null) {
dst.repository = types[5].convert(src.repository, impl, options);
}
if (dst.bugs === undefined && options.defaults) {
Oops, something went wrong.

0 comments on commit 9035d48

Please sign in to comment.