Permalink
Browse files

Added ParseOptions to protobuf.parse and --keep-case for .proto sourc…

…es to pbjs, see #564
  • Loading branch information...
dcodeIO committed Dec 16, 2016
1 parent 4531d75 commit 1a6fdc9a11fb08506d09351f8e853384c2b8be25
@@ -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
@@ -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
}
});
@@ -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.",
@@ -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);
@@ -271,7 +271,7 @@ function buildType(ref, type) {
push("});");
});
if (config.encode !== false) {
if (config.encode) {
// #encode
push("");
@@ -287,7 +287,7 @@ function buildType(ref, type) {
util : "$protobuf.util"
});
if (config.delimited !== false) {
if (config.delimited) {
// #encodeDelimited
push("");
@@ -307,7 +307,7 @@ function buildType(ref, type) {
}
if (config.decode !== false) {
if (config.decode) {
// #decode
push("");
@@ -323,7 +323,7 @@ function buildType(ref, type) {
util : "$protobuf.util"
});
if (config.delimited !== false) {
if (config.delimited) {
// #decodeDelimited
push("");
@@ -342,7 +342,7 @@ function buildType(ref, type) {
}
}
if (config.verify !== false) {
if (config.verify) {
// #verify
push("");

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +45 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +1 Byte (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.
@@ -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,
@@ -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));
@@ -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));
@@ -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()) !== "}") {
@@ -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);
@@ -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));
@@ -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.
Oops, something went wrong.

0 comments on commit 1a6fdc9

Please sign in to comment.