diff --git a/cli/README.md b/cli/README.md index e7f189007..cfca67191 100644 --- a/cli/README.md +++ b/cli/README.md @@ -67,7 +67,7 @@ Translates between file formats and generates static code. --force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields. --force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields. - --force-message Enforces the use of message instances instead of plain objects. + --force-message Enforces the use of message instances instead of plain objects for method calls. usage: pbjs [options] file1.proto file2.json ... (or pipe) other | pbjs [options] - ``` diff --git a/cli/pbjs.js b/cli/pbjs.js index 150a2b6f0..fd0d84fd7 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -145,7 +145,7 @@ exports.main = function main(args, callback) { "", " --force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.", " --force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields.", - " --force-message Enforces the use of message instances instead of plain objects.", + " --force-message Enforces the use of message instances instead of plain objects for method calls.", "", " --null-defaults Default value for optional fields is null instead of zero value.", "", diff --git a/cli/targets/static.js b/cli/targets/static.js index c130d9026..4ec14b2d0 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -311,9 +311,13 @@ function buildFunction(type, functionName, gen, scope) { push("};"); } -function toJsType(field) { +function toJsType(field, asInterface) { var type; + // Never declare enums as interfaces + if (field.resolvedType != null && field.resolvedType instanceof protobuf.Enum) + asInterface = false; + switch (field.type) { case "double": case "float": @@ -342,7 +346,7 @@ function toJsType(field) { break; default: if (field.resolve().resolvedType) - type = exportName(field.resolvedType, !(field.resolvedType instanceof protobuf.Enum || config.forceMessage)); + type = exportName(field.resolvedType, asInterface); else type = "*"; // should not happen break; @@ -365,9 +369,9 @@ function buildType(ref, type) { type.fieldsArray.forEach(function(field) { var prop = util.safeProp(field.name); // either .name or ["name"] prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length); - var jsType = toJsType(field); + var jsType = toJsType(field, /*asInterface = */ true); if (field.optional) - jsType = jsType + "|null"; + jsType = jsType + "|null|undefined"; // Incoming properties can include undefined members typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : prop) + " " + (field.comment || type.name + " " + field.name)); }); push(""); @@ -393,9 +397,9 @@ function buildType(ref, type) { var prop = util.safeProp(field.name); if (config.comments) { push(""); - var jsType = toJsType(field); + var jsType = toJsType(field, /*asInterface = */ false); if (field.optional && !field.map && !field.repeated && (field.resolvedType instanceof Type || config["null-defaults"]) || field.partOf) - jsType = jsType + "|null|undefined"; + jsType = jsType + "|null"; // Members are never undefined, they are initialized in the prototype pushComment([ field.comment || type.name + " " + field.name + ".", "@member {" + jsType + "} " + field.name,