ts-proto 2.2.7 → 2.4.0
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 +18 -27
- package/package.json +1 -1
package/build/src/main.js
CHANGED
|
@@ -836,36 +836,27 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
836
836
|
}
|
|
837
837
|
function generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo) {
|
|
838
838
|
const { options } = ctx;
|
|
839
|
-
const fields = messageDesc.field
|
|
839
|
+
const fields = messageDesc.field
|
|
840
|
+
.map((field, index) => ({ index, field }))
|
|
841
|
+
.filter((item) => (0, types_1.isWithinOneOf)(item.field) && item.field.oneofIndex === oneofIndex);
|
|
840
842
|
const mbReadonly = maybeReadonly(options);
|
|
841
|
-
const
|
|
842
|
-
|
|
843
|
-
|
|
843
|
+
const info = sourceInfo.lookup(sourceInfo_1.Fields.message.oneof_decl, oneofIndex);
|
|
844
|
+
let outerComments = [];
|
|
845
|
+
(0, utils_1.maybeAddComment)(options, info, outerComments);
|
|
846
|
+
const unionType = (0, ts_poet_1.joinCode)(fields.flatMap((f) => {
|
|
847
|
+
const fieldInfo = sourceInfo.lookup(sourceInfo_1.Fields.message.field, f.index);
|
|
848
|
+
let fieldName = (0, case_1.maybeSnakeToCamel)(f.field.name, options);
|
|
849
|
+
let typeName = (0, types_1.toTypeName)(ctx, messageDesc, f.field);
|
|
844
850
|
let valueName = (0, utils_1.oneofValueName)(fieldName, options);
|
|
845
|
-
|
|
846
|
-
|
|
851
|
+
let fieldComments = [];
|
|
852
|
+
(0, utils_1.maybeAddComment)(options, fieldInfo, fieldComments);
|
|
853
|
+
const combinedComments = fieldComments.join("\n");
|
|
854
|
+
return (0, ts_poet_1.code) `|${combinedComments ? " // " : ""}\n ${combinedComments} { ${mbReadonly}$case: '${fieldName}', ${mbReadonly}${valueName}: ${typeName} }`;
|
|
855
|
+
}));
|
|
847
856
|
const name = (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[oneofIndex].name, options);
|
|
848
|
-
return (0, ts_poet_1.code) `${mbReadonly}${name}
|
|
849
|
-
|
|
850
|
-
// Ideally we'd put the comments for each oneof field next to the anonymous
|
|
851
|
-
// type we've created in the type union above, but ts-poet currently lacks
|
|
852
|
-
// that ability. For now just concatenate all comments into one big one.
|
|
853
|
-
let comments: Array<string> = [];
|
|
854
|
-
const info = sourceInfo.lookup(Fields.message.oneof_decl, oneofIndex);
|
|
855
|
-
maybeAddComment(options, info, (text) => comments.push(text));
|
|
856
|
-
messageDesc.field.forEach((field, index) => {
|
|
857
|
-
if (!isWithinOneOf(field) || field.oneofIndex !== oneofIndex) {
|
|
858
|
-
return;
|
|
859
|
-
}
|
|
860
|
-
const info = sourceInfo.lookup(Fields.message.field, index);
|
|
861
|
-
const name = maybeSnakeToCamel(field.name, options);
|
|
862
|
-
maybeAddComment(options, info, (text) => comments.push(name + '\n' + text));
|
|
857
|
+
return (0, ts_poet_1.joinCode)([...outerComments, (0, ts_poet_1.code) `${mbReadonly}${name}?:`, unionType, (0, ts_poet_1.code) `| ${(0, utils_1.nullOrUndefined)(options)},`], {
|
|
858
|
+
on: "\n",
|
|
863
859
|
});
|
|
864
|
-
if (comments.length) {
|
|
865
|
-
prop = prop.addJavadoc(comments.join('\n'));
|
|
866
|
-
}
|
|
867
|
-
return prop;
|
|
868
|
-
*/
|
|
869
860
|
}
|
|
870
861
|
// Create a function that constructs 'base' instance with default values for decode to use as a prototype
|
|
871
862
|
function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
|
|
@@ -1164,7 +1155,7 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
1164
1155
|
}
|
|
1165
1156
|
chunks.push((0, ts_poet_1.code) `
|
|
1166
1157
|
const buf = reader.skip(tag & 7);
|
|
1167
|
-
|
|
1158
|
+
|
|
1168
1159
|
${unknownFieldsInitializerSnippet}
|
|
1169
1160
|
const list = message._unknownFields${maybeNonNullAssertion}[tag];
|
|
1170
1161
|
|