Skip to content

Commit

Permalink
Remove assertSet from fromPartial; it'll only be used in fromJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
tresabhi committed Oct 5, 2024
1 parent fe0a942 commit a3cff76
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2756,22 +2756,15 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri
}
`);
} else if (readSnippet(`x`).toCodeString([]) == "x") {
if (
ctx.currentFile.isProto3Syntax ||
(
// proto2 optional label should be trated the same as any field in proto3
field.label === FieldDescriptorProto_Label.LABEL_OPTIONAL
// key-value pairs (the ones with the "Entry" suffix) have an optional label for some reason
// so detect a pair and force it to be defined
&& !keyValuePair
)
) {
// An optimized case of the else below that works when `readSnippet` returns the plain input
const fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field);
chunks.push(code`${messageProperty} = ${objectProperty} ?? ${fallback};`);
let fallback

if (keyValuePair) {
fallback = '""'
} else {
chunks.push(code`${messageProperty} = ${ctx.utils.assertSet}('${fullName}.${fieldName}', ${objectProperty});`);
fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field);
}

chunks.push(code`${messageProperty} = ${objectProperty} ?? ${fallback};`);
} else {
const fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field);
chunks.push(code`
Expand Down

0 comments on commit a3cff76

Please sign in to comment.