Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type alias to toObject method to prevent from inferred type too long error when compiling #232

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function createEnum(
function createFromObject(
rootDescriptor: descriptor.FileDescriptorProto,
messageDescriptor: descriptor.DescriptorProto,
objectTypeName: string,
parentName: string = ''
): ts.MethodDeclaration {
const dataIdentifier = ts.factory.createIdentifier("data");
Expand Down Expand Up @@ -297,7 +298,9 @@ function createFromObject(
undefined,
dataIdentifier,
undefined,
createPrimitiveMessageSignature(rootDescriptor, messageDescriptor),
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(objectTypeName)
),
),
],
ts.factory.createTypeReferenceNode(
Expand All @@ -311,6 +314,7 @@ function createFromObject(
function createToObject(
rootDescriptor: descriptor.FileDescriptorProto,
messageDescriptor: descriptor.DescriptorProto,
objectTypeName: string
): ts.MethodDeclaration {
const statements = [];
const properties = [];
Expand Down Expand Up @@ -481,7 +485,9 @@ function createToObject(
ts.factory.createVariableDeclaration(
"data",
undefined,
createPrimitiveMessageSignature(rootDescriptor, messageDescriptor),
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(objectTypeName)
),
ts.factory.createObjectLiteralExpression(properties, true),
),
],
Expand All @@ -499,7 +505,9 @@ function createToObject(
undefined,
undefined,
[],
undefined,
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(objectTypeName)
),
ts.factory.createBlock(statements, true),
);
}
Expand Down Expand Up @@ -2182,6 +2190,7 @@ function createMessage(
messageDescriptor: descriptor.DescriptorProto,
pbIdentifier: ts.Identifier,
parentName: string,
objectTypeName: string
): ts.ClassDeclaration {
const statements: ts.ClassElement[] = [
createOneOfDecls(messageDescriptor),
Expand Down Expand Up @@ -2209,9 +2218,9 @@ function createMessage(

statements.push(
// Create fromObject method
createFromObject(rootDescriptor, messageDescriptor, parentName),
createFromObject(rootDescriptor, messageDescriptor, objectTypeName, parentName),
// Create toObject method
createToObject(rootDescriptor, messageDescriptor),
createToObject(rootDescriptor, messageDescriptor, objectTypeName),
);

statements.push(
Expand Down Expand Up @@ -2258,8 +2267,16 @@ export function processDescriptorRecursively(
no_namespace: boolean,
parentName: string = '',
): ts.Statement[] {
const objectTypeName = `ObjectTypeFor${parentName}${descriptor.name}`;
const statements: ts.Statement[] = [
createMessage(rootDescriptor, descriptor, pbIdentifier, parentName),
ts.factory.createTypeAliasDeclaration(
undefined,
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier(objectTypeName),
undefined,
createPrimitiveMessageSignature(rootDescriptor, descriptor),
),
createMessage(rootDescriptor, descriptor, pbIdentifier, parentName, objectTypeName),
Comment on lines +2270 to +2279
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of toObject and the parameter of fromObject are supposed to be compatible.

Aside from preventing from duplicated source codes, we might wanna use only object type other than protocol Buffer. For example, this open source project, which provides PostgreSQL parser, uses .proto file as a common language across several languages. So we actually need only data types corresponding to .proto file, instead of serializing/deserializing Protocol buffer and whatnot.

];

const namespacedStatements: ts.Statement[] = [];
Expand Down