Skip to content

Commit

Permalink
Implement propagation of interface / message types under --null-seman…
Browse files Browse the repository at this point in the history
…tics flag
  • Loading branch information
martin-traverse committed Jul 26, 2024
1 parent 623477b commit d5700a5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,15 @@ function buildFunction(type, functionName, gen, scope) {
push("};");
}

function toJsType(field) {
function toJsType(field, parentIsInterface) {
var type;

// With null semantics, interfaces are composed from interfaces and messages from messages
// Without null semantics, child types depend on the --force-message flag
var asInterface = config["null-semantics"]
? parentIsInterface && !(field.resolvedType instanceof protobuf.Enum)
: !(field.resolvedType instanceof protobuf.Enum || config.forceMessage);

switch (field.type) {
case "double":
case "float":
Expand Down Expand Up @@ -342,7 +348,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 Down Expand Up @@ -407,7 +413,7 @@ 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, /* parentIsInterface = */ true);
var nullable = false;
if (config["null-semantics"]) {
// With semantic nulls, decide which fields are required for the current protobuf version
Expand Down Expand Up @@ -451,7 +457,7 @@ function buildType(ref, type) {
var prop = util.safeProp(field.name);
if (config.comments) {
push("");
var jsType = toJsType(field);
var jsType = toJsType(field, /* parentIsInterface = */ false);
if (config["null-semantics"]) {
// With semantic nulls, fields are nullable if they are explicitly optional or part of a one-of
// Maps, repeated values and fields with implicit defaults are never null after construction
Expand Down

0 comments on commit d5700a5

Please sign in to comment.