Skip to content

Commit

Permalink
Use empty string for key-value pair
Browse files Browse the repository at this point in the history
  • Loading branch information
tresabhi committed Oct 5, 2024
1 parent b61f16a commit 59ea571
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@ function generateBaseInstanceFactory(
): Code {
const { options, currentFile } = ctx;
const fields: Code[] = [];
const keyValuePair = isKeyValuePair(messageDesc.field);

// When oneof=unions, we generate a single property with an ADT per `oneof` clause.
const processedOneofs = new Set<number>();
Expand All @@ -1260,15 +1261,23 @@ function generateBaseInstanceFactory(
}

const fieldKey = safeAccessor(getFieldName(field, options));
const val = isWithinOneOf(field)
? nullOrUndefined(options)
: isMapType(ctx, messageDesc, field)
? shouldGenerateJSMapType(ctx, messageDesc, field)
? "new Map()"
: "{}"
: isRepeated(field)
? "[]"
: defaultValue(ctx, field);
let val;

if (keyValuePair) {
val = '""';
} else if (isWithinOneOf(field)) {
val = nullOrUndefined(options);
} else if (isMapType(ctx, messageDesc, field)) {
if (shouldGenerateJSMapType(ctx, messageDesc, field)) {
val = "new Map()";
} else {
val = "{}";
}
} else if (isRepeated(field)) {
val = "[]";
} else {
val = defaultValue(ctx, field);
}

fields.push(code`${fieldKey}: ${val}`);
}
Expand Down

0 comments on commit 59ea571

Please sign in to comment.