Skip to content

Commit

Permalink
Added more default value checks to converter, fixes #616
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 4, 2017
1 parent 62eef58 commit 9035d48
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 76 deletions.
8 changes: 5 additions & 3 deletions src/converter.js
Expand Up @@ -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
Expand Down
23 changes: 6 additions & 17 deletions src/converters.js
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/data/ambiguous-names.js
Expand Up @@ -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);
}
}
Expand Down
18 changes: 9 additions & 9 deletions tests/data/mapbox/vector_tile.js
Expand Up @@ -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 = {};
}
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -751,7 +751,7 @@ $root.vector_tile = (function() {
}
}
return dst;
};})($types);
};})($protobuf.util, $types);

/**
* Creates a Feature message from JSON.
Expand Down
2 changes: 1 addition & 1 deletion tests/data/package.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 9035d48

Please sign in to comment.