Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/dcodeIO/protobuf.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jun 9, 2017
2 parents 114f7ea + 5f2f177 commit 3a86c17
Show file tree
Hide file tree
Showing 23 changed files with 2,913 additions and 2,889 deletions.
41 changes: 28 additions & 13 deletions cli/lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,20 +574,35 @@ function handleMember(element, parent) {
begin(element);

if (element.isEnum) {

writeln("enum ", element.name, " {");
++indent;
element.properties.forEach(function(property, i) {
write(property.name);
if (property.defaultvalue !== undefined)
write(" = ", JSON.stringify(property.defaultvalue));
if (i < element.properties.length - 1)
writeln(",");
else
writeln();
var stringEnum = false;
element.properties.forEach(function(property) {
if (isNaN(property.defaultvalue)) {
stringEnum = true;
}
});
--indent;
writeln("}");
if (stringEnum) {
writeln("type ", element.name, " =");
++indent;
element.properties.forEach(function(property, i) {
write(i === 0 ? "" : "| ", JSON.stringify(property.defaultvalue));
});
--indent;
writeln(";");
} else {
writeln("enum ", element.name, " {");
++indent;
element.properties.forEach(function(property, i) {
write(property.name);
if (property.defaultvalue !== undefined)
write(" = ", JSON.stringify(property.defaultvalue));
if (i < element.properties.length - 1)
writeln(",");
else
writeln();
});
--indent;
writeln("}");
}

} else {

Expand Down
9 changes: 7 additions & 2 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ exports.main = function main(args, callback) {
"force-long": "strict-long",
"force-message": "strict-message"
},
string: [ "target", "out", "path", "wrap", "root", "lint" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-message" ],
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
default: {
target: "json",
create: true,
Expand All @@ -49,6 +49,8 @@ exports.main = function main(args, callback) {
lint: lintDefault,
"keep-case": false,
"force-long": false,
"force-number": false,
"force-enum-string": false,
"force-message": false
}
});
Expand Down Expand Up @@ -99,6 +101,8 @@ exports.main = function main(args, callback) {
" es6 ES6 wrapper (implies --es6)",
" closure A closure adding to protobuf.roots where protobuf is a global",
"",
" --dependency Specifies which version of protobuf to require. Accepts any valid module id",
"",
" -r, --root Specifies an alternative protobuf.roots name.",
"",
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
Expand All @@ -123,6 +127,7 @@ exports.main = function main(args, callback) {
" --no-comments Does not output any JSDoc comments.",
"",
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
" --force-message Enfores the use of message instances instead of plain objects.",
"",
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
Expand Down
4 changes: 3 additions & 1 deletion cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module.exports = json_module;

var util = require("../util");

var protobuf = require("../..");

json_module.description = "JSON representation as a module";

function json_module(root, options, callback) {
Expand All @@ -17,7 +19,7 @@ function json_module(root, options, callback) {
}
var json = util.jsonSafeProp(JSON.stringify(root.nested, null, 2).trim());
output.push(".addJSON(" + json + ");");
output = util.wrap(output.join(""), options);
output = util.wrap(output.join(""), protobuf.util.merge({ dependency: "protobufjs/minimal" }, options));
process.nextTick(function() {
callback(null, output);
});
Expand Down
20 changes: 11 additions & 9 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ function buildFunction(type, functionName, gen, scope) {

function toJsType(field) {
var type;

switch (field.type) {
case "double":
case "float":
Expand All @@ -328,7 +329,7 @@ function toJsType(field) {
case "sint64":
case "fixed64":
case "sfixed64":
type = config.forceLong ? "Long" : "number|Long";
type = config.forceLong ? "Long" : config.forceNumber ? "number" : "number|Long";
break;
case "bool":
type = "boolean";
Expand Down Expand Up @@ -505,7 +506,7 @@ function buildType(ref, type) {
++indent;
push("if (!(reader instanceof $Reader))");
++indent;
push("reader = $Reader(reader);");
push("reader = new $Reader(reader);");
--indent;
push("return this.decode(reader, reader.uint32());");
--indent;
Expand Down Expand Up @@ -636,24 +637,25 @@ function buildEnum(ref, enm) {
var comment = [
enm.comment || enm.name + " enum.",
enm.parent instanceof protobuf.Root ? "@exports " + escapeName(enm.name) : undefined,
"@enum {number}",
config.forceEnumString ? "@enum {number}" : "@enum {string}",
];
Object.keys(enm.values).forEach(function(key) {
var val = enm.values[key];
comment.push("@property {number} " + key + "=" + val + " " + (enm.comments[key] || key + " value"));
var val = config.forceEnumString ? key : enm.values[key];
comment.push((config.forceEnumString ? "@property {string} " : "@property {number} ") + key + "=" + val + " " + (enm.comments[key] || key + " value"));
});
pushComment(comment);
push(escapeName(ref) + "." + escapeName(enm.name) + " = (function() {");
++indent;
push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
var aliased = [];
Object.keys(enm.values).forEach(function(key) {
var val = enm.values[key];
if (aliased.indexOf(val) > -1)
var valueId = enm.values[key];
var val = config.forceEnumString ? JSON.stringify(key) : valueId;
if (aliased.indexOf(valueId) > -1)
push("values[" + JSON.stringify(key) + "] = " + val + ";");
else {
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");
aliased.push(val);
push("values[valuesById[" + valueId + "] = " + JSON.stringify(key) + "] = " + val + ";");
aliased.push(valueId);
}
});
push("return values;");
Expand Down
2 changes: 1 addition & 1 deletion cli/wrappers/amd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["protobuf"], function($protobuf) {
define([$DEPENDENCY], function($protobuf) {
"use strict";

$OUTPUT;
Expand Down
2 changes: 1 addition & 1 deletion cli/wrappers/default.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(global, factory) { /* global define, require, module */

/* AMD */ if (typeof define === 'function' && define.amd)
define(["protobuf"], factory);
define([$DEPENDENCY], factory);

/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
module.exports = factory(require($DEPENDENCY));
Expand Down
2 changes: 1 addition & 1 deletion cli/wrappers/es6.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as $protobuf from "protobufjs";
import * as $protobuf from $DEPENDENCY;

$OUTPUT;

Expand Down
Loading

0 comments on commit 3a86c17

Please sign in to comment.