Skip to content

Commit d5700a5

Browse files
Implement propagation of interface / message types under --null-semantics flag
1 parent 623477b commit d5700a5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

cli/targets/static.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,15 @@ function buildFunction(type, functionName, gen, scope) {
311311
push("};");
312312
}
313313

314-
function toJsType(field) {
314+
function toJsType(field, parentIsInterface) {
315315
var type;
316316

317+
// With null semantics, interfaces are composed from interfaces and messages from messages
318+
// Without null semantics, child types depend on the --force-message flag
319+
var asInterface = config["null-semantics"]
320+
? parentIsInterface && !(field.resolvedType instanceof protobuf.Enum)
321+
: !(field.resolvedType instanceof protobuf.Enum || config.forceMessage);
322+
317323
switch (field.type) {
318324
case "double":
319325
case "float":
@@ -342,7 +348,7 @@ function toJsType(field) {
342348
break;
343349
default:
344350
if (field.resolve().resolvedType)
345-
type = exportName(field.resolvedType, !(field.resolvedType instanceof protobuf.Enum || config.forceMessage));
351+
type = exportName(field.resolvedType, asInterface);
346352
else
347353
type = "*"; // should not happen
348354
break;
@@ -407,7 +413,7 @@ function buildType(ref, type) {
407413
type.fieldsArray.forEach(function(field) {
408414
var prop = util.safeProp(field.name); // either .name or ["name"]
409415
prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
410-
var jsType = toJsType(field);
416+
var jsType = toJsType(field, /* parentIsInterface = */ true);
411417
var nullable = false;
412418
if (config["null-semantics"]) {
413419
// With semantic nulls, decide which fields are required for the current protobuf version
@@ -451,7 +457,7 @@ function buildType(ref, type) {
451457
var prop = util.safeProp(field.name);
452458
if (config.comments) {
453459
push("");
454-
var jsType = toJsType(field);
460+
var jsType = toJsType(field, /* parentIsInterface = */ false);
455461
if (config["null-semantics"]) {
456462
// With semantic nulls, fields are nullable if they are explicitly optional or part of a one-of
457463
// Maps, repeated values and fields with implicit defaults are never null after construction

0 commit comments

Comments
 (0)