Skip to content

Commit

Permalink
feat: generate clients
Browse files Browse the repository at this point in the history
  • Loading branch information
castarco committed Apr 15, 2021
1 parent 64e0813 commit 6cb8aa2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 30 deletions.
78 changes: 74 additions & 4 deletions internal/generator/runer_generate_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ func (r *Runner) generateTypescriptServiceDefinitions(generatedFileStream *proto
for _, serviceSpec := range protoFile.Proto.GetService() {
r.generateTypescriptServiceImplementationInterface(generatedFileStream, serviceSpec)
r.generateTypescriptServiceDefinition(generatedFileStream, serviceSpec)
r.generateTypescriptClient(generatedFileStream, serviceSpec)
}
}

func (r *Runner) generateTypescriptServiceImplementationInterface(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto) {
r.P(generatedFileStream, "export interface I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation extends grpc.UntypedServiceImplementation {")
r.P(generatedFileStream, "export interface I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation {")
r.indentLevel += 2

for _, methodSpec := range serviceSpec.GetMethod() {
Expand All @@ -31,12 +32,12 @@ func (r *Runner) generateTypescriptServiceImplementationInterface(generatedFileS

if !clientStream && !serverStream {
r.P(generatedFileStream, methodSpec.GetName()+": grpc.handleUnaryCall<"+inputInterface+", "+outputInterface+">")
} else if clientStream && serverStream {
r.P(generatedFileStream, methodSpec.GetName()+": grpc.handleBidiStreamingCall<"+inputInterface+", "+outputInterface+">")
} else if clientStream {
r.P(generatedFileStream, methodSpec.GetName()+": grpc.handleClientStreamingCall<"+inputInterface+", "+outputInterface+">")
} else if serverStream {
} else { //if serverStream {
r.P(generatedFileStream, methodSpec.GetName()+": grpc.handleServerStreamingCall<"+inputInterface+", "+outputInterface+">")
} else {
r.P(generatedFileStream, methodSpec.GetName()+": grpc.handleBidiStreamingCall<"+inputInterface+", "+outputInterface+">")
}
}

Expand Down Expand Up @@ -85,3 +86,72 @@ func (r *Runner) generateTypescriptServiceDefinitionMethod(generatedFileStream *
r.indentLevel -= 2
r.P(generatedFileStream, "},")
}

func (r *Runner) generateTypescriptClient(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto) {
r.P(
generatedFileStream,
"export class "+strcase.ToCamel(serviceSpec.GetName())+"Client",
"extends joinGRPC.Client<grpc.ServiceDefinition<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation>> ",
"implements joinGRPC.IExtendedClient<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation> {",
)
r.indentLevel += 2

for _, methodSpec := range serviceSpec.GetMethod() {
r.generateTypescriptClientMethod(generatedFileStream, serviceSpec, methodSpec)
}

r.indentLevel -= 2
r.P(generatedFileStream, "}\n")
}

func (r *Runner) generateTypescriptClientMethod(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto, methodSpec *descriptorpb.MethodDescriptorProto) {
// Function's Signature
methodName := strcase.ToCamel(methodSpec.GetName())
r.P(generatedFileStream, "public "+methodName+"(")
r.indentLevel += 2

inputTypeName := methodSpec.GetInputType()
inputInterface := r.getEnumOrMessageTypeName(inputTypeName, true)
outputTypeName := methodSpec.GetOutputType()
outputInterface := r.getEnumOrMessageTypeName(outputTypeName, true)

clientStream := methodSpec.GetClientStreaming()
serverStrean := methodSpec.GetServerStreaming()

if !clientStream {
r.P(generatedFileStream, "request: "+inputInterface+",")
}

r.P(generatedFileStream,
"metadata?: Record<string, string>,",
"options?: grpc.CallOptions,",
)

var returnType string
if clientStream && serverStrean {
returnType = "grpc.ClientDuplexStream<" + inputInterface + ", " + outputInterface + ">"
} else if !clientStream && !serverStrean {
returnType = "joinGRPC.IUnaryRequest<" + outputInterface + ">"
} else if clientStream {
returnType = "joinGRPC.IClientStreamRequest<" + inputInterface + ", " + outputInterface + ">"
} else { // if serverStream
returnType = "grpc.ClientReadableStream<" + outputInterface + ">"
}

r.indentLevel -= 2
r.P(generatedFileStream, "): "+returnType+" {")
r.indentLevel += 2

if clientStream && serverStrean {
r.P(generatedFileStream, "return this.makeBidiStreamRequest('"+methodName+"', metadata, options)")
} else if !clientStream && !serverStrean {
r.P(generatedFileStream, "return this.makeUnaryRequest('"+methodName+"', request, metadata, options)")
} else if clientStream {
r.P(generatedFileStream, "return this.makeClientStreamRequest('"+methodName+"', metadata, options)")
} else { // if serverStream
r.P(generatedFileStream, "return this.makeServerStreamRequest('"+methodName+"', request, metadata, options)")
}

r.indentLevel -= 2
r.P(generatedFileStream, "}\n")
}
44 changes: 41 additions & 3 deletions tests/__tests__/v2/generated/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ export namespace Foo {
}
}

export interface IUsersServiceImplementation
extends grpc.UntypedServiceImplementation {
export interface IUsersServiceImplementation {
Find: grpc.handleUnaryCall<IRequest, Common_Common.IOtherPkgMessage>
FindClientStream: grpc.handleClientStreamingCall<
IRequest,
Expand All @@ -425,7 +424,7 @@ export namespace Foo {
IRequest,
Common_Common.IOtherPkgMessage
>
FindBidiStream: grpc.handleClientStreamingCall<
FindBidiStream: grpc.handleBidiStreamingCall<
IRequest,
Common_Common.IOtherPkgMessage
>
Expand Down Expand Up @@ -485,4 +484,43 @@ export namespace Foo {
responseDeserialize: Common_Common.OtherPkgMessage.decodePatched,
},
}

export class UsersClient
extends joinGRPC.Client<grpc.ServiceDefinition<IUsersServiceImplementation>>
implements joinGRPC.IExtendedClient<IUsersServiceImplementation> {
public Find(
request: IRequest,
metadata?: Record<string, string>,
options?: grpc.CallOptions
): joinGRPC.IUnaryRequest<Common_Common.IOtherPkgMessage> {
return this.makeUnaryRequest('Find', request, metadata, options)
}

public FindClientStream(
metadata?: Record<string, string>,
options?: grpc.CallOptions
): joinGRPC.IClientStreamRequest<IRequest, Common_Common.IOtherPkgMessage> {
return this.makeClientStreamRequest('FindClientStream', metadata, options)
}

public FindServerStream(
request: IRequest,
metadata?: Record<string, string>,
options?: grpc.CallOptions
): grpc.ClientReadableStream<Common_Common.IOtherPkgMessage> {
return this.makeServerStreamRequest(
'FindServerStream',
request,
metadata,
options
)
}

public FindBidiStream(
metadata?: Record<string, string>,
options?: grpc.CallOptions
): grpc.ClientDuplexStream<IRequest, Common_Common.IOtherPkgMessage> {
return this.makeBidiStreamRequest('FindBidiStream', metadata, options)
}
}
}
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"@join-com/gcloud-logger-trace": "^0.1.17",
"@join-com/grpc": "^0.1.0",
"@join-com/grpc": "^0.1.1",
"@join-com/grpc-ts": "^2.0.2",
"grpc": "^1.24.6",
"protobufjs": "^6.10.2"
Expand Down
45 changes: 23 additions & 22 deletions tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,13 @@
dependencies:
grpc "^1.24.5"

"@join-com/grpc@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@join-com/grpc/-/grpc-0.1.0.tgz#8cfe0c51fd4aed9a7504605962d927e819ba9379"
integrity sha512-x+IWclVMEPSKAjPiC8LUmfUzI8MU3UPwIuBAnxS1JzyQZOFV44gu21qheVcYMmDDN0XxKtT9x79m1IIrHVnGYQ==
"@join-com/grpc@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@join-com/grpc/-/grpc-0.1.1.tgz#e8e8631d4e0075de014cec9fe6b22d77da69e667"
integrity sha512-haZ92zW9oxMl2Rw3DmIPgo6eOzOocjwxrRGJfMkANdH46Jo/4WnxrCSOc/Z/MH0chpEnf1zbCD87TdGiUkKfKA==
dependencies:
"@grpc/grpc-js" "^1.2.12"
protobufjs "^6.10.2"

"@join-com/node-trace@^0.1.8":
version "0.1.8"
Expand Down Expand Up @@ -786,19 +787,19 @@
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==

"@types/node@*", "@types/node@>=12.12.47", "@types/node@^14.14.37":
version "14.14.37"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==
version "14.14.40"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.40.tgz#05a7cd31154487f357ca0bec4334ed1b1ab825a0"
integrity sha512-2HoZZGylcnz19ZSbvWhgWHqvprw1ZGHanxIrDWYykPD4CauLW4gcyLzCVfUN2kv/1t1F3CurQIdi+s1l9+XgEA==

"@types/node@^10.3.4":
version "10.17.56"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.56.tgz#010c9e047c3ff09ddcd11cbb6cf5912725cdc2b3"
integrity sha512-LuAa6t1t0Bfw4CuSR0UITsm1hP17YL+u82kfHGrHUWdhlBtH7sa7jGY5z7glGaIj/WDYDkRtgGd+KCjCzxBW1w==
version "10.17.57"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.57.tgz#b99c5dbd97ad1af6b912fc6594d3c0e1e1b01423"
integrity sha512-9ejqfD/nkpl2RTUByUnkhE1xQFw6NWBE/CVsMuKnUvHRGm+HKFvSdHoyuJqKpG/N0hX7i3QHuf+OddN5WIHxMQ==

"@types/node@^13.7.0":
version "13.13.48"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.48.tgz#46a3df718aed5217277f2395a682e055a487e341"
integrity sha512-z8wvSsgWQzkr4sVuMEEOvwMdOQjiRY2Y/ZW4fDfjfe3+TfQrZqFKOthBgk2RnVEmtOKrkwdZ7uTvsxTBLjKGDQ==
version "13.13.49"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.49.tgz#c274f1d842879082237f700a0e3140f875764aa5"
integrity sha512-v5fPzKjPAZ33/G4X8EXvGlbHFKQClfKAz1bKF/+cKaWss9lAIqrOETfcFNL3xG+DG2VCEev+dK4/lCa+YZaxBA==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
Expand Down Expand Up @@ -1736,9 +1737,9 @@ [email protected]:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

electron-to-chromium@^1.3.712:
version "1.3.713"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.713.tgz#4583efb17f2d1e9ec07a44c8004ea73c013ad146"
integrity sha512-HWgkyX4xTHmxcWWlvv7a87RHSINEcpKYZmDMxkUlHcY+CJcfx7xEfBHuXVsO1rzyYs1WQJ7EgDp2CoErakBIow==
version "1.3.717"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.717.tgz#78d4c857070755fb58ab64bcc173db1d51cbc25f"
integrity sha512-OfzVPIqD1MkJ7fX+yTl2nKyOE4FReeVfMCzzxQS+Kp43hZYwHwThlGP+EGIZRXJsxCM7dqo8Y65NOX/HP12iXQ==

emittery@^0.7.1:
version "0.7.2"
Expand Down Expand Up @@ -4798,9 +4799,9 @@ symbol-tree@^3.2.4:
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

table@^6.0.4:
version "6.0.9"
resolved "https://registry.yarnpkg.com/table/-/table-6.0.9.tgz#790a12bf1e09b87b30e60419bafd6a1fd85536fb"
integrity sha512-F3cLs9a3hL1Z7N4+EkSscsel3z55XT950AvB05bwayrNg5T1/gykXtigioTAjbltvbMSJvvhFCbnf6mX+ntnJQ==
version "6.1.0"
resolved "https://registry.yarnpkg.com/table/-/table-6.1.0.tgz#676a0cfb206008b59e783fcd94ef8ba7d67d966c"
integrity sha512-T4G5KMmqIk6X87gLKWyU5exPpTjLjY5KyrFWaIjv3SvgaIUGXV7UEzGEnZJdTA38/yUS6f9PlKezQ0bYXG3iIQ==
dependencies:
ajv "^8.0.1"
is-boolean-object "^1.1.0"
Expand Down Expand Up @@ -4919,9 +4920,9 @@ tr46@^2.0.2:
punycode "^2.1.1"

ts-jest@^26.5.4:
version "26.5.4"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.5.4.tgz#207f4c114812a9c6d5746dd4d1cdf899eafc9686"
integrity sha512-I5Qsddo+VTm94SukBJ4cPimOoFZsYTeElR2xy6H2TOVs+NsvgYglW8KuQgKoApOKuaU/Ix/vrF9ebFZlb5D2Pg==
version "26.5.5"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.5.5.tgz#e40481b6ee4dd162626ba481a2be05fa57160ea5"
integrity sha512-7tP4m+silwt1NHqzNRAPjW1BswnAhopTdc2K3HEkRZjF0ZG2F/e/ypVH0xiZIMfItFtD3CX0XFbwPzp9fIEUVg==
dependencies:
bs-logger "0.x"
buffer-from "1.x"
Expand Down

0 comments on commit 6cb8aa2

Please sign in to comment.