Skip to content

Commit

Permalink
New: Instead of protobuf.parse.keepCase, fall back to protobuf.parse.…
Browse files Browse the repository at this point in the history
…defaults holding all possible defaults, see #608
  • Loading branch information
dcodeIO committed Jan 3, 2017
1 parent 804739d commit bcadffe
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,9 @@ Documentation
-------------

* [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)

* [protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)

* [Questions and answers on StackOverflow](http://stackoverflow.com/questions/tagged/protobuf.js)
* [Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki
* [More questions and answers](http://stackoverflow.com/questions/tagged/protobuf.js) on StackOverflow

Command line
------------
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// $> pbts --main --global protobuf --out index.d.ts src
// Generated Mon, 02 Jan 2017 15:34:42 UTC
// Generated Tue, 03 Jan 2017 02:10:19 UTC

export as namespace protobuf;

Expand Down
4 changes: 2 additions & 2 deletions lib/tape-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Test.prototype.deepEqual
= Test.prototype.same
= function (a, b, msg, extra) {
this._assert(deepEqual(a, b, { strict: true }), {
message : msg || 'should be equivalent',
operator : 'deepEqual',
message : msg || "should be equivalent",
operator : "deepEqual",
actual : a,
expected : b,
extra : extra
Expand Down
13 changes: 11 additions & 2 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ var sprintf = util.codegen.sprintf;
function genConvert(field, fieldIndex, prop) {
if (field.resolvedType)
return field.resolvedType instanceof Enum
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex)
: sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop);
// enums
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex)
// recurse into messages
: sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop);
switch (field.type) {
case "int64":
case "uint64":
case "sint64":
case "fixed64":
case "sfixed64":
// longs
return sprintf("f.longs(s%s,%d,%d,%j,o)", prop, 0, 0, field.type.charAt(0) === "u");
case "bytes":
// bytes
return sprintf("f.bytes(s%s,%j,o)", prop, Array.prototype.slice.call(field.defaultValue));
}
return null;
Expand All @@ -45,6 +49,8 @@ function converter(mtype) {
var convert;
fields.forEach(function(field, i) {
var prop = util.safeProp(field.resolve().name);

// repeated
if (field.repeated) { gen
("if(s%s&&s%s.length){", prop, prop)
("d%s=[]", prop)
Expand All @@ -56,11 +62,14 @@ function converter(mtype) {
gen
("}else if(o.defaults||o.arrays)")
("d%s=[]", prop);

// non-repeated
} else if (convert = genConvert(field, i, prop)) gen
("d%s=%s", prop, convert);
else gen
("if(d%s===undefined&&o.defaults)", prop)
("d%s=%j", prop, field.defaultValue);

});
gen
("}");
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var protobuf = global.protobuf = exports;
* @param {Root} root Root namespace, defaults to create a new one if omitted.
* @param {LoadCallback} callback Callback function
* @returns {undefined}
* @see {@link Root#load}
*/
function load(filename, root, callback) {
if (typeof root === "function") {
Expand All @@ -34,6 +35,7 @@ function load(filename, root, callback) {
* @param {string|string[]} filename One or multiple files to load
* @param {LoadCallback} callback Callback function
* @returns {undefined}
* @see {@link Root#load}
* @variation 2
*/
// function load(filename:string, callback:LoadCallback):undefined
Expand All @@ -45,6 +47,7 @@ function load(filename, root, callback) {
* @param {string|string[]} filename One or multiple files to load
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
* @returns {Promise<Root>} Promise
* @see {@link Root#load}
* @variation 3
*/
// function load(filename:string, [root:Root]):Promise<Root>
Expand All @@ -57,6 +60,7 @@ protobuf.load = load;
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
* @returns {Root} Root namespace
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
* @see {@link Root#loadSync}
*/
function loadSync(filename, root) {
if (!root)
Expand Down
11 changes: 6 additions & 5 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";
module.exports = parse;

parse.keepCase = false;
parse.filename = null;
parse.defaults = { keepCase: false };

var tokenize = require("./tokenize"),
Root = require("./root"),
Expand Down Expand Up @@ -60,10 +61,10 @@ function camelCase(str) {
* @function
* @param {string} source Source contents
* @param {Root} root Root to populate
* @param {ParseOptions} [options] Parse options
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {ParserResult} Parser result
* @property {string} filename=null Currently processing file name for error reporting, if known
* @property {boolean} keepCase=false When set to `true`, always keeps field casing instead of converting to camel case when no {@link ParseOptions} are specified
* @property {ParseOptions} defaults Default {@link ParseOptions}
*/
function parse(source, root, options) {
/* eslint-disable callback-return */
Expand All @@ -72,7 +73,7 @@ function parse(source, root, options) {
root = new Root();
}
if (!options)
options = { keepCase: parse.keepCase };
options = parse.defaults;

var tn = tokenize(source),
next = tn.next,
Expand Down Expand Up @@ -664,7 +665,7 @@ function parse(source, root, options) {
* @name parse
* @function
* @param {string} source Source contents
* @param {ParseOptions} [options] Parse options
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {ParserResult} Parser result
* @variation 2
*/
4 changes: 2 additions & 2 deletions src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ RootPrototype.load = function load(filename, options, callback) {
* @name Root#load
* @function
* @param {string|string[]} filename Names of one or multiple files to load
* @param {ParseOptions} [options] Parse options
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {Promise<Root>} Promise
* @variation 3
*/
Expand All @@ -224,7 +224,7 @@ RootPrototype.load = function load(filename, options, callback) {
/**
* Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace.
* @param {string|string[]} filename Names of one or multiple files to load
* @param {ParseOptions} [options] Parse options
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {Root} Root namespace
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
*/
Expand Down

0 comments on commit bcadffe

Please sign in to comment.