From 45a9969171e4faaa30dbb90b47d51c7ab635aa05 Mon Sep 17 00:00:00 2001 From: Yo-mah-Ya Date: Sat, 7 Oct 2023 16:03:10 -0700 Subject: [PATCH 1/2] add return type to toObject method --- src/descriptor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/descriptor.ts b/src/descriptor.ts index f70441de..070ede02 100644 --- a/src/descriptor.ts +++ b/src/descriptor.ts @@ -499,7 +499,7 @@ function createToObject( undefined, undefined, [], - undefined, + createPrimitiveMessageSignature(rootDescriptor, messageDescriptor), ts.factory.createBlock(statements, true), ); } From d768557945fa3136f78303defee07c1d9f4c853b Mon Sep 17 00:00:00 2001 From: Yo-mah-Ya Date: Sat, 7 Oct 2023 16:40:37 -0700 Subject: [PATCH 2/2] add type alias to reduce duplicated object types --- src/descriptor.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/descriptor.ts b/src/descriptor.ts index 070ede02..7ebce117 100644 --- a/src/descriptor.ts +++ b/src/descriptor.ts @@ -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"); @@ -297,7 +298,9 @@ function createFromObject( undefined, dataIdentifier, undefined, - createPrimitiveMessageSignature(rootDescriptor, messageDescriptor), + ts.factory.createTypeReferenceNode( + ts.factory.createIdentifier(objectTypeName) + ), ), ], ts.factory.createTypeReferenceNode( @@ -311,6 +314,7 @@ function createFromObject( function createToObject( rootDescriptor: descriptor.FileDescriptorProto, messageDescriptor: descriptor.DescriptorProto, + objectTypeName: string ): ts.MethodDeclaration { const statements = []; const properties = []; @@ -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), ), ], @@ -499,7 +505,9 @@ function createToObject( undefined, undefined, [], - createPrimitiveMessageSignature(rootDescriptor, messageDescriptor), + ts.factory.createTypeReferenceNode( + ts.factory.createIdentifier(objectTypeName) + ), ts.factory.createBlock(statements, true), ); } @@ -2182,6 +2190,7 @@ function createMessage( messageDescriptor: descriptor.DescriptorProto, pbIdentifier: ts.Identifier, parentName: string, + objectTypeName: string ): ts.ClassDeclaration { const statements: ts.ClassElement[] = [ createOneOfDecls(messageDescriptor), @@ -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( @@ -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), ]; const namespacedStatements: ts.Statement[] = [];