diff --git a/integration/oneof-unions-snake/google/protobuf/struct.ts b/integration/oneof-unions-snake/google/protobuf/struct.ts index 6e2f28c4e..6c2965c35 100644 --- a/integration/oneof-unions-snake/google/protobuf/struct.ts +++ b/integration/oneof-unions-snake/google/protobuf/struct.ts @@ -420,36 +420,43 @@ export const Value: MessageFns & AnyValueWrapperFns = { }, fromPartial, I>>(object: I): Value { const message = createBaseValue(); - if ( - object.kind?.$case === "null_value" && object.kind?.null_value !== undefined && object.kind?.null_value !== null - ) { - message.kind = { $case: "null_value", null_value: object.kind.null_value }; - } else if ( - object.kind?.$case === "number_value" && - object.kind?.number_value !== undefined && - object.kind?.number_value !== null - ) { - message.kind = { $case: "number_value", number_value: object.kind.number_value }; - } else if ( - object.kind?.$case === "string_value" && - object.kind?.string_value !== undefined && - object.kind?.string_value !== null - ) { - message.kind = { $case: "string_value", string_value: object.kind.string_value }; - } else if ( - object.kind?.$case === "bool_value" && object.kind?.bool_value !== undefined && object.kind?.bool_value !== null - ) { - message.kind = { $case: "bool_value", bool_value: object.kind.bool_value }; - } else if ( - object.kind?.$case === "struct_value" && - object.kind?.struct_value !== undefined && - object.kind?.struct_value !== null - ) { - message.kind = { $case: "struct_value", struct_value: object.kind.struct_value }; - } else if ( - object.kind?.$case === "list_value" && object.kind?.list_value !== undefined && object.kind?.list_value !== null - ) { - message.kind = { $case: "list_value", list_value: object.kind.list_value }; + switch (object.kind?.$case) { + case "null_value": { + if (object.kind?.null_value !== undefined && object.kind?.null_value !== null) { + message.kind = { $case: "null_value", null_value: object.kind.null_value }; + } + break; + } + case "number_value": { + if (object.kind?.number_value !== undefined && object.kind?.number_value !== null) { + message.kind = { $case: "number_value", number_value: object.kind.number_value }; + } + break; + } + case "string_value": { + if (object.kind?.string_value !== undefined && object.kind?.string_value !== null) { + message.kind = { $case: "string_value", string_value: object.kind.string_value }; + } + break; + } + case "bool_value": { + if (object.kind?.bool_value !== undefined && object.kind?.bool_value !== null) { + message.kind = { $case: "bool_value", bool_value: object.kind.bool_value }; + } + break; + } + case "struct_value": { + if (object.kind?.struct_value !== undefined && object.kind?.struct_value !== null) { + message.kind = { $case: "struct_value", struct_value: object.kind.struct_value }; + } + break; + } + case "list_value": { + if (object.kind?.list_value !== undefined && object.kind?.list_value !== null) { + message.kind = { $case: "list_value", list_value: object.kind.list_value }; + } + break; + } } return message; }, diff --git a/integration/oneof-unions-value/google/protobuf/struct.ts b/integration/oneof-unions-value/google/protobuf/struct.ts index 602beeb67..4bac8e861 100644 --- a/integration/oneof-unions-value/google/protobuf/struct.ts +++ b/integration/oneof-unions-value/google/protobuf/struct.ts @@ -417,24 +417,43 @@ export const Value: MessageFns & AnyValueWrapperFns = { }, fromPartial, I>>(object: I): Value { const message = createBaseValue(); - if (object.kind?.$case === "nullValue" && object.kind?.value !== undefined && object.kind?.value !== null) { - message.kind = { $case: "nullValue", value: object.kind.value }; - } else if ( - object.kind?.$case === "numberValue" && object.kind?.value !== undefined && object.kind?.value !== null - ) { - message.kind = { $case: "numberValue", value: object.kind.value }; - } else if ( - object.kind?.$case === "stringValue" && object.kind?.value !== undefined && object.kind?.value !== null - ) { - message.kind = { $case: "stringValue", value: object.kind.value }; - } else if (object.kind?.$case === "boolValue" && object.kind?.value !== undefined && object.kind?.value !== null) { - message.kind = { $case: "boolValue", value: object.kind.value }; - } else if ( - object.kind?.$case === "structValue" && object.kind?.value !== undefined && object.kind?.value !== null - ) { - message.kind = { $case: "structValue", value: object.kind.value }; - } else if (object.kind?.$case === "listValue" && object.kind?.value !== undefined && object.kind?.value !== null) { - message.kind = { $case: "listValue", value: object.kind.value }; + switch (object.kind?.$case) { + case "nullValue": { + if (object.kind?.value !== undefined && object.kind?.value !== null) { + message.kind = { $case: "nullValue", value: object.kind.value }; + } + break; + } + case "numberValue": { + if (object.kind?.value !== undefined && object.kind?.value !== null) { + message.kind = { $case: "numberValue", value: object.kind.value }; + } + break; + } + case "stringValue": { + if (object.kind?.value !== undefined && object.kind?.value !== null) { + message.kind = { $case: "stringValue", value: object.kind.value }; + } + break; + } + case "boolValue": { + if (object.kind?.value !== undefined && object.kind?.value !== null) { + message.kind = { $case: "boolValue", value: object.kind.value }; + } + break; + } + case "structValue": { + if (object.kind?.value !== undefined && object.kind?.value !== null) { + message.kind = { $case: "structValue", value: object.kind.value }; + } + break; + } + case "listValue": { + if (object.kind?.value !== undefined && object.kind?.value !== null) { + message.kind = { $case: "listValue", value: object.kind.value }; + } + break; + } } return message; }, diff --git a/integration/oneof-unions-value/oneof.ts b/integration/oneof-unions-value/oneof.ts index 439063cb4..5782a91e6 100644 --- a/integration/oneof-unions-value/oneof.ts +++ b/integration/oneof-unions-value/oneof.ts @@ -338,44 +338,64 @@ export const PleaseChoose: MessageFns = { fromPartial, I>>(object: I): PleaseChoose { const message = createBasePleaseChoose(); message.name = object.name ?? ""; - if (object.choice?.$case === "aNumber" && object.choice?.value !== undefined && object.choice?.value !== null) { - message.choice = { $case: "aNumber", value: object.choice.value }; - } else if ( - object.choice?.$case === "aString" && object.choice?.value !== undefined && object.choice?.value !== null - ) { - message.choice = { $case: "aString", value: object.choice.value }; - } else if ( - object.choice?.$case === "aMessage" && object.choice?.value !== undefined && object.choice?.value !== null - ) { - message.choice = { $case: "aMessage", value: PleaseChoose_Submessage.fromPartial(object.choice.value) }; - } else if ( - object.choice?.$case === "aBool" && object.choice?.value !== undefined && object.choice?.value !== null - ) { - message.choice = { $case: "aBool", value: object.choice.value }; - } else if ( - object.choice?.$case === "bunchaBytes" && object.choice?.value !== undefined && object.choice?.value !== null - ) { - message.choice = { $case: "bunchaBytes", value: object.choice.value }; - } else if ( - object.choice?.$case === "anEnum" && object.choice?.value !== undefined && object.choice?.value !== null - ) { - message.choice = { $case: "anEnum", value: object.choice.value }; + switch (object.choice?.$case) { + case "aNumber": { + if (object.choice?.value !== undefined && object.choice?.value !== null) { + message.choice = { $case: "aNumber", value: object.choice.value }; + } + break; + } + case "aString": { + if (object.choice?.value !== undefined && object.choice?.value !== null) { + message.choice = { $case: "aString", value: object.choice.value }; + } + break; + } + case "aMessage": { + if (object.choice?.value !== undefined && object.choice?.value !== null) { + message.choice = { $case: "aMessage", value: PleaseChoose_Submessage.fromPartial(object.choice.value) }; + } + break; + } + case "aBool": { + if (object.choice?.value !== undefined && object.choice?.value !== null) { + message.choice = { $case: "aBool", value: object.choice.value }; + } + break; + } + case "bunchaBytes": { + if (object.choice?.value !== undefined && object.choice?.value !== null) { + message.choice = { $case: "bunchaBytes", value: object.choice.value }; + } + break; + } + case "anEnum": { + if (object.choice?.value !== undefined && object.choice?.value !== null) { + message.choice = { $case: "anEnum", value: object.choice.value }; + } + break; + } } message.age = object.age ?? 0; - if ( - object.eitherOr?.$case === "either" && object.eitherOr?.value !== undefined && object.eitherOr?.value !== null - ) { - message.eitherOr = { $case: "either", value: object.eitherOr.value }; - } else if ( - object.eitherOr?.$case === "or" && object.eitherOr?.value !== undefined && object.eitherOr?.value !== null - ) { - message.eitherOr = { $case: "or", value: object.eitherOr.value }; - } else if ( - object.eitherOr?.$case === "thirdOption" && - object.eitherOr?.value !== undefined && - object.eitherOr?.value !== null - ) { - message.eitherOr = { $case: "thirdOption", value: object.eitherOr.value }; + switch (object.eitherOr?.$case) { + case "either": { + if (object.eitherOr?.value !== undefined && object.eitherOr?.value !== null) { + message.eitherOr = { $case: "either", value: object.eitherOr.value }; + } + break; + } + case "or": { + if (object.eitherOr?.value !== undefined && object.eitherOr?.value !== null) { + message.eitherOr = { $case: "or", value: object.eitherOr.value }; + } + break; + } + case "thirdOption": { + if (object.eitherOr?.value !== undefined && object.eitherOr?.value !== null) { + message.eitherOr = { $case: "thirdOption", value: object.eitherOr.value }; + } + break; + } } message.signature = object.signature ?? new Uint8Array(0); message.value = object.value ?? undefined; diff --git a/integration/oneof-unions/google/protobuf/struct.ts b/integration/oneof-unions/google/protobuf/struct.ts index 66c81b604..8cb68ac9c 100644 --- a/integration/oneof-unions/google/protobuf/struct.ts +++ b/integration/oneof-unions/google/protobuf/struct.ts @@ -417,34 +417,43 @@ export const Value: MessageFns & AnyValueWrapperFns = { }, fromPartial, I>>(object: I): Value { const message = createBaseValue(); - if (object.kind?.$case === "nullValue" && object.kind?.nullValue !== undefined && object.kind?.nullValue !== null) { - message.kind = { $case: "nullValue", nullValue: object.kind.nullValue }; - } else if ( - object.kind?.$case === "numberValue" && - object.kind?.numberValue !== undefined && - object.kind?.numberValue !== null - ) { - message.kind = { $case: "numberValue", numberValue: object.kind.numberValue }; - } else if ( - object.kind?.$case === "stringValue" && - object.kind?.stringValue !== undefined && - object.kind?.stringValue !== null - ) { - message.kind = { $case: "stringValue", stringValue: object.kind.stringValue }; - } else if ( - object.kind?.$case === "boolValue" && object.kind?.boolValue !== undefined && object.kind?.boolValue !== null - ) { - message.kind = { $case: "boolValue", boolValue: object.kind.boolValue }; - } else if ( - object.kind?.$case === "structValue" && - object.kind?.structValue !== undefined && - object.kind?.structValue !== null - ) { - message.kind = { $case: "structValue", structValue: object.kind.structValue }; - } else if ( - object.kind?.$case === "listValue" && object.kind?.listValue !== undefined && object.kind?.listValue !== null - ) { - message.kind = { $case: "listValue", listValue: object.kind.listValue }; + switch (object.kind?.$case) { + case "nullValue": { + if (object.kind?.nullValue !== undefined && object.kind?.nullValue !== null) { + message.kind = { $case: "nullValue", nullValue: object.kind.nullValue }; + } + break; + } + case "numberValue": { + if (object.kind?.numberValue !== undefined && object.kind?.numberValue !== null) { + message.kind = { $case: "numberValue", numberValue: object.kind.numberValue }; + } + break; + } + case "stringValue": { + if (object.kind?.stringValue !== undefined && object.kind?.stringValue !== null) { + message.kind = { $case: "stringValue", stringValue: object.kind.stringValue }; + } + break; + } + case "boolValue": { + if (object.kind?.boolValue !== undefined && object.kind?.boolValue !== null) { + message.kind = { $case: "boolValue", boolValue: object.kind.boolValue }; + } + break; + } + case "structValue": { + if (object.kind?.structValue !== undefined && object.kind?.structValue !== null) { + message.kind = { $case: "structValue", structValue: object.kind.structValue }; + } + break; + } + case "listValue": { + if (object.kind?.listValue !== undefined && object.kind?.listValue !== null) { + message.kind = { $case: "listValue", listValue: object.kind.listValue }; + } + break; + } } return message; }, diff --git a/integration/oneof-unions/oneof.ts b/integration/oneof-unions/oneof.ts index 8c17b53e3..667bf4ba6 100644 --- a/integration/oneof-unions/oneof.ts +++ b/integration/oneof-unions/oneof.ts @@ -338,44 +338,64 @@ export const PleaseChoose: MessageFns = { fromPartial, I>>(object: I): PleaseChoose { const message = createBasePleaseChoose(); message.name = object.name ?? ""; - if (object.choice?.$case === "aNumber" && object.choice?.aNumber !== undefined && object.choice?.aNumber !== null) { - message.choice = { $case: "aNumber", aNumber: object.choice.aNumber }; - } else if ( - object.choice?.$case === "aString" && object.choice?.aString !== undefined && object.choice?.aString !== null - ) { - message.choice = { $case: "aString", aString: object.choice.aString }; - } else if ( - object.choice?.$case === "aMessage" && object.choice?.aMessage !== undefined && object.choice?.aMessage !== null - ) { - message.choice = { $case: "aMessage", aMessage: PleaseChoose_Submessage.fromPartial(object.choice.aMessage) }; - } else if ( - object.choice?.$case === "aBool" && object.choice?.aBool !== undefined && object.choice?.aBool !== null - ) { - message.choice = { $case: "aBool", aBool: object.choice.aBool }; - } else if ( - object.choice?.$case === "bunchaBytes" && - object.choice?.bunchaBytes !== undefined && - object.choice?.bunchaBytes !== null - ) { - message.choice = { $case: "bunchaBytes", bunchaBytes: object.choice.bunchaBytes }; - } else if ( - object.choice?.$case === "anEnum" && object.choice?.anEnum !== undefined && object.choice?.anEnum !== null - ) { - message.choice = { $case: "anEnum", anEnum: object.choice.anEnum }; + switch (object.choice?.$case) { + case "aNumber": { + if (object.choice?.aNumber !== undefined && object.choice?.aNumber !== null) { + message.choice = { $case: "aNumber", aNumber: object.choice.aNumber }; + } + break; + } + case "aString": { + if (object.choice?.aString !== undefined && object.choice?.aString !== null) { + message.choice = { $case: "aString", aString: object.choice.aString }; + } + break; + } + case "aMessage": { + if (object.choice?.aMessage !== undefined && object.choice?.aMessage !== null) { + message.choice = { $case: "aMessage", aMessage: PleaseChoose_Submessage.fromPartial(object.choice.aMessage) }; + } + break; + } + case "aBool": { + if (object.choice?.aBool !== undefined && object.choice?.aBool !== null) { + message.choice = { $case: "aBool", aBool: object.choice.aBool }; + } + break; + } + case "bunchaBytes": { + if (object.choice?.bunchaBytes !== undefined && object.choice?.bunchaBytes !== null) { + message.choice = { $case: "bunchaBytes", bunchaBytes: object.choice.bunchaBytes }; + } + break; + } + case "anEnum": { + if (object.choice?.anEnum !== undefined && object.choice?.anEnum !== null) { + message.choice = { $case: "anEnum", anEnum: object.choice.anEnum }; + } + break; + } } message.age = object.age ?? 0; - if ( - object.eitherOr?.$case === "either" && object.eitherOr?.either !== undefined && object.eitherOr?.either !== null - ) { - message.eitherOr = { $case: "either", either: object.eitherOr.either }; - } else if (object.eitherOr?.$case === "or" && object.eitherOr?.or !== undefined && object.eitherOr?.or !== null) { - message.eitherOr = { $case: "or", or: object.eitherOr.or }; - } else if ( - object.eitherOr?.$case === "thirdOption" && - object.eitherOr?.thirdOption !== undefined && - object.eitherOr?.thirdOption !== null - ) { - message.eitherOr = { $case: "thirdOption", thirdOption: object.eitherOr.thirdOption }; + switch (object.eitherOr?.$case) { + case "either": { + if (object.eitherOr?.either !== undefined && object.eitherOr?.either !== null) { + message.eitherOr = { $case: "either", either: object.eitherOr.either }; + } + break; + } + case "or": { + if (object.eitherOr?.or !== undefined && object.eitherOr?.or !== null) { + message.eitherOr = { $case: "or", or: object.eitherOr.or }; + } + break; + } + case "thirdOption": { + if (object.eitherOr?.thirdOption !== undefined && object.eitherOr?.thirdOption !== null) { + message.eitherOr = { $case: "thirdOption", thirdOption: object.eitherOr.thirdOption }; + } + break; + } } message.signature = object.signature ?? new Uint8Array(0); message.value = object.value ?? undefined; diff --git a/integration/use-readonly-types/google/protobuf/struct.ts b/integration/use-readonly-types/google/protobuf/struct.ts index a3c773bf5..cde196620 100644 --- a/integration/use-readonly-types/google/protobuf/struct.ts +++ b/integration/use-readonly-types/google/protobuf/struct.ts @@ -417,34 +417,43 @@ export const Value: MessageFns & AnyValueWrapperFns = { }, fromPartial, I>>(object: I): Value { const message = createBaseValue() as any; - if (object.kind?.$case === "nullValue" && object.kind?.nullValue !== undefined && object.kind?.nullValue !== null) { - message.kind = { $case: "nullValue", nullValue: object.kind.nullValue }; - } else if ( - object.kind?.$case === "numberValue" && - object.kind?.numberValue !== undefined && - object.kind?.numberValue !== null - ) { - message.kind = { $case: "numberValue", numberValue: object.kind.numberValue }; - } else if ( - object.kind?.$case === "stringValue" && - object.kind?.stringValue !== undefined && - object.kind?.stringValue !== null - ) { - message.kind = { $case: "stringValue", stringValue: object.kind.stringValue }; - } else if ( - object.kind?.$case === "boolValue" && object.kind?.boolValue !== undefined && object.kind?.boolValue !== null - ) { - message.kind = { $case: "boolValue", boolValue: object.kind.boolValue }; - } else if ( - object.kind?.$case === "structValue" && - object.kind?.structValue !== undefined && - object.kind?.structValue !== null - ) { - message.kind = { $case: "structValue", structValue: object.kind.structValue }; - } else if ( - object.kind?.$case === "listValue" && object.kind?.listValue !== undefined && object.kind?.listValue !== null - ) { - message.kind = { $case: "listValue", listValue: object.kind.listValue }; + switch (object.kind?.$case) { + case "nullValue": { + if (object.kind?.nullValue !== undefined && object.kind?.nullValue !== null) { + message.kind = { $case: "nullValue", nullValue: object.kind.nullValue }; + } + break; + } + case "numberValue": { + if (object.kind?.numberValue !== undefined && object.kind?.numberValue !== null) { + message.kind = { $case: "numberValue", numberValue: object.kind.numberValue }; + } + break; + } + case "stringValue": { + if (object.kind?.stringValue !== undefined && object.kind?.stringValue !== null) { + message.kind = { $case: "stringValue", stringValue: object.kind.stringValue }; + } + break; + } + case "boolValue": { + if (object.kind?.boolValue !== undefined && object.kind?.boolValue !== null) { + message.kind = { $case: "boolValue", boolValue: object.kind.boolValue }; + } + break; + } + case "structValue": { + if (object.kind?.structValue !== undefined && object.kind?.structValue !== null) { + message.kind = { $case: "structValue", structValue: object.kind.structValue }; + } + break; + } + case "listValue": { + if (object.kind?.listValue !== undefined && object.kind?.listValue !== null) { + message.kind = { $case: "listValue", listValue: object.kind.listValue }; + } + break; + } } return message; }, diff --git a/integration/use-readonly-types/use-readonly-types.ts b/integration/use-readonly-types/use-readonly-types.ts index a88bb5bf8..a9bcbd5a9 100644 --- a/integration/use-readonly-types/use-readonly-types.ts +++ b/integration/use-readonly-types/use-readonly-types.ts @@ -293,18 +293,19 @@ export const Entity: MessageFns = { message.fieldMask = object.fieldMask ?? undefined; message.listValue = object.listValue ?? undefined; message.structValue = object.structValue ?? undefined; - if ( - object.oneOfValue?.$case === "theStringValue" && - object.oneOfValue?.theStringValue !== undefined && - object.oneOfValue?.theStringValue !== null - ) { - message.oneOfValue = { $case: "theStringValue", theStringValue: object.oneOfValue.theStringValue }; - } else if ( - object.oneOfValue?.$case === "theIntValue" && - object.oneOfValue?.theIntValue !== undefined && - object.oneOfValue?.theIntValue !== null - ) { - message.oneOfValue = { $case: "theIntValue", theIntValue: object.oneOfValue.theIntValue }; + switch (object.oneOfValue?.$case) { + case "theStringValue": { + if (object.oneOfValue?.theStringValue !== undefined && object.oneOfValue?.theStringValue !== null) { + message.oneOfValue = { $case: "theStringValue", theStringValue: object.oneOfValue.theStringValue }; + } + break; + } + case "theIntValue": { + if (object.oneOfValue?.theIntValue !== undefined && object.oneOfValue?.theIntValue !== null) { + message.oneOfValue = { $case: "theIntValue", theIntValue: object.oneOfValue.theIntValue }; + } + break; + } } return message; }, diff --git a/src/main.ts b/src/main.ts index 2c73628af..4fdc12157 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2592,7 +2592,7 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri chunks.push(code`const message = ${createBase}${maybeAsAny(options)};`); - let currentIfTarget = ""; + let currentSwitchTarget: string | undefined; // add a check for each incoming field messageDesc.field.forEach((field) => { @@ -2600,6 +2600,12 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri const messageProperty = getPropertyAccessor("message", fieldName); const objectProperty = getPropertyAccessor("object", fieldName); + if (currentSwitchTarget && !isWithinOneOfThatShouldBeUnion(options, field)) { + // We are exiting a switch, we need to close it + chunks.push(code`}`); + currentSwitchTarget = undefined; + } + const readSnippet = (from: string): Code => { if ( (isLong(field) || isLongValueType(field)) && @@ -2710,16 +2716,25 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri const oneofNameWithObject = getPropertyAccessor("object", oneofName); const valueName = oneofValueName(fieldName, options); const v = readSnippet(`${oneofNameWithObject}.${valueName}`); + + // If we are entering a new switch, close the previous one + if (currentSwitchTarget !== undefined && currentSwitchTarget !== oneofNameWithObject) { + chunks.push(code`}`); + currentSwitchTarget = undefined; + } + if (currentSwitchTarget === undefined) { + chunks.push(code`switch (${oneofNameWithObject}?.$case) {`); + } chunks.push(code` - ${currentIfTarget === oneofNameWithObject ? "else " : ""}if ( - ${oneofNameWithObject}?.$case === '${fieldName}' - && ${oneofNameWithObject}?.${valueName} !== undefined - && ${oneofNameWithObject}?.${valueName} !== null - ) { - ${oneofNameWithMessage} = { $case: '${fieldName}', ${valueName}: ${v} }; + case '${fieldName}': { + if (${oneofNameWithObject}?.${valueName} !== undefined + && ${oneofNameWithObject}?.${valueName} !== null) { + ${oneofNameWithMessage} = { $case: '${fieldName}', ${valueName}: ${v} }; + } + break; } `); - currentIfTarget = oneofNameWithObject; + currentSwitchTarget = oneofNameWithObject; } else if (readSnippet(`x`).toCodeString([]) == "x") { // An optimized case of the else below that works when `readSnippet` returns the plain input const fallback = isWithinOneOf(field) || noDefaultValue ? "undefined" : defaultValue(ctx, field); @@ -2734,6 +2749,12 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri } }); + if (currentSwitchTarget) { + // We are exiting a switch, we need to close it + chunks.push(code`}`); + currentSwitchTarget = ""; + } + // and then wrap up the switch/while/return chunks.push(code`return message;`); chunks.push(code`}`);