Skip to content

Commit 3a86c17

Browse files
committed
Merge branch 'master' of https://github.com/dcodeIO/protobuf.js
2 parents 114f7ea + 5f2f177 commit 3a86c17

23 files changed

+2913
-2889
lines changed

cli/lib/tsd-jsdoc/publish.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,35 @@ function handleMember(element, parent) {
574574
begin(element);
575575

576576
if (element.isEnum) {
577-
578-
writeln("enum ", element.name, " {");
579-
++indent;
580-
element.properties.forEach(function(property, i) {
581-
write(property.name);
582-
if (property.defaultvalue !== undefined)
583-
write(" = ", JSON.stringify(property.defaultvalue));
584-
if (i < element.properties.length - 1)
585-
writeln(",");
586-
else
587-
writeln();
577+
var stringEnum = false;
578+
element.properties.forEach(function(property) {
579+
if (isNaN(property.defaultvalue)) {
580+
stringEnum = true;
581+
}
588582
});
589-
--indent;
590-
writeln("}");
583+
if (stringEnum) {
584+
writeln("type ", element.name, " =");
585+
++indent;
586+
element.properties.forEach(function(property, i) {
587+
write(i === 0 ? "" : "| ", JSON.stringify(property.defaultvalue));
588+
});
589+
--indent;
590+
writeln(";");
591+
} else {
592+
writeln("enum ", element.name, " {");
593+
++indent;
594+
element.properties.forEach(function(property, i) {
595+
write(property.name);
596+
if (property.defaultvalue !== undefined)
597+
write(" = ", JSON.stringify(property.defaultvalue));
598+
if (i < element.properties.length - 1)
599+
writeln(",");
600+
else
601+
writeln();
602+
});
603+
--indent;
604+
writeln("}");
605+
}
591606

592607
} else {
593608

cli/pbjs.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ exports.main = function main(args, callback) {
3333
"force-long": "strict-long",
3434
"force-message": "strict-message"
3535
},
36-
string: [ "target", "out", "path", "wrap", "root", "lint" ],
37-
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-message" ],
36+
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
37+
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
3838
default: {
3939
target: "json",
4040
create: true,
@@ -49,6 +49,8 @@ exports.main = function main(args, callback) {
4949
lint: lintDefault,
5050
"keep-case": false,
5151
"force-long": false,
52+
"force-number": false,
53+
"force-enum-string": false,
5254
"force-message": false
5355
}
5456
});
@@ -99,6 +101,8 @@ exports.main = function main(args, callback) {
99101
" es6 ES6 wrapper (implies --es6)",
100102
" closure A closure adding to protobuf.roots where protobuf is a global",
101103
"",
104+
" --dependency Specifies which version of protobuf to require. Accepts any valid module id",
105+
"",
102106
" -r, --root Specifies an alternative protobuf.roots name.",
103107
"",
104108
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
@@ -123,6 +127,7 @@ exports.main = function main(args, callback) {
123127
" --no-comments Does not output any JSDoc comments.",
124128
"",
125129
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
130+
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
126131
" --force-message Enfores the use of message instances instead of plain objects.",
127132
"",
128133
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",

cli/targets/json-module.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = json_module;
33

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

6+
var protobuf = require("../..");
7+
68
json_module.description = "JSON representation as a module";
79

810
function json_module(root, options, callback) {
@@ -17,7 +19,7 @@ function json_module(root, options, callback) {
1719
}
1820
var json = util.jsonSafeProp(JSON.stringify(root.nested, null, 2).trim());
1921
output.push(".addJSON(" + json + ");");
20-
output = util.wrap(output.join(""), options);
22+
output = util.wrap(output.join(""), protobuf.util.merge({ dependency: "protobufjs/minimal" }, options));
2123
process.nextTick(function() {
2224
callback(null, output);
2325
});

cli/targets/static.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ function buildFunction(type, functionName, gen, scope) {
313313

314314
function toJsType(field) {
315315
var type;
316+
316317
switch (field.type) {
317318
case "double":
318319
case "float":
@@ -328,7 +329,7 @@ function toJsType(field) {
328329
case "sint64":
329330
case "fixed64":
330331
case "sfixed64":
331-
type = config.forceLong ? "Long" : "number|Long";
332+
type = config.forceLong ? "Long" : config.forceNumber ? "number" : "number|Long";
332333
break;
333334
case "bool":
334335
type = "boolean";
@@ -505,7 +506,7 @@ function buildType(ref, type) {
505506
++indent;
506507
push("if (!(reader instanceof $Reader))");
507508
++indent;
508-
push("reader = $Reader(reader);");
509+
push("reader = new $Reader(reader);");
509510
--indent;
510511
push("return this.decode(reader, reader.uint32());");
511512
--indent;
@@ -636,24 +637,25 @@ function buildEnum(ref, enm) {
636637
var comment = [
637638
enm.comment || enm.name + " enum.",
638639
enm.parent instanceof protobuf.Root ? "@exports " + escapeName(enm.name) : undefined,
639-
"@enum {number}",
640+
config.forceEnumString ? "@enum {number}" : "@enum {string}",
640641
];
641642
Object.keys(enm.values).forEach(function(key) {
642-
var val = enm.values[key];
643-
comment.push("@property {number} " + key + "=" + val + " " + (enm.comments[key] || key + " value"));
643+
var val = config.forceEnumString ? key : enm.values[key];
644+
comment.push((config.forceEnumString ? "@property {string} " : "@property {number} ") + key + "=" + val + " " + (enm.comments[key] || key + " value"));
644645
});
645646
pushComment(comment);
646647
push(escapeName(ref) + "." + escapeName(enm.name) + " = (function() {");
647648
++indent;
648649
push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
649650
var aliased = [];
650651
Object.keys(enm.values).forEach(function(key) {
651-
var val = enm.values[key];
652-
if (aliased.indexOf(val) > -1)
652+
var valueId = enm.values[key];
653+
var val = config.forceEnumString ? JSON.stringify(key) : valueId;
654+
if (aliased.indexOf(valueId) > -1)
653655
push("values[" + JSON.stringify(key) + "] = " + val + ";");
654656
else {
655-
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");
656-
aliased.push(val);
657+
push("values[valuesById[" + valueId + "] = " + JSON.stringify(key) + "] = " + val + ";");
658+
aliased.push(valueId);
657659
}
658660
});
659661
push("return values;");

cli/wrappers/amd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
define(["protobuf"], function($protobuf) {
1+
define([$DEPENDENCY], function($protobuf) {
22
"use strict";
33

44
$OUTPUT;

cli/wrappers/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function(global, factory) { /* global define, require, module */
22

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

66
/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
77
module.exports = factory(require($DEPENDENCY));

cli/wrappers/es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as $protobuf from "protobufjs";
1+
import * as $protobuf from $DEPENDENCY;
22

33
$OUTPUT;
44

0 commit comments

Comments
 (0)