From 2059ee0f6f951575d5c5d2dc5eb06b6fa34e27aa Mon Sep 17 00:00:00 2001 From: Robin Anil Date: Tue, 23 May 2017 13:28:37 -0500 Subject: [PATCH] Add support to generate types for JSON object. Add support for string types. --- cli/lib/tsd-jsdoc/publish.js | 39 +++++++++++++++++++++++++----------- cli/pbjs.js | 8 +++++++- cli/targets/static.js | 18 +++++++++-------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/cli/lib/tsd-jsdoc/publish.js b/cli/lib/tsd-jsdoc/publish.js index f9321cd10..e506cfa1f 100644 --- a/cli/lib/tsd-jsdoc/publish.js +++ b/cli/lib/tsd-jsdoc/publish.js @@ -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 { diff --git a/cli/pbjs.js b/cli/pbjs.js index af74d931a..78c12b3a0 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -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", @@ -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, @@ -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 } }); @@ -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.", @@ -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] -", diff --git a/cli/targets/static.js b/cli/targets/static.js index 8176018fc..873a1658a 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -313,6 +313,7 @@ function buildFunction(type, functionName, gen, scope) { function toJsType(field) { var type; + switch (field.type) { case "double": case "float": @@ -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"; @@ -636,11 +637,11 @@ 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() {"); @@ -648,12 +649,13 @@ function buildEnum(ref, enm) { 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;");