Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature / Updated null behaviour (TS, --force-message, properties and members) #2013

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] -
```
Expand Down
2 changes: 1 addition & 1 deletion cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
"",
Expand Down
16 changes: 10 additions & 6 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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;
Expand All @@ -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("");
Expand All @@ -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,
Expand Down
Loading