Skip to content

Commit

Permalink
Merge pull request #31 from join-com/join-11663-fix-protobufjs-duplic…
Browse files Browse the repository at this point in the history
…ate-name

[JOIN-11663] fix protobufjs duplicate name
  • Loading branch information
castarco authored Apr 28, 2021
2 parents e1256d9 + f491636 commit ac837de
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 227 deletions.
2 changes: 1 addition & 1 deletion internal/generator/runner_generate_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *Runner) generateTypescriptMessageClass(generatedFileStream *protogen.Ge
}
r.P(
generatedFileStream,
"@protobufjs.Type.d('"+className+"')",
"@protobufjs.Type.d('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')",
"export class "+className+" extends protobufjs.Message<"+className+"> implements "+implementedInterfaces+" {\n",
)
r.indentLevel += 2
Expand Down
15 changes: 10 additions & 5 deletions internal/generator/runner_generate_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (r *Runner) generateTypescriptClientInterfaceMethod(generatedFileStream *pr
}

func (r *Runner) generateTypescriptClientClass(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto) {
serviceName := serviceSpec.GetName()

serviceOptions := serviceSpec.GetOptions()
if serviceOptions != nil {
if serviceOptions.GetDeprecated() {
Expand All @@ -89,16 +91,19 @@ func (r *Runner) generateTypescriptClientClass(generatedFileStream *protogen.Gen
}
r.P(
generatedFileStream,
"export class "+strcase.ToCamel(serviceSpec.GetName())+"Client",
"extends joinGRPC.Client<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation, '"+r.currentPackage+"."+serviceSpec.GetName()+"'> ",
"implements I"+strcase.ToCamel(serviceSpec.GetName())+"Client {",
"export class "+strcase.ToCamel(serviceName)+"Client",
"extends joinGRPC.Client<I"+strcase.ToCamel(serviceName)+"ServiceImplementation, '"+r.currentPackage+"."+serviceName+"'> ",
"implements I"+strcase.ToCamel(serviceName)+"Client {",
)
r.indentLevel += 2

r.P(generatedFileStream, "constructor(public readonly config: joinGRPC.IClientConfig<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation>) {")
r.P(
generatedFileStream,
"constructor(config: Omit<joinGRPC.IClientConfig<I"+strcase.ToCamel(serviceName)+"ServiceImplementation>, 'serviceDefinition'>) {",
)
r.indentLevel += 2

r.P(generatedFileStream, "super(config, '"+r.currentPackage+"."+serviceSpec.GetName()+"')")
r.P(generatedFileStream, "super({ ...config, serviceDefinition: "+strcase.ToLowerCamel(serviceName)+"ServiceDefinition }, '"+r.currentPackage+"."+serviceName+"')")

r.indentLevel -= 2
r.P(generatedFileStream, "}\n")
Expand Down
14 changes: 10 additions & 4 deletions tests/__tests__/generated/Flavors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Flavors {
emails: Email[]
}

@protobufjs.Type.d('UserProfile')
@protobufjs.Type.d('flavors_UserProfile')
export class UserProfile
extends protobufjs.Message<UserProfile>
implements ConvertibleTo<IUserProfile>, IUserProfile {
Expand Down Expand Up @@ -76,7 +76,7 @@ export namespace Flavors {
}
}

@protobufjs.Type.d('UserRequest')
@protobufjs.Type.d('flavors_UserRequest')
export class UserRequest
extends protobufjs.Message<UserRequest>
implements ConvertibleTo<IUserRequest>, IUserRequest {
Expand Down Expand Up @@ -161,9 +161,15 @@ export namespace Flavors {
extends joinGRPC.Client<IUsersServiceImplementation, 'flavors.Users'>
implements IUsersClient {
constructor(
public readonly config: joinGRPC.IClientConfig<IUsersServiceImplementation>
config: Omit<
joinGRPC.IClientConfig<IUsersServiceImplementation>,
'serviceDefinition'
>
) {
super(config, 'flavors.Users')
super(
{ ...config, serviceDefinition: usersServiceDefinition },
'flavors.Users'
)
}

public Find(
Expand Down
20 changes: 13 additions & 7 deletions tests/__tests__/generated/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export namespace Foo {
optionalField?: number
}

@protobufjs.Type.d('CustomOptionsTest')
@protobufjs.Type.d('foo_CustomOptionsTest')
export class CustomOptionsTest
extends protobufjs.Message<CustomOptionsTest>
implements ConvertibleTo<ICustomOptionsTest> {
Expand Down Expand Up @@ -157,7 +157,7 @@ export namespace Foo {
/**
* @deprecated
*/
@protobufjs.Type.d('Nested')
@protobufjs.Type.d('foo_Nested')
export class Nested
extends protobufjs.Message<Nested>
implements ConvertibleTo<INested>, INested {
Expand Down Expand Up @@ -188,7 +188,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('Request')
@protobufjs.Type.d('foo_Request')
export class Request
extends protobufjs.Message<Request>
implements ConvertibleTo<IRequest>, IRequest {
Expand Down Expand Up @@ -219,7 +219,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('RequiredPropertiesTest')
@protobufjs.Type.d('foo_RequiredPropertiesTest')
export class RequiredPropertiesTest
extends protobufjs.Message<RequiredPropertiesTest>
implements ConvertibleTo<IRequiredPropertiesTest>, IRequiredPropertiesTest {
Expand Down Expand Up @@ -270,7 +270,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('Test')
@protobufjs.Type.d('foo_Test')
export class Test
extends protobufjs.Message<Test>
implements ConvertibleTo<ITest> {
Expand Down Expand Up @@ -587,9 +587,15 @@ export namespace Foo {
extends joinGRPC.Client<IUsersServiceImplementation, 'foo.Users'>
implements IUsersClient {
constructor(
public readonly config: joinGRPC.IClientConfig<IUsersServiceImplementation>
config: Omit<
joinGRPC.IClientConfig<IUsersServiceImplementation>,
'serviceDefinition'
>
) {
super(config, 'foo.Users')
super(
{ ...config, serviceDefinition: usersServiceDefinition },
'foo.Users'
)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/__tests__/generated/common/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export namespace Common {
latsName?: string
}

@protobufjs.Type.d('OtherPkgMessage')
@protobufjs.Type.d('common_OtherPkgMessage')
export class OtherPkgMessage
extends protobufjs.Message<OtherPkgMessage>
implements ConvertibleTo<IOtherPkgMessage>, IOtherPkgMessage {
Expand Down
2 changes: 1 addition & 1 deletion tests/__tests__/generated/common/Extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export namespace Common {
birthDate?: Date
}

@protobufjs.Type.d('ExtraPkgMessage')
@protobufjs.Type.d('common_ExtraPkgMessage')
export class ExtraPkgMessage
extends protobufjs.Message<ExtraPkgMessage>
implements ConvertibleTo<IExtraPkgMessage> {
Expand Down
2 changes: 1 addition & 1 deletion tests/__tests__/generated/google/protobuf/Timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export namespace GoogleProtobuf {
nanos?: number
}

@protobufjs.Type.d('Timestamp')
@protobufjs.Type.d('google_protobuf_Timestamp')
export class Timestamp
extends protobufjs.Message<Timestamp>
implements ConvertibleTo<ITimestamp>, ITimestamp {
Expand Down
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"@coderspirit/nominal": "^3.0.0",
"@join-com/gcloud-logger-trace": "^0.1.17",
"@join-com/grpc": "^0.3.7",
"@join-com/grpc": "^0.3.9",
"@join-com/grpc-ts": "^2.0.2",
"grpc": "^1.24.7",
"protobufjs": "^6.10.2"
Expand Down
Loading

0 comments on commit ac837de

Please sign in to comment.