ts-proto 2.4.0 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/main.js +29 -7
- package/package.json +1 -1
package/build/src/main.js
CHANGED
|
@@ -1971,6 +1971,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1971
1971
|
toJSON(${messageDesc.field.length > 0 ? "message" : "_"}: ${fullName}): unknown {
|
|
1972
1972
|
const obj: any = {};
|
|
1973
1973
|
`);
|
|
1974
|
+
let currentIfTarget = "";
|
|
1974
1975
|
// then add a case for each field
|
|
1975
1976
|
messageDesc.field.forEach((field) => {
|
|
1976
1977
|
const fieldName = (0, utils_1.getFieldName)(field, options);
|
|
@@ -2120,10 +2121,11 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
2120
2121
|
: (0, utils_1.getPropertyAccessor)("message", (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[field.oneofIndex].name, options));
|
|
2121
2122
|
const valueName = (0, utils_1.oneofValueName)(fieldName, options);
|
|
2122
2123
|
chunks.push((0, ts_poet_1.code) `
|
|
2123
|
-
if (${oneofNameWithMessage}?.$case === '${fieldName}') {
|
|
2124
|
+
${currentIfTarget === oneofNameWithMessage ? "else " : ""}if (${oneofNameWithMessage}?.$case === '${fieldName}') {
|
|
2124
2125
|
${jsonProperty} = ${readSnippet(`${oneofNameWithMessage}.${valueName}`)};
|
|
2125
2126
|
}
|
|
2126
2127
|
`);
|
|
2128
|
+
currentIfTarget = oneofNameWithMessage;
|
|
2127
2129
|
}
|
|
2128
2130
|
else {
|
|
2129
2131
|
let emitDefaultValuesForJson = ctx.options.emitDefaultValues.includes("json-methods");
|
|
@@ -2176,11 +2178,17 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
2176
2178
|
createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
|
|
2177
2179
|
}
|
|
2178
2180
|
chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
|
|
2181
|
+
let currentSwitchTarget;
|
|
2179
2182
|
// add a check for each incoming field
|
|
2180
2183
|
messageDesc.field.forEach((field) => {
|
|
2181
2184
|
const fieldName = (0, utils_1.getFieldName)(field, options);
|
|
2182
2185
|
const messageProperty = (0, utils_1.getPropertyAccessor)("message", fieldName);
|
|
2183
2186
|
const objectProperty = (0, utils_1.getPropertyAccessor)("object", fieldName);
|
|
2187
|
+
if (currentSwitchTarget && !(0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
|
|
2188
|
+
// We are exiting a switch, we need to close it
|
|
2189
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
2190
|
+
currentSwitchTarget = undefined;
|
|
2191
|
+
}
|
|
2184
2192
|
const readSnippet = (from) => {
|
|
2185
2193
|
if (((0, types_1.isLong)(field) || (0, types_1.isLongValueType)(field)) &&
|
|
2186
2194
|
options.forceLong === options_1.LongOption.LONG &&
|
|
@@ -2297,15 +2305,24 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
2297
2305
|
const oneofNameWithObject = (0, utils_1.getPropertyAccessor)("object", oneofName);
|
|
2298
2306
|
const valueName = (0, utils_1.oneofValueName)(fieldName, options);
|
|
2299
2307
|
const v = readSnippet(`${oneofNameWithObject}.${valueName}`);
|
|
2308
|
+
// If we are entering a new switch, close the previous one
|
|
2309
|
+
if (currentSwitchTarget !== undefined && currentSwitchTarget !== oneofNameWithObject) {
|
|
2310
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
2311
|
+
currentSwitchTarget = undefined;
|
|
2312
|
+
}
|
|
2313
|
+
if (currentSwitchTarget === undefined) {
|
|
2314
|
+
chunks.push((0, ts_poet_1.code) `switch (${oneofNameWithObject}?.$case) {`);
|
|
2315
|
+
}
|
|
2300
2316
|
chunks.push((0, ts_poet_1.code) `
|
|
2301
|
-
|
|
2302
|
-
${oneofNameWithObject}?.$
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2317
|
+
case '${fieldName}': {
|
|
2318
|
+
if (${oneofNameWithObject}?.${valueName} !== undefined
|
|
2319
|
+
&& ${oneofNameWithObject}?.${valueName} !== null) {
|
|
2320
|
+
${oneofNameWithMessage} = { $case: '${fieldName}', ${valueName}: ${v} };
|
|
2321
|
+
}
|
|
2322
|
+
break;
|
|
2307
2323
|
}
|
|
2308
2324
|
`);
|
|
2325
|
+
currentSwitchTarget = oneofNameWithObject;
|
|
2309
2326
|
}
|
|
2310
2327
|
else if (readSnippet(`x`).toCodeString([]) == "x") {
|
|
2311
2328
|
// An optimized case of the else below that works when `readSnippet` returns the plain input
|
|
@@ -2321,6 +2338,11 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
2321
2338
|
`);
|
|
2322
2339
|
}
|
|
2323
2340
|
});
|
|
2341
|
+
if (currentSwitchTarget) {
|
|
2342
|
+
// We are exiting a switch, we need to close it
|
|
2343
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
2344
|
+
currentSwitchTarget = "";
|
|
2345
|
+
}
|
|
2324
2346
|
// and then wrap up the switch/while/return
|
|
2325
2347
|
chunks.push((0, ts_poet_1.code) `return message;`);
|
|
2326
2348
|
chunks.push((0, ts_poet_1.code) `}`);
|