Skip to content

Commit

Permalink
Add support to generate types for JSON object.
Browse files Browse the repository at this point in the history
Add support for string types.
  • Loading branch information
robin-anil committed May 23, 2017
1 parent 60fabe6 commit 2059ee0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
39 changes: 27 additions & 12 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;
var stringEnum = false;
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();
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
8 changes: 7 additions & 1 deletion cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports.main = function main(args, callback) {
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins";
var argv = minimist(args, {
alias: {
constructor: "ct",
target: "t",
out: "o",
path: "p",
Expand All @@ -34,9 +35,10 @@ exports.main = function main(args, callback) {
"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" ],
boolean: [ "constructor", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
default: {
target: "json",
constructor: true,
create: true,
encode: true,
decode: true,
Expand All @@ -49,6 +51,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 @@ -113,6 +117,7 @@ exports.main = function main(args, callback) {
"",
chalk.bold.gray(" Static targets only:"),
"",
" --no-constructor Does not generate constructor.",
" --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.",
Expand All @@ -123,6 +128,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) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
Expand Down
18 changes: 10 additions & 8 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 @@ -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

0 comments on commit 2059ee0

Please sign in to comment.