Comparing version 1.1.14 to 1.1.16
@@ -5,9 +5,30 @@ 'use strict'; | ||
module.exports = function() { | ||
this.stack = []; | ||
}; | ||
module.exports.prototype.getValue = function(jo, field) { | ||
function getErrorObjectSnap(jo) { | ||
var o = {}; | ||
var i = 0; | ||
for(var field in jo) { | ||
if(typeof jo[field] == "object") { | ||
continue; | ||
} | ||
if(i > 5) { | ||
break; | ||
} | ||
o[field] = jo[field]; | ||
++i; | ||
} | ||
var str = JSON.stringify(o); | ||
if(i > 5) { | ||
str = str.substring(0, str.length - 1); | ||
str += '........' + '}'; | ||
} | ||
return str; | ||
} | ||
module.exports.prototype.getValue = function(value, field, jo) { | ||
var type = field.type; | ||
var resolvedType = field.resolvedType; | ||
if(jo == null) { | ||
if(value == null) { | ||
return type?type.defaultValue : null; | ||
@@ -27,18 +48,18 @@ } | ||
case "sfixed32": | ||
if(!Number.isInteger(jo)) { | ||
throw new Error(util.format("Field[%s] not integer", field.name)); | ||
if(!Number.isInteger(value)) { | ||
throw new Error(util.format("Field[\"%s\":%s] not integer, in %s", field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
if((jo < int32_min) || (jo > int32_max)) { | ||
throw new Error(util.format("Field[%s] integer Overflow", field.name)); | ||
if((value < int32_min) || (value > int32_max)) { | ||
throw new Error(util.format("Integer[%s] Overflow at \"%s\":%s, in %s", field.type.name, field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
return jo; | ||
return value; | ||
case "uint32": | ||
case "fixed32": | ||
if(!Number.isInteger(jo)) { | ||
throw new Error(util.format("Field[%s] not integer", field.name)); | ||
if(!Number.isInteger(value)) { | ||
throw new Error(util.format("Field[\"%s\":%s] not integer, in %s", field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
if((jo < 0) || (jo > uint32_max)) { | ||
throw new Error(util.format("Field[%s] integer Overflow", field.name)); | ||
if((value < 0) || (value > uint32_max)) { | ||
throw new Error(util.format("Integer[%s] Overflow at \"%s\":%s, in %s", field.type.name, field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
return jo; | ||
return value; | ||
@@ -48,21 +69,21 @@ case "int64": | ||
case "sfixed64": | ||
if(!Number.isInteger(jo)) { | ||
throw new Error(util.format("Field[%s] not integer", field.name)); | ||
if(!Number.isInteger(value)) { | ||
throw new Error(util.format("Field[\"%s\":%s] not integer, in %s", field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
if((jo < int64_min) || (jo > int64_max)) { | ||
throw new Error(util.format("Field[%s] integer Overflow", field.name)); | ||
if((value < int64_min) || (value > int64_max)) { | ||
throw new Error(util.format("Integer[%s] Overflow at \"%s\":%s, in %s", field.type.name, field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
return jo; | ||
return value; | ||
case "uint64": | ||
case "fixed64": | ||
if(!Number.isInteger(jo)) { | ||
throw new Error(util.format("Field[%s] not integer", field.name)); | ||
if(!Number.isInteger(value)) { | ||
throw new Error(util.format("Field[\"%s\":%s] not integer, in %s", field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
if((jo < 0) || (jo > uint64_max)) { | ||
throw new Error(util.format("Field[%s] integer Overflow", field.name)); | ||
if((value < 0) || (value > uint64_max)) { | ||
throw new Error(util.format("Integer[%s] Overflow at \"%s\":%s, in %s", field.type.name, field.name, value, getErrorObjectSnap(jo))); | ||
} | ||
return jo; | ||
return value; | ||
case "message" : | ||
return this.jsonDecode(jo, resolvedType.clazz); | ||
return this.jsonDecode(value, resolvedType.clazz); | ||
case "float": | ||
@@ -73,5 +94,5 @@ case "double": | ||
case "enum": | ||
return jo; | ||
return value; | ||
default: | ||
throw new Error('Field[%s] type unknow.', field.name, type.name); | ||
throw new Error(util.format("Field[%s] type unknow, in %s", field.name, getErrorObjectSnap(jo))); | ||
} | ||
@@ -81,4 +102,2 @@ }; | ||
module.exports.prototype.jsonDecode = function(jo, T) { | ||
this.stack.push(jo); | ||
var pbo = new T(); | ||
@@ -89,3 +108,2 @@ for(var field of pbo.$type.children) | ||
try { | ||
if(field.required) { | ||
@@ -101,19 +119,11 @@ if(fjo == undefined) { | ||
for(var element of fjo) { | ||
var fpbo = this.getValue(element, field); | ||
var fpbo = this.getValue(element, field, jo); | ||
pbo.add(field.name, fpbo); | ||
} | ||
} else { | ||
var fpbo = this.getValue(fjo, field); | ||
var fpbo = this.getValue(fjo, field, jo); | ||
pbo.set(field.name, fpbo); | ||
} | ||
} catch (error) { | ||
error.jo = jo; | ||
error.ejo = { | ||
}; | ||
error.ejo[field.name] = fjo; | ||
throw error; | ||
} | ||
} | ||
this.stack.pop(); | ||
return pbo; | ||
}; |
{ | ||
"name": "json2pb", | ||
"version": "1.1.14", | ||
"version": "1.1.16", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Randy Liu <randyliu@foxmail.com>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58968
24
129