Skip to content

Commit bcadffe

Browse files
committed
New: Instead of protobuf.parse.keepCase, fall back to protobuf.parse.defaults holding all possible defaults, see #608
1 parent 804739d commit bcadffe

7 files changed

Lines changed: 28 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,9 @@ Documentation
315315
-------------
316316

317317
* [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)
318-
319318
* [protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
320-
321-
* [Questions and answers on StackOverflow](http://stackoverflow.com/questions/tagged/protobuf.js)
319+
* [Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki
320+
* [More questions and answers](http://stackoverflow.com/questions/tagged/protobuf.js) on StackOverflow
322321

323322
Command line
324323
------------

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// $> pbts --main --global protobuf --out index.d.ts src
2-
// Generated Mon, 02 Jan 2017 15:34:42 UTC
2+
// Generated Tue, 03 Jan 2017 02:10:19 UTC
33

44
export as namespace protobuf;
55

lib/tape-adapter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Test.prototype.deepEqual
1313
= Test.prototype.same
1414
= function (a, b, msg, extra) {
1515
this._assert(deepEqual(a, b, { strict: true }), {
16-
message : msg || 'should be equivalent',
17-
operator : 'deepEqual',
16+
message : msg || "should be equivalent",
17+
operator : "deepEqual",
1818
actual : a,
1919
expected : b,
2020
extra : extra

src/converter.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ var sprintf = util.codegen.sprintf;
1010
function genConvert(field, fieldIndex, prop) {
1111
if (field.resolvedType)
1212
return field.resolvedType instanceof Enum
13-
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex)
14-
: sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop);
13+
// enums
14+
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex)
15+
// recurse into messages
16+
: sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop);
1517
switch (field.type) {
1618
case "int64":
1719
case "uint64":
1820
case "sint64":
1921
case "fixed64":
2022
case "sfixed64":
23+
// longs
2124
return sprintf("f.longs(s%s,%d,%d,%j,o)", prop, 0, 0, field.type.charAt(0) === "u");
2225
case "bytes":
26+
// bytes
2327
return sprintf("f.bytes(s%s,%j,o)", prop, Array.prototype.slice.call(field.defaultValue));
2428
}
2529
return null;
@@ -45,6 +49,8 @@ function converter(mtype) {
4549
var convert;
4650
fields.forEach(function(field, i) {
4751
var prop = util.safeProp(field.resolve().name);
52+
53+
// repeated
4854
if (field.repeated) { gen
4955
("if(s%s&&s%s.length){", prop, prop)
5056
("d%s=[]", prop)
@@ -56,11 +62,14 @@ function converter(mtype) {
5662
gen
5763
("}else if(o.defaults||o.arrays)")
5864
("d%s=[]", prop);
65+
66+
// non-repeated
5967
} else if (convert = genConvert(field, i, prop)) gen
6068
("d%s=%s", prop, convert);
6169
else gen
6270
("if(d%s===undefined&&o.defaults)", prop)
6371
("d%s=%j", prop, field.defaultValue);
72+
6473
});
6574
gen
6675
("}");

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var protobuf = global.protobuf = exports;
1616
* @param {Root} root Root namespace, defaults to create a new one if omitted.
1717
* @param {LoadCallback} callback Callback function
1818
* @returns {undefined}
19+
* @see {@link Root#load}
1920
*/
2021
function load(filename, root, callback) {
2122
if (typeof root === "function") {
@@ -34,6 +35,7 @@ function load(filename, root, callback) {
3435
* @param {string|string[]} filename One or multiple files to load
3536
* @param {LoadCallback} callback Callback function
3637
* @returns {undefined}
38+
* @see {@link Root#load}
3739
* @variation 2
3840
*/
3941
// function load(filename:string, callback:LoadCallback):undefined
@@ -45,6 +47,7 @@ function load(filename, root, callback) {
4547
* @param {string|string[]} filename One or multiple files to load
4648
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
4749
* @returns {Promise<Root>} Promise
50+
* @see {@link Root#load}
4851
* @variation 3
4952
*/
5053
// function load(filename:string, [root:Root]):Promise<Root>
@@ -57,6 +60,7 @@ protobuf.load = load;
5760
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
5861
* @returns {Root} Root namespace
5962
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
63+
* @see {@link Root#loadSync}
6064
*/
6165
function loadSync(filename, root) {
6266
if (!root)

src/parse.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22
module.exports = parse;
33

4-
parse.keepCase = false;
4+
parse.filename = null;
5+
parse.defaults = { keepCase: false };
56

67
var tokenize = require("./tokenize"),
78
Root = require("./root"),
@@ -60,10 +61,10 @@ function camelCase(str) {
6061
* @function
6162
* @param {string} source Source contents
6263
* @param {Root} root Root to populate
63-
* @param {ParseOptions} [options] Parse options
64+
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
6465
* @returns {ParserResult} Parser result
6566
* @property {string} filename=null Currently processing file name for error reporting, if known
66-
* @property {boolean} keepCase=false When set to `true`, always keeps field casing instead of converting to camel case when no {@link ParseOptions} are specified
67+
* @property {ParseOptions} defaults Default {@link ParseOptions}
6768
*/
6869
function parse(source, root, options) {
6970
/* eslint-disable callback-return */
@@ -72,7 +73,7 @@ function parse(source, root, options) {
7273
root = new Root();
7374
}
7475
if (!options)
75-
options = { keepCase: parse.keepCase };
76+
options = parse.defaults;
7677

7778
var tn = tokenize(source),
7879
next = tn.next,
@@ -664,7 +665,7 @@ function parse(source, root, options) {
664665
* @name parse
665666
* @function
666667
* @param {string} source Source contents
667-
* @param {ParseOptions} [options] Parse options
668+
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
668669
* @returns {ParserResult} Parser result
669670
* @variation 2
670671
*/

src/root.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ RootPrototype.load = function load(filename, options, callback) {
215215
* @name Root#load
216216
* @function
217217
* @param {string|string[]} filename Names of one or multiple files to load
218-
* @param {ParseOptions} [options] Parse options
218+
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
219219
* @returns {Promise<Root>} Promise
220220
* @variation 3
221221
*/
@@ -224,7 +224,7 @@ RootPrototype.load = function load(filename, options, callback) {
224224
/**
225225
* Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace.
226226
* @param {string|string[]} filename Names of one or multiple files to load
227-
* @param {ParseOptions} [options] Parse options
227+
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
228228
* @returns {Root} Root namespace
229229
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
230230
*/

0 commit comments

Comments
 (0)