Skip to content

Commit

Permalink
Added ParseOptions to protobuf.parse and --keep-case for .proto sourc…
Browse files Browse the repository at this point in the history
…es to pbjs, see #564
  • Loading branch information
dcodeIO committed Dec 16, 2016
1 parent 4531d75 commit 1a6fdc9
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 46 deletions.
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
This license applies to all parts of protobuf.js except those files including
or referencing a different license.

Apache License
Version 2.0, January 2004
Expand Down
25 changes: 20 additions & 5 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ exports.main = function(args, callback) {
root : "r"
},
string: [ "target", "out", "path", "wrap", "root" ],
boolean: [ "keep-case", "encode", "decode", "verify", "delimited" ],
default: {
target: "json"
target: "json",
encode: true,
decode: true,
verify: true,
delimited: true
}
});

Expand Down Expand Up @@ -55,15 +60,21 @@ exports.main = function(args, callback) {
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" -w, --wrap Specifies the wrapper to use for *-module targets. Also accepts a path.",
" Module targets only:",
"",
" -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
"",
" default Default wrapper supporting both CommonJS and AMD",
" commonjs CommonJS only wrapper",
" amd AMD only wrapper",
"",
" -r, --root Specifies an alternative protobuf.roots name for *-module targets.",
" -r, --root Specifies an alternative protobuf.roots name.",
"",
" Proto sources only:",
"",
" --keep-case Keeps field casing instead of converting to camel case (not recommended).",
"",
" Static code generation only:",
" Static targets only:",
"",
" --no-encode Does not generate encode functions.",
" --no-decode Does not generate decode functions.",
Expand Down Expand Up @@ -104,7 +115,11 @@ exports.main = function(args, callback) {
return filepath;
};

root.load(files, function(err) {
var parseOptions = {
"keepCase": argv["keep-case"] || false
};

root.load(files, parseOptions, function(err) {
if (err) {
if (callback)
return callback(err);
Expand Down
10 changes: 5 additions & 5 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function buildType(ref, type) {
push("});");
});

if (config.encode !== false) {
if (config.encode) {

// #encode
push("");
Expand All @@ -287,7 +287,7 @@ function buildType(ref, type) {
util : "$protobuf.util"
});

if (config.delimited !== false) {
if (config.delimited) {

// #encodeDelimited
push("");
Expand All @@ -307,7 +307,7 @@ function buildType(ref, type) {

}

if (config.decode !== false) {
if (config.decode) {

// #decode
push("");
Expand All @@ -323,7 +323,7 @@ function buildType(ref, type) {
util : "$protobuf.util"
});

if (config.delimited !== false) {
if (config.delimited) {

// #decodeDelimited
push("");
Expand All @@ -342,7 +342,7 @@ function buildType(ref, type) {
}
}

if (config.verify !== false) {
if (config.verify) {

// #verify
push("");
Expand Down
52 changes: 40 additions & 12 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
27 changes: 20 additions & 7 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,28 @@ function lower(token) {
* @property {string|undefined} syntax Syntax, if specified (either `"proto2"` or `"proto3"`)
* @property {Root} root Populated root instance
*/
/**/

/**
* Options modifying the the behavior of {@link parse}.
* @typedef ParseOptions
* @type {Object}
* @property {boolean} [keepCase=false] Keeps field casing instead of converting to camel case
*/

/**
* Parses the given .proto source and returns an object with the parsed contents.
* @param {string} source Source contents
* @param {Root} [root] Root to populate
* @param {Root|ParseOptions} [root] Root to populate
* @param {ParseOptions} [options] Parse options
* @returns {ParserResult} Parser result
*/
function parse(source, root) {
function parse(source, root, options) {
/* eslint-disable callback-return */
if (!root)
if (!(root instanceof Root)) {
root = new Root();
options = root || {};
} else if (!options)
options = {};

var tn = tokenize(source),
next = tn.next,
Expand Down Expand Up @@ -267,7 +277,8 @@ function parse(source, root) {
var name = next();
if (!nameRe.test(name))
throw illegal(name, "name");
name = camelCase(name);
if (!options.keepCase)
name = camelCase(name);
skip("=");
var id = parseId(next());
var field = parseInlineOptions(new Field(name, id, type, rule, extend));
Expand All @@ -289,7 +300,8 @@ function parse(source, root) {
var name = next();
if (!nameRe.test(name))
throw illegal(name, "name");
name = camelCase(name);
if (!options.keepCase)
name = camelCase(name);
skip("=");
var id = parseId(next());
var field = parseInlineOptions(new MapField(name, id, keyType, valueType));
Expand All @@ -300,7 +312,8 @@ function parse(source, root) {
var name = next();
if (!nameRe.test(name))
throw illegal(name, "name");
name = camelCase(name);
if (!options.keepCase)
name = camelCase(name);
var oneof = new OneOf(name);
if (skip("{", true)) {
while ((token = next()) !== "}") {
Expand Down
23 changes: 19 additions & 4 deletions src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ function SYNC() {} // eslint-disable-line no-empty-function
/**
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
* @param {string|string[]} filename Names of one or multiple files to load
* @param {ParseOptions} options Parse options
* @param {LoadCallback} callback Callback function
* @returns {undefined}
*/
RootPrototype.load = function load(filename, callback) {
RootPrototype.load = function load(filename, options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
var self = this;
if (!callback)
return util.asPromise(load, self, filename);
Expand All @@ -89,7 +94,7 @@ RootPrototype.load = function load(filename, callback) {
if (!util.isString(source))
self.setOptions(source.options).addJSON(source.nested);
else {
var parsed = require("./parse")(source, self);
var parsed = require("./parse")(source, self, options);
if (parsed.imports)
parsed.imports.forEach(function(name) {
fetch(self.resolvePath(filename, name));
Expand Down Expand Up @@ -179,17 +184,27 @@ RootPrototype.load = function load(filename, callback) {
finish(null, self);
return undefined;
};
// function load(filename:string, options:ParseOptions, callback:LoadCallback):undefined

/**
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
* @param {string|string[]} filename Names of one or multiple files to load
* @param {LoadCallback} callback Callback function
* @returns {undefined}
* @variation 2
*/
// function load(filename:string, callback:LoadCallback):undefined

/**
* Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
* @name Root#load
* @function
* @param {string|string[]} filename Names of one or multiple files to load
* @param {ParseOptions} [options] Parse options
* @returns {Promise<Root>} Promise
* @variation 2
* @variation 3
*/
// function load(filename:string):Promise<Root>
// function load(filename:string, [options:ParseOptions]):Promise<Root>

/**
* Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace.
Expand Down
Loading

0 comments on commit 1a6fdc9

Please sign in to comment.