Permalink
Browse files

Convert inner messages and undefined/null values more thoroughly, fixes

  • Loading branch information...
dcodeIO committed Jan 4, 2017
1 parent ee20b81 commit e3170a160079a3a7a99997a2661cdf654cb69e24
@@ -1,10 +1,10 @@
# <p align="center"><img src="./pbjs.png" /></p>
# <p align="center"><img alt="protobuf.js" src="./pbjs.png" /></p>
[![travis][travis-image]][travis-url] [![david][david-image]][david-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url]
**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).
**protobuf.js** is a pure JavaScript implementation with TypeScript support for node and the browser. It efficiently encodes all teh codez and works out of the box with .proto files.
**protobuf.js** is a pure JavaScript implementation with TypeScript support for node and the browser. It's super easy to use and works out of the box on .proto files!
[travis-image]: https://img.shields.io/travis/dcodeIO/protobuf.js.svg
[travis-url]: https://travis-ci.org/dcodeIO/protobuf.js
@@ -29,6 +29,7 @@ var root = protobuf.loadSync(require.resolve("./bench.proto")),
var buf = Test.encode(data).finish();
// warm up
process.stdout.write("warming up ...\n");
var i;
for (i = 0; i < 500000; ++i)
Test.encode(data).finish();
@@ -1,6 +1,3 @@
// $> pbts --main --global protobuf --out index.d.ts src
// Generated Tue, 03 Jan 2017 23:45:16 UTC
export as namespace protobuf;
/**
@@ -64,9 +64,14 @@ function converter(mtype) {
("d%s=[]", prop);
// non-repeated
} else if (convert = genConvert(field, i, prop)) gen
("d%s=%s", prop, convert);
else gen
} 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);
else gen
("if(s%s!==undefined||o.defaults)", prop);
gen
("d%s=%s", prop, convert);
} else gen
("if(d%s===undefined&&o.defaults)", prop)
("d%s=%j", prop, field.typeDefault /* == field.defaultValue */);
@@ -30,23 +30,6 @@ util.toArray = function toArray(object) {
}) : [];
};
/**
* Merges the properties of the source object into the destination object.
* @param {Object.<string,*>} dst Destination object
* @param {Object.<string,*>} src Source object
* @param {boolean} [ifNotSet=false] Merges only if the key is not already set
* @returns {Object.<string,*>} Destination object
*/
util.merge = function merge(dst, src, ifNotSet) {
if (src) {
var keys = Object.keys(src);
for (var i = 0; i < keys.length; ++i)
if (dst[keys[i]] === undefined || !ifNotSet)
dst[keys[i]] = src[keys[i]];
}
return dst;
};
/**
* Returns a safe property accessor for the specified properly name.
* @param {string} prop Property name
@@ -148,3 +148,20 @@ util.arrayNe = function arrayNe(a, b) {
return true;
return false;
};
/**
* Merges the properties of the source object into the destination object.
* @param {Object.<string,*>} dst Destination object
* @param {Object.<string,*>} src Source object
* @param {boolean} [ifNotSet=false] Merges only if the key is not already set
* @returns {Object.<string,*>} Destination object
*/
util.merge = function merge(dst, src, ifNotSet) {
if (src) {
var keys = Object.keys(src);
for (var i = 0; i < keys.length; ++i)
if (dst[keys[i]] === undefined || !ifNotSet)
dst[keys[i]] = src[keys[i]];
}
return dst;
};
@@ -134,7 +134,7 @@ $root.A = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.whatever === undefined && options.defaults) {
dst.whatever = "";
@@ -294,9 +294,11 @@ $root.B = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
dst.A = types[0].convert(src.A, impl, options);
if (!(src.A === undefined || src.A === null) || options.defaults) {
dst.A = types[0].convert(src.A, impl, options);
}
}
return dst;
};})($types);
@@ -9,7 +9,7 @@ message A {
message C {
message Two {
extend B.One {
Two two = 1000;
C.Two two = 1000;
}
}
}
@@ -157,7 +157,7 @@ $root.vector_tile = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (src.layers && src.layers.length) {
dst.layers = [];
@@ -445,7 +445,7 @@ $root.vector_tile = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.stringValue === undefined && options.defaults) {
dst.stringValue = "";
@@ -456,9 +456,15 @@ $root.vector_tile = (function() {
if (dst.doubleValue === undefined && options.defaults) {
dst.doubleValue = 0;
}
dst.intValue = impl.longs(src.intValue, 0, 0, false, options);
dst.uintValue = impl.longs(src.uintValue, 0, 0, true, options);
dst.sintValue = impl.longs(src.sintValue, 0, 0, false, options);
if (!(src.intValue === undefined || src.intValue === null) || options.defaults) {
dst.intValue = impl.longs(src.intValue, 0, 0, false, options);
}
if (!(src.uintValue === undefined || src.uintValue === null) || options.defaults) {
dst.uintValue = impl.longs(src.uintValue, 0, 0, true, options);
}
if (!(src.sintValue === undefined || src.sintValue === null) || options.defaults) {
dst.sintValue = impl.longs(src.sintValue, 0, 0, false, options);
}
if (dst.boolValue === undefined && options.defaults) {
dst.boolValue = false;
}
@@ -715,9 +721,11 @@ $root.vector_tile = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
dst.id = impl.longs(src.id, 0, 0, true, options);
if (!(src.id === undefined || src.id === null) || options.defaults) {
dst.id = impl.longs(src.id, 0, 0, true, options);
}
if (src.tags && src.tags.length) {
dst.tags = [];
for (var i = 0; i < src.tags.length; ++i) {
@@ -728,7 +736,9 @@ $root.vector_tile = (function() {
dst.tags = [];
}
}
dst.type = impl.enums(src.type, undefined, types[2], options);
if (!(src.type === undefined || src.type === null) || options.defaults) {
dst.type = impl.enums(src.type, undefined, types[2], options);
}
if (src.geometry && src.geometry.length) {
dst.geometry = [];
for (var i = 0; i < src.geometry.length; ++i) {
@@ -1007,7 +1017,7 @@ $root.vector_tile = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.version === undefined && options.defaults) {
dst.version = 1;
@@ -503,7 +503,7 @@ $root.Package = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.name === undefined && options.defaults) {
dst.name = "";
@@ -520,7 +520,9 @@ $root.Package = (function() {
if (dst.license === undefined && options.defaults) {
dst.license = "";
}
dst.repository = types[5].convert(src.repository, impl, options);
if (!(src.repository === undefined || src.repository === null) || options.defaults) {
dst.repository = types[5].convert(src.repository, impl, options);
}
if (dst.bugs === undefined && options.defaults) {
dst.bugs = "";
}
@@ -724,7 +726,7 @@ $root.Package = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.type === undefined && options.defaults) {
dst.type = "";
@@ -226,7 +226,7 @@ $root.MyRequest = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.path === undefined && options.defaults) {
dst.path = "";
@@ -382,7 +382,7 @@ $root.MyResponse = (function() {
if (!options) {
options = {};
}
var dst = impl.create(src, this, options);
var dst = impl.create(src, this, options) || null;
if (dst) {
if (dst.status === undefined && options.defaults) {
dst.status = 0;
Oops, something went wrong.

0 comments on commit e3170a1

Please sign in to comment.