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

Update TypeScript default values for proto3 syntax #743

Closed
wants to merge 2 commits into from
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
39 changes: 33 additions & 6 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function static_target(root, options, callback) {
}
var rootProp = cliUtil.safeProp(config.root || "default");
push((config.es6 ? "const" : "var") + " $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
buildNamespace(null, root);
buildNamespace(null, root, root);
return callback(null, out.join("\n"));
} catch (err) {
return callback(err);
Expand Down Expand Up @@ -92,7 +92,7 @@ function aOrAn(name) {
: "a ") + name;
}

function buildNamespace(ref, ns) {
function buildNamespace(ref, ns, root) {
if (!ns)
return;
if (ns.name !== "") {
Expand All @@ -105,7 +105,7 @@ function buildNamespace(ref, ns) {
}

if (ns instanceof Type) {
buildType(undefined, ns);
buildType(undefined, ns, root);
} else if (ns instanceof Service)
buildService(undefined, ns);
else if (ns.name !== "") {
Expand All @@ -122,7 +122,7 @@ function buildNamespace(ref, ns) {
if (nested instanceof Enum)
buildEnum(ns.name, nested);
else if (nested instanceof Namespace)
buildNamespace(ns.name, nested);
buildNamespace(ns.name, nested, root);
});
if (ns.name !== "") {
push("");
Expand Down Expand Up @@ -338,7 +338,27 @@ function toJsType(field) {
: type;
}

function buildType(ref, type) {
/**
* If the given field will have a default value assigned to it by a Proto3 lib
* @param {Field} field
* @returns {boolean}
*/
function willFieldHaveDefaultValue(field) {
if (field.repeated || field.map) {
// Default value being an empty array or map
return true;
}

field.resolve();
if (!field.resolvedType || field.resolvedType instanceof Enum) {
// Enums default to 0
return true;
}

return false;
}

function buildType(ref, type, root) {
var fullName = type.fullName.substring(1);

if (config.comments) {
Expand Down Expand Up @@ -372,11 +392,18 @@ function buildType(ref, type) {
type.fieldsArray.forEach(function(field) {
field.resolve();
var prop = util.safeProp(field.name);
var canFieldBeUndefined = field.optional;
if (root.containsProto3) {
// Only fields not given defaults can be undefined since proto3 has
// no "optional" or "required" fields
canFieldBeUndefined = !willFieldHaveDefaultValue(field);
}

if (config.comments) {
push("");
pushComment([
field.comment || type.name + " " + field.name + ".",
"@type {" + toJsType(field) + (field.optional ? "|undefined" : "") + "}"
"@type {" + toJsType(field) + (canFieldBeUndefined ? "|null" : "") + "}"
]);
} else if (firstField) {
push("");
Expand Down
10 changes: 10 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ function parse(source, root, options) {
syntax,
isProto3 = false;

/**
* If proto3 syntax was used at any point in the source
*/
var sourceContainsProto3 = false;

var ptr = root;

var applyCase = options.keepCase ? function(name) { return name; } : camelCase;
Expand Down Expand Up @@ -244,6 +249,10 @@ function parse(source, root, options) {
syntax = readString();
isProto3 = syntax === "proto3";

if (!sourceContainsProto3) {
sourceContainsProto3 = isProto3;
}

/* istanbul ignore if */
if (!isProto3 && syntax !== "proto2")
throw illegal(syntax, "syntax");
Expand Down Expand Up @@ -729,6 +738,7 @@ function parse(source, root, options) {
}

parse.filename = null;
root.containsProto3 = sourceContainsProto3;
return {
"package" : pkg,
"imports" : imports,
Expand Down
6 changes: 6 additions & 0 deletions src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ function Root(options) {
* @type {string[]}
*/
this.files = [];

/**
* If any sources which built this Root used protobuf 3 syntax
* @type {boolean}
*/
this.containsProto3 = false;
}

/**
Expand Down