Permalink
Browse files

New: Instead of protobuf.parse.keepCase, fall back to protobuf.parse.…

…defaults holding all possible defaults, see #608
  • Loading branch information...
dcodeIO committed Jan 3, 2017
1 parent 804739d commit bcadffecb3a8b98fbbd34b45bae0e6af58f9c810
Showing with 28 additions and 15 deletions.
  1. +2 −3 README.md
  2. +1 −1 index.d.ts
  3. +2 −2 lib/tape-adapter.js
  4. +11 −2 src/converter.js
  5. +4 −0 src/index.js
  6. +6 −5 src/parse.js
  7. +2 −2 src/root.js
@@ -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
------------
@@ -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;
@@ -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
@@ -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;
@@ -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)
@@ -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
("}");
@@ -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") {
@@ -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
@@ -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>
@@ -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)
@@ -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"),
@@ -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 */
@@ -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,
@@ -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
*/
@@ -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
*/
@@ -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
*/

0 comments on commit bcadffe

Please sign in to comment.