Skip to content

Commit e3170a1

Browse files
committed
Convert inner messages and undefined/null values more thoroughly, fixes #615
1 parent ee20b81 commit e3170a1

File tree

12 files changed

+230
-135
lines changed

12 files changed

+230
-135
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# <p align="center"><img src="./pbjs.png" /></p>
1+
# <p align="center"><img alt="protobuf.js" src="./pbjs.png" /></p>
22

33
[![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]
44

55
**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/)).
66

7-
**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.
7+
**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!
88

99
[travis-image]: https://img.shields.io/travis/dcodeIO/protobuf.js.svg
1010
[travis-url]: https://travis-ci.org/dcodeIO/protobuf.js

bench/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var root = protobuf.loadSync(require.resolve("./bench.proto")),
2929
var buf = Test.encode(data).finish();
3030

3131
// warm up
32+
process.stdout.write("warming up ...\n");
3233
var i;
3334
for (i = 0; i < 500000; ++i)
3435
Test.encode(data).finish();

index.d.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// $> pbts --main --global protobuf --out index.d.ts src
2-
// Generated Tue, 03 Jan 2017 23:45:16 UTC
3-
41
export as namespace protobuf;
52

63
/**

src/converter.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ function converter(mtype) {
6464
("d%s=[]", prop);
6565

6666
// non-repeated
67-
} else if (convert = genConvert(field, i, prop)) gen
68-
("d%s=%s", prop, convert);
69-
else gen
67+
} else if (convert = genConvert(field, i, prop)) {
68+
if (field.long || field.resolvedType && !(field.resolvedType instanceof Enum)) gen
69+
("if(s%s!==undefined&&s%s!==null||o.defaults)", prop, prop);
70+
else gen
71+
("if(s%s!==undefined||o.defaults)", prop);
72+
gen
73+
("d%s=%s", prop, convert);
74+
} else gen
7075
("if(d%s===undefined&&o.defaults)", prop)
7176
("d%s=%j", prop, field.typeDefault /* == field.defaultValue */);
7277

src/util.js

-17
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@ util.toArray = function toArray(object) {
3030
}) : [];
3131
};
3232

33-
/**
34-
* Merges the properties of the source object into the destination object.
35-
* @param {Object.<string,*>} dst Destination object
36-
* @param {Object.<string,*>} src Source object
37-
* @param {boolean} [ifNotSet=false] Merges only if the key is not already set
38-
* @returns {Object.<string,*>} Destination object
39-
*/
40-
util.merge = function merge(dst, src, ifNotSet) {
41-
if (src) {
42-
var keys = Object.keys(src);
43-
for (var i = 0; i < keys.length; ++i)
44-
if (dst[keys[i]] === undefined || !ifNotSet)
45-
dst[keys[i]] = src[keys[i]];
46-
}
47-
return dst;
48-
};
49-
5033
/**
5134
* Returns a safe property accessor for the specified properly name.
5235
* @param {string} prop Property name

src/util/runtime.js

+17
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,20 @@ util.arrayNe = function arrayNe(a, b) {
148148
return true;
149149
return false;
150150
};
151+
152+
/**
153+
* Merges the properties of the source object into the destination object.
154+
* @param {Object.<string,*>} dst Destination object
155+
* @param {Object.<string,*>} src Source object
156+
* @param {boolean} [ifNotSet=false] Merges only if the key is not already set
157+
* @returns {Object.<string,*>} Destination object
158+
*/
159+
util.merge = function merge(dst, src, ifNotSet) {
160+
if (src) {
161+
var keys = Object.keys(src);
162+
for (var i = 0; i < keys.length; ++i)
163+
if (dst[keys[i]] === undefined || !ifNotSet)
164+
dst[keys[i]] = src[keys[i]];
165+
}
166+
return dst;
167+
};

tests/data/ambiguous-names.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ $root.A = (function() {
134134
if (!options) {
135135
options = {};
136136
}
137-
var dst = impl.create(src, this, options);
137+
var dst = impl.create(src, this, options) || null;
138138
if (dst) {
139139
if (dst.whatever === undefined && options.defaults) {
140140
dst.whatever = "";
@@ -294,9 +294,11 @@ $root.B = (function() {
294294
if (!options) {
295295
options = {};
296296
}
297-
var dst = impl.create(src, this, options);
297+
var dst = impl.create(src, this, options) || null;
298298
if (dst) {
299-
dst.A = types[0].convert(src.A, impl, options);
299+
if (!(src.A === undefined || src.A === null) || options.defaults) {
300+
dst.A = types[0].convert(src.A, impl, options);
301+
}
300302
}
301303
return dst;
302304
};})($types);

tests/data/extend.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ message A {
99
message C {
1010
message Two {
1111
extend B.One {
12-
Two two = 1000;
12+
C.Two two = 1000;
1313
}
1414
}
1515
}

tests/data/mapbox/vector_tile.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ $root.vector_tile = (function() {
157157
if (!options) {
158158
options = {};
159159
}
160-
var dst = impl.create(src, this, options);
160+
var dst = impl.create(src, this, options) || null;
161161
if (dst) {
162162
if (src.layers && src.layers.length) {
163163
dst.layers = [];
@@ -445,7 +445,7 @@ $root.vector_tile = (function() {
445445
if (!options) {
446446
options = {};
447447
}
448-
var dst = impl.create(src, this, options);
448+
var dst = impl.create(src, this, options) || null;
449449
if (dst) {
450450
if (dst.stringValue === undefined && options.defaults) {
451451
dst.stringValue = "";
@@ -456,9 +456,15 @@ $root.vector_tile = (function() {
456456
if (dst.doubleValue === undefined && options.defaults) {
457457
dst.doubleValue = 0;
458458
}
459-
dst.intValue = impl.longs(src.intValue, 0, 0, false, options);
460-
dst.uintValue = impl.longs(src.uintValue, 0, 0, true, options);
461-
dst.sintValue = impl.longs(src.sintValue, 0, 0, false, options);
459+
if (!(src.intValue === undefined || src.intValue === null) || options.defaults) {
460+
dst.intValue = impl.longs(src.intValue, 0, 0, false, options);
461+
}
462+
if (!(src.uintValue === undefined || src.uintValue === null) || options.defaults) {
463+
dst.uintValue = impl.longs(src.uintValue, 0, 0, true, options);
464+
}
465+
if (!(src.sintValue === undefined || src.sintValue === null) || options.defaults) {
466+
dst.sintValue = impl.longs(src.sintValue, 0, 0, false, options);
467+
}
462468
if (dst.boolValue === undefined && options.defaults) {
463469
dst.boolValue = false;
464470
}
@@ -715,9 +721,11 @@ $root.vector_tile = (function() {
715721
if (!options) {
716722
options = {};
717723
}
718-
var dst = impl.create(src, this, options);
724+
var dst = impl.create(src, this, options) || null;
719725
if (dst) {
720-
dst.id = impl.longs(src.id, 0, 0, true, options);
726+
if (!(src.id === undefined || src.id === null) || options.defaults) {
727+
dst.id = impl.longs(src.id, 0, 0, true, options);
728+
}
721729
if (src.tags && src.tags.length) {
722730
dst.tags = [];
723731
for (var i = 0; i < src.tags.length; ++i) {
@@ -728,7 +736,9 @@ $root.vector_tile = (function() {
728736
dst.tags = [];
729737
}
730738
}
731-
dst.type = impl.enums(src.type, undefined, types[2], options);
739+
if (!(src.type === undefined || src.type === null) || options.defaults) {
740+
dst.type = impl.enums(src.type, undefined, types[2], options);
741+
}
732742
if (src.geometry && src.geometry.length) {
733743
dst.geometry = [];
734744
for (var i = 0; i < src.geometry.length; ++i) {
@@ -1007,7 +1017,7 @@ $root.vector_tile = (function() {
10071017
if (!options) {
10081018
options = {};
10091019
}
1010-
var dst = impl.create(src, this, options);
1020+
var dst = impl.create(src, this, options) || null;
10111021
if (dst) {
10121022
if (dst.version === undefined && options.defaults) {
10131023
dst.version = 1;

tests/data/package.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ $root.Package = (function() {
503503
if (!options) {
504504
options = {};
505505
}
506-
var dst = impl.create(src, this, options);
506+
var dst = impl.create(src, this, options) || null;
507507
if (dst) {
508508
if (dst.name === undefined && options.defaults) {
509509
dst.name = "";
@@ -520,7 +520,9 @@ $root.Package = (function() {
520520
if (dst.license === undefined && options.defaults) {
521521
dst.license = "";
522522
}
523-
dst.repository = types[5].convert(src.repository, impl, options);
523+
if (!(src.repository === undefined || src.repository === null) || options.defaults) {
524+
dst.repository = types[5].convert(src.repository, impl, options);
525+
}
524526
if (dst.bugs === undefined && options.defaults) {
525527
dst.bugs = "";
526528
}
@@ -724,7 +726,7 @@ $root.Package = (function() {
724726
if (!options) {
725727
options = {};
726728
}
727-
var dst = impl.create(src, this, options);
729+
var dst = impl.create(src, this, options) || null;
728730
if (dst) {
729731
if (dst.type === undefined && options.defaults) {
730732
dst.type = "";

tests/data/rpc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ $root.MyRequest = (function() {
226226
if (!options) {
227227
options = {};
228228
}
229-
var dst = impl.create(src, this, options);
229+
var dst = impl.create(src, this, options) || null;
230230
if (dst) {
231231
if (dst.path === undefined && options.defaults) {
232232
dst.path = "";
@@ -382,7 +382,7 @@ $root.MyResponse = (function() {
382382
if (!options) {
383383
options = {};
384384
}
385-
var dst = impl.create(src, this, options);
385+
var dst = impl.create(src, this, options) || null;
386386
if (dst) {
387387
if (dst.status === undefined && options.defaults) {
388388
dst.status = 0;

0 commit comments

Comments
 (0)