mirror of
https://github.com/protobufjs/protobuf.js.git
synced 2026-02-01 17:21:20 +00:00
Add support to generate types for JSON object.
Add support for string types.
This commit is contained in:
parent
60fabe60e8
commit
2059ee0f6f
@ -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 {
|
||||
|
||||
|
||||
@ -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] -",
|
||||
|
||||
@ -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;");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user