Skip to content

Commit

Permalink
CLI: Added --strict-message option to pbjs to strictly reference mess…
Browse files Browse the repository at this point in the history
…age instances instead of $Properties, see #741
  • Loading branch information
dcodeIO committed Apr 3, 2017
1 parent 7a0b5b1 commit c812cef
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
82 changes: 42 additions & 40 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ exports.main = function main(args, callback) {
lint : "l"
},
string: [ "target", "out", "path", "wrap", "root", "lint" ],
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long" ],
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long", "strict-message" ],
default: {
target : "json",
create : true,
encode : true,
decode : true,
verify : true,
convert : true,
delimited : true,
beautify : true,
comments : true,
es6 : null,
lint : lintDefault,
"keep-case" : false,
"strict-long": false
target : "json",
create : true,
encode : true,
decode : true,
verify : true,
convert : true,
delimited : true,
beautify : true,
comments : true,
es6 : null,
lint : lintDefault,
"keep-case" : false,
"strict-long" : false,
"strict-message": false
}
});

Expand All @@ -68,48 +69,49 @@ exports.main = function main(args, callback) {
"",
chalk.bold.white("Translates between file formats and generates static code."),
"",
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
"",
descs.join("\n"),
"",
" -p, --path Adds a directory to the include path.",
" -p, --path Adds a directory to the include path.",
"",
" -o, --out Saves to a file instead of writing to stdout.",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" --sparse Exports only those types referenced from a main file (experimental).",
" --sparse Exports only those types referenced from a main file (experimental).",
"",
chalk.bold.gray(" Module targets only:"),
chalk.bold.gray(" Module targets only:"),
"",
" -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
" -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 wrapper",
" amd AMD wrapper",
" es6 ES6 wrapper (implies --es6)",
" default Default wrapper supporting both CommonJS and AMD",
" commonjs CommonJS wrapper",
" amd AMD wrapper",
" es6 ES6 wrapper (implies --es6)",
"",
" -r, --root Specifies an alternative protobuf.roots name.",
" -r, --root Specifies an alternative protobuf.roots name.",
"",
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
"",
" " + lintDefault,
" " + lintDefault,
"",
" --es6 Enables ES6 syntax (const/let instead of var)",
" --es6 Enables ES6 syntax (const/let instead of var)",
"",
chalk.bold.gray(" Proto sources only:"),
chalk.bold.gray(" Proto sources only:"),
"",
" --keep-case Keeps field casing instead of converting to camel case.",
" --keep-case Keeps field casing instead of converting to camel case.",
"",
chalk.bold.gray(" Static targets only:"),
chalk.bold.gray(" Static targets only:"),
"",
" --no-create Does not generate create functions used for reflection compatibility.",
" --no-encode Does not generate encode functions.",
" --no-decode Does not generate decode functions.",
" --no-verify Does not generate verify functions.",
" --no-convert Does not generate convert functions like from/toObject",
" --no-delimited Does not generate delimited encode/decode functions.",
" --no-beautify Does not beautify generated code.",
" --no-comments Does not output any JSDoc comments.",
" --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).",
" --no-create Does not generate create functions used for reflection compatibility.",
" --no-encode Does not generate encode functions.",
" --no-decode Does not generate decode functions.",
" --no-verify Does not generate verify functions.",
" --no-convert Does not generate convert functions like from/toObject",
" --no-delimited Does not generate delimited encode/decode functions.",
" --no-beautify Does not beautify generated code.",
" --no-comments Does not output any JSDoc comments.",
" --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).",
" --strict-message Forces message types to actual instances (no plain objects).",
"",
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
""
Expand Down
20 changes: 13 additions & 7 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function toJsType(field) {
if (field.resolve().resolvedType instanceof Enum)
type = field.resolvedType.fullName.substring(1); // reference the enum
else if (field.resolvedType instanceof Type)
type = field.resolvedType.fullName.substring(1) + "$Properties"; // reference the interface
type = messageRef(field.resolvedType.fullName.substring(1)); // reference the interface
else
type = "*"; // should not happen
break;
Expand All @@ -338,13 +338,19 @@ function toJsType(field) {
: type;
}

function messageRef(fullName) {
return config["strict-message"] || !config.comments
? fullName
: fullName + "$Properties";
}

function buildType(ref, type) {
var fullName = type.fullName.substring(1);

if (config.comments) {
if (config.comments && !config["strict-message"]) {
var typeDef = [
"Properties of " + aOrAn(type.name) + ".",
"@typedef " + fullName + "$Properties",
"@typedef " + messageRef(fullName),
"@type {Object}"
];
type.fieldsArray.forEach(function(field) {
Expand All @@ -363,7 +369,7 @@ function buildType(ref, type) {
type.comment ? "@classdesc " + type.comment : null,
"@exports " + fullName,
"@constructor",
"@param {" + fullName + "$Properties=} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
"@param {" + messageRef(fullName) + "=} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
]);
buildFunction(type, type.name, Class.generate(type));

Expand Down Expand Up @@ -427,7 +433,7 @@ function buildType(ref, type) {
push("");
pushComment([
"Creates a new " + type.name + " instance using the specified properties.",
"@param {" + fullName + "$Properties=} [properties] Properties to set",
"@param {" + messageRef(fullName) + "=} [properties] Properties to set",
"@returns {" + fullName + "} " + type.name + " instance"
]);
push(name(type.name) + ".create = function create(properties) {");
Expand All @@ -441,7 +447,7 @@ function buildType(ref, type) {
push("");
pushComment([
"Encodes the specified " + type.name + " message. Does not implicitly {@link " + fullName + ".verify|verify} messages.",
"@param {" + fullName + "$Properties} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode",
"@param {" + messageRef(fullName) + "} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode",
"@param {$protobuf.Writer} [" + (config.beautify ? "writer" : "w") + "] Writer to encode to",
"@returns {$protobuf.Writer} Writer"
]);
Expand All @@ -451,7 +457,7 @@ function buildType(ref, type) {
push("");
pushComment([
"Encodes the specified " + type.name + " message, length delimited. Does not implicitly {@link " + fullName + ".verify|verify} messages.",
"@param {" + fullName + "$Properties} message " + type.name + " message or plain object to encode",
"@param {" + messageRef(fullName) + "} message " + type.name + " message or plain object to encode",
"@param {$protobuf.Writer} [writer] Writer to encode to",
"@returns {$protobuf.Writer} Writer"
]);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.7.0",
"version": "6.7.1",
"versionScheme": "~",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
Expand Down Expand Up @@ -90,7 +90,7 @@
"tmp": "0.0.31",
"tslint": "^5.0.0",
"typescript": "^2.2.2",
"uglify-js": "^2.8.20",
"uglify-js": "^2.8.21",
"vinyl-buffer": "^1.0.0",
"vinyl-fs": "^2.4.4",
"vinyl-source-stream": "^1.1.0"
Expand Down

0 comments on commit c812cef

Please sign in to comment.