Skip to content

Commit

Permalink
Convert inner messages and undefined/null values more thoroughly, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 4, 2017
1 parent ee20b81 commit e3170a1
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 135 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down
11 changes: 8 additions & 3 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);

Expand Down
17 changes: 0 additions & 17 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
8 changes: 5 additions & 3 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/data/extend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ message A {
message C {
message Two {
extend B.One {
Two two = 1000;
C.Two two = 1000;
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 = "";
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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 = "";
}
Expand Down Expand Up @@ -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 = "";
Expand Down
4 changes: 2 additions & 2 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit e3170a1

Please sign in to comment.