Skip to content

Commit 2059ee0

Browse files
committed
Add support to generate types for JSON object.
Add support for string types.
1 parent 60fabe6 commit 2059ee0

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

cli/lib/tsd-jsdoc/publish.js

+27-12
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;
577+
var stringEnum = false;
580578
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();
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports.main = function main(args, callback) {
2323
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins";
2424
var argv = minimist(args, {
2525
alias: {
26+
constructor: "ct",
2627
target: "t",
2728
out: "o",
2829
path: "p",
@@ -34,9 +35,10 @@ exports.main = function main(args, callback) {
3435
"force-message": "strict-message"
3536
},
3637
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" ],
38+
boolean: [ "constructor", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
3839
default: {
3940
target: "json",
41+
constructor: true,
4042
create: true,
4143
encode: true,
4244
decode: true,
@@ -49,6 +51,8 @@ exports.main = function main(args, callback) {
4951
lint: lintDefault,
5052
"keep-case": false,
5153
"force-long": false,
54+
"force-number": false,
55+
"force-enum-string": false,
5256
"force-message": false
5357
}
5458
});
@@ -113,6 +117,7 @@ exports.main = function main(args, callback) {
113117
"",
114118
chalk.bold.gray(" Static targets only:"),
115119
"",
120+
" --no-constructor Does not generate constructor.",
116121
" --no-create Does not generate create functions used for reflection compatibility.",
117122
" --no-encode Does not generate encode functions.",
118123
" --no-decode Does not generate decode functions.",
@@ -123,6 +128,7 @@ exports.main = function main(args, callback) {
123128
" --no-comments Does not output any JSDoc comments.",
124129
"",
125130
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
131+
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
126132
" --force-message Enfores the use of message instances instead of plain objects.",
127133
"",
128134
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",

cli/targets/static.js

+10-8
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";
@@ -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;");

0 commit comments

Comments
 (0)