Skip to content

Commit aa61793

Browse files
Only look up proto syntax once per type in the static generator
1 parent 996e445 commit aa61793

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

cli/targets/static.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,26 @@ function toJsType(field) {
354354
return type;
355355
}
356356

357-
function isOptional(type, field) {
358-
359-
// Figure out if a field is explicitly marked optional, depending on the proto syntax (proto2 vs proto3)
360-
// If the syntax has not been recorded in the AST, proto2 semantics will be the default
357+
function syntaxForType(type) {
361358

362359
var syntax = null;
363360
var namespace = type;
364361

365362
while (syntax === null && namespace !== null) {
366363
if (namespace.options != null && "syntax" in namespace.options)
367-
syntax = namespace.options["syntax"]
364+
syntax = namespace.options["syntax"];
368365
else
369-
namespace = namespace.parent
366+
namespace = namespace.parent;
370367
}
371368

369+
if (syntax === "proto3")
370+
return "proto3";
371+
else
372+
return "proto2";
373+
}
374+
375+
function isOptional(field, syntax) {
376+
372377
if (syntax === "proto3")
373378
return field.proto3Optional
374379
else
@@ -377,6 +382,8 @@ function isOptional(type, field) {
377382

378383
function buildType(ref, type) {
379384

385+
var syntax = syntaxForType(type);
386+
380387
if (config.comments) {
381388
var typeDef = [
382389
"Properties of " + aOrAn(type.name) + ".",
@@ -392,7 +399,7 @@ function buildType(ref, type) {
392399
// New behaviour - fields explicitly marked as optional and members of a one-of are nullable
393400
// Maps and repeated fields are not explicitly optional, they default to empty instances
394401
if (config["force-optional"]) {
395-
if (isOptional(type, field) || field.partOf) {
402+
if (isOptional(field, syntax) || field.partOf) {
396403
jsType = jsType + "|null|undefined";
397404
nullable = true;
398405
}
@@ -435,7 +442,7 @@ function buildType(ref, type) {
435442
// New behaviour - fields explicitly marked as optional and members of a one-of are nullable
436443
// Maps and repeated fields are not explicitly optional, they default to empty instances
437444
if (config["force-optional"]) {
438-
if (isOptional(type, field) || field.partOf)
445+
if (isOptional(field, syntax) || field.partOf)
439446
jsType = jsType + "|null|undefined";
440447
}
441448
// Old behaviour - field.optional is true for all fields in proto3
@@ -457,7 +464,7 @@ function buildType(ref, type) {
457464
// New behaviour sets a null default when the optional keyword is used explicitly
458465
// Old behaviour considers all proto3 fields optional and uses the null-defaults config flag
459466
var nullDefault = config["force-optional"]
460-
? isOptional(type, field)
467+
? isOptional(field, syntax)
461468
: field.optional && config["null-defaults"];
462469
if (field.repeated)
463470
push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyArray;"); // overwritten in constructor

0 commit comments

Comments
 (0)