Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from finos-plexus/db-contrib/db_contrib_develop
Browse files Browse the repository at this point in the history
Generic Client API Extensions, reduced TS code generation output
  • Loading branch information
udalmik authored Jul 18, 2018
2 parents 7caeae7 + 37064d7 commit ef59e88
Show file tree
Hide file tree
Showing 57 changed files with 1,075 additions and 12,380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ class RulesModule extends AbstractModule {
rulesBinder.addBinding().to(typeof(NoMethodsDeletedRule))
rulesBinder.addBinding().to(typeof(NoMethodTypesChangedRule))
rulesBinder.addBinding().to(typeof(NoServicesDeletedRule))
rulesBinder.addBinding().to(typeof(NoAppsDeletedRule))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class TypescriptApplicationApiGenerator extends ApplicationCodeGenerator {
/**
* Main client API
*
*/
export interface «app.name»Client extends GenericClientApi {
Expand All @@ -97,7 +96,6 @@ export interface «app.name»Client extends GenericClientApi {
/**
* Client's API internal implementation
*
*/
class «app.name»ClientImpl extends GenericClientApiBase implements «app.name»Client {

Expand All @@ -121,48 +119,28 @@ class «app.name»ClientImpl extends GenericClientApiBase implements «app.name
«FOR providedService : providedServices SEPARATOR '\n' »
/**
* Client invocation handler for «providedService.aliasOrName.toFirstUpper», to be implemented by Client
*
*/
export abstract class «providedService.aliasOrName.toFirstUpper»InvocationHandler {

«FOR providedMethod : providedService.methods»
public abstract «clientHandlerSignature(providedMethod.method, genConfig)»;

«ENDFOR»
}
«ENDFOR»

«FOR providedService : providedServices SEPARATOR '\n' »
/**
* Internal invocation handler delegate for «providedService.aliasOrName.toFirstUpper»
*
*/
class «providedService.aliasOrName.toFirstUpper»InvocationHandlerInternal {

public constructor(private readonly clientHandler: «providedService.aliasOrName.toFirstUpper»InvocationHandler) {}

«FOR providedMethod : providedService.methods SEPARATOR '\n'»
public «genericClientHandlerSignature(providedMethod.method, genConfig)» {
«handlerMethodImpl(providedMethod.method, genConfig)»
}
public abstract «clientHandlerSignature(providedMethod.method, genConfig)»;
«ENDFOR»
}
«ENDFOR»

/**
* Client API builder
*
*/
export class «app.name»ClientBuilder {

private clientDetails: ClientConnectRequest = {
applicationId: "«app.fullName»"
applicationId: '«app.fullName»'
};

private transportConnectionProvider: () => Promise<TransportConnection>;

«FOR providedElement : providedServices SEPARATOR '\n' »
private «providedElement.aliasOrName.toFirstLower»Handler: «providedElement.aliasOrName.toFirstUpper»InvocationHandlerInternal;
private «providedElement.aliasOrName.toFirstLower»Handler: «providedElement.aliasOrName.toFirstUpper»InvocationHandler;
«ENDFOR»

public withClientDetails(clientId: ClientConnectRequest): «app.name»ClientBuilder {
Expand All @@ -182,7 +160,7 @@ export class «app.name»ClientBuilder {

«FOR providedMethod : providedServices SEPARATOR '\n' »
public with«providedMethod.aliasOrName.toFirstUpper»InvocationsHandler(invocationsHandler: «providedMethod.aliasOrName.toFirstUpper»InvocationHandler): «app.name»ClientBuilder {
this.«providedMethod.aliasOrName.toFirstLower»Handler = new «providedMethod.aliasOrName.toFirstUpper»InvocationHandlerInternal(invocationsHandler);
this.«providedMethod.aliasOrName.toFirstLower»Handler = invocationsHandler;
return this;
}
«ENDFOR»
Expand All @@ -202,7 +180,7 @@ export class «app.name»ClientBuilder {
«ENDFOR»
«ENDFOR»
.connect()
.then(genericClient => new «app.name»ClientImpl(
.then((genericClient: GenericClientApi) => new «app.name»ClientImpl(
genericClient«IF !consumedServices.isEmpty»,«ENDIF»
«FOR consumedService : consumedServices SEPARATOR ","»
new «consumedService.aliasOrName.toFirstUpper»ProxyImpl(genericClient)
Expand All @@ -215,45 +193,43 @@ export class «app.name»ClientBuilder {
def invocationHandlerBuilder(Method rpcMethod, ProvidedService providedService, PlexusGenConfig genConfig) {
switch (rpcMethod) {
case rpcMethod.isPointToPoint: '''
.withUnaryInvocationHandler({
.withTypeAwareUnaryHandler({
«handlerBuilderParam(rpcMethod, providedService, genConfig)»
})
}, «requestTypeImpl(rpcMethod, genConfig)», «responseTypeImpl(rpcMethod, genConfig)»)
'''
case rpcMethod.isBidiStreaming
|| rpcMethod.isClientStreaming: '''
.withBidiStreamingInvocationHandler({
.withTypeAwareBidiStreamingHandler({
«handlerBuilderParam(rpcMethod, providedService, genConfig)»
})
}, «requestTypeImpl(rpcMethod, genConfig)», «responseTypeImpl(rpcMethod, genConfig)»)
'''
case rpcMethod.isServerStreaming: '''
.withServerStreamingInvocationHandler({
.withTypeAwareServerStreamingHandler({
«handlerBuilderParam(rpcMethod, providedService, genConfig)»
})
}, «requestTypeImpl(rpcMethod, genConfig)», «responseTypeImpl(rpcMethod, genConfig)»)
'''
}
}
def handlerBuilderParam(Method rpcMethod, ProvidedService providedService, PlexusGenConfig genConfig) {
return '''
serviceInfo: {
serviceId: "«rpcMethod.service.fullName»"«IF providedService.alias !== null»,
serviceAlias: "«providedService.alias»"«ENDIF»
serviceId: '«rpcMethod.service.fullName»'«IF providedService.alias !== null»,
serviceAlias: '«providedService.alias»'«ENDIF»
},
handler: {
methodId: "«rpcMethod.name»",
handle: this.«providedService.aliasOrName.toFirstLower»Handler.on«rpcMethod.name».bind(this.«providedService.aliasOrName.toFirstLower»Handler)
}
methodId: '«rpcMethod.name»',
handle: this.«providedService.aliasOrName.toFirstLower»Handler.on«rpcMethod.name».bind(this.«providedService.aliasOrName.toFirstLower»Handler)
'''
}
def imports(PlexusGenConfig genConfig) '''
import { MethodInvocationContext, Completion, ClientConnectRequest, StreamingInvocationClient, GenericClientApi, InvocationRequestInfo, InvocationClient, GenericRequest, GenericClientApiBase } from "@plexus-interop/client";
import { ProvidedMethodReference, ServiceDiscoveryRequest, ServiceDiscoveryResponse, MethodDiscoveryRequest, MethodDiscoveryResponse, GenericClientApiBuilder, ValueHandler } from "@plexus-interop/client";
import { TransportConnection, UniqueId } from "@plexus-interop/transport-common";
import { Arrays, Observer } from "@plexus-interop/common";
import { InvocationObserver, InvocationObserverConverter, ContainerAwareClientAPIBuilder } from "@plexus-interop/client";
import { MethodInvocationContext, Completion, ClientConnectRequest, StreamingInvocationClient, GenericClientApi, InvocationRequestInfo, InvocationClient, GenericRequest, GenericClientApiBase } from '@plexus-interop/client';
import { ProvidedMethodReference, ServiceDiscoveryRequest, ServiceDiscoveryResponse, MethodDiscoveryRequest, MethodDiscoveryResponse, GenericClientApiBuilder, ValueHandler } from '@plexus-interop/client';
import { TransportConnection, UniqueId } from '@plexus-interop/transport-common';
import { Arrays, Observer } from '@plexus-interop/common';
import { InvocationObserver, InvocationObserverConverter, ContainerAwareClientAPIBuilder } from '@plexus-interop/client';

import * as plexus from "«genConfig.getExternalDependencies().get(0)»";
import * as plexus from '«genConfig.getExternalDependencies().get(0)»';
'''
def clientMethodSignature(ConsumedMethod methodLink, PlexusGenConfig genConfig) {
Expand Down Expand Up @@ -294,67 +270,47 @@ import * as plexus from "«genConfig.getExternalDependencies().get(0)»";
def clientPointToPointImpl(ConsumedMethod consumed, ConsumedService consumedService, PlexusGenConfig genConfig) {
val rpcMethod = consumed.method
return '''
«clientConverters(rpcMethod, genConfig)»
«clientInvocationInfo(consumed, consumedService, genConfig)»
return new Promise((resolve, reject) => {
this.genericClient.sendRawUnaryRequest(invocationInfo, requestToBinaryConverter(request), {
value: (responsePayload: ArrayBuffer) => {
resolve(responseFromBinaryConverter(responsePayload));
},
error: (e) => {
reject(e);
}
});
this.genericClient.sendUnaryRequest(invocationInfo, request, {
value: responsePayload => resolve(responsePayload),
error: e => reject(e)
}, «requestTypeImpl(rpcMethod, genConfig)», «responseTypeImpl(rpcMethod, genConfig)»);
});
'''
}
def clientBidiStreamingImpl(ConsumedMethod consumed, ConsumedService consumedService, PlexusGenConfig genConfig) {
val rpcMethod = consumed.method
return '''
«clientConverters(rpcMethod, genConfig)»
«clientInvocationInfo(consumed, consumedService, genConfig)»
return this.genericClient.sendRawBidirectionalStreamingRequest(
return this.genericClient.sendBidirectionalStreamingRequest(
invocationInfo,
new InvocationObserverConverter<«responseType(rpcMethod, genConfig)», ArrayBuffer>(responseObserver, responseFromBinaryConverter))
.then(baseClient => {
return {
next: (request: «requestType(rpcMethod, genConfig)») => baseClient.next(requestToBinaryConverter(request)),
error: baseClient.error.bind(baseClient),
complete: baseClient.complete.bind(baseClient),
cancel: baseClient.cancel.bind(baseClient)
};
});
responseObserver,
«requestTypeImpl(rpcMethod, genConfig)», «responseTypeImpl(rpcMethod, genConfig)»);
'''
}
def serverStreamingImpl(ConsumedMethod consumed, ConsumedService consumedService, PlexusGenConfig genConfig) {
val rpcMethod = consumed.method
return '''
«clientConverters(rpcMethod, genConfig)»
«clientInvocationInfo(consumed, consumedService, genConfig)»
return this.genericClient.sendRawServerStreamingRequest(
return this.genericClient.sendServerStreamingRequest(
invocationInfo,
requestToBinaryConverter(request),
new InvocationObserverConverter<«responseType(rpcMethod, genConfig)», ArrayBuffer>(responseObserver, responseFromBinaryConverter));
request,
responseObserver,
«requestTypeImpl(rpcMethod, genConfig)», «responseTypeImpl(rpcMethod, genConfig)»
);
'''
}
def clientConverters(Method rpcMethod, PlexusGenConfig genConfig) '''
const requestToBinaryConverter = (from: «requestType(rpcMethod, genConfig)») => Arrays.toArrayBuffer(«requestTypeImpl(rpcMethod, genConfig)».encode(from).finish());
const responseFromBinaryConverter = (from: ArrayBuffer) => {
const decoded = «responseTypeImpl(rpcMethod, genConfig)».decode(new Uint8Array(from));
return «responseTypeImpl(rpcMethod, genConfig)».toObject(decoded);
};
'''
def clientInvocationInfo(ConsumedMethod consumed, ConsumedService consumedService, PlexusGenConfig genConfig) {
val rpcMethod = consumed.method
return '''
const invocationInfo: InvocationRequestInfo = {
methodId: "«rpcMethod.name»",
serviceId: "«rpcMethod.service.fullName»"«IF consumedService.alias !== null»,
serviceAlias: "«consumedService.alias»"«ENDIF»
methodId: '«rpcMethod.name»',
serviceId: '«rpcMethod.service.fullName»'«IF consumedService.alias !== null»,
serviceAlias: '«consumedService.alias»'«ENDIF»
};
'''
}
Expand All @@ -377,55 +333,4 @@ import * as plexus from "«genConfig.getExternalDependencies().get(0)»";
}
}
def handlerMethodImpl(Method rpcMethod, PlexusGenConfig genConfig) {
switch (rpcMethod) {
case rpcMethod.isPointToPoint: handlerPointToPointImpl(rpcMethod, genConfig)
case rpcMethod.isBidiStreaming
|| rpcMethod.isClientStreaming: handlerBidiStreamingImpl(rpcMethod, genConfig)
case rpcMethod.isServerStreaming: handlerServerStreamingImpl(rpcMethod, genConfig)
}
}
def handlerPointToPointImpl(Method rpcMethod, PlexusGenConfig genConfig) '''
«handlerConverters(rpcMethod, genConfig)»
return this.clientHandler
.on«rpcMethod.name»(invocationContext, requestFromBinaryConverter(request))
.then(response => responseToBinaryConverter(response));
'''
def handlerBidiStreamingImpl(Method rpcMethod, PlexusGenConfig genConfig) '''
«handlerConverters(rpcMethod, genConfig)»
const baseObserver = this.clientHandler
.on«rpcMethod.name»(invocationContext, {
next: (response) => hostClient.next(responseToBinaryConverter(response)),
complete: hostClient.complete.bind(hostClient),
error: hostClient.error.bind(hostClient),
cancel: hostClient.cancel.bind(hostClient)
});
return {
next: (value) => baseObserver.next(requestFromBinaryConverter(value)),
complete: baseObserver.complete.bind(baseObserver),
error: baseObserver.error.bind(baseObserver),
streamCompleted: baseObserver.streamCompleted.bind(baseObserver)
};
'''
def handlerServerStreamingImpl(Method rpcMethod, PlexusGenConfig genConfig) '''
«handlerConverters(rpcMethod, genConfig)»
this.clientHandler
.on«rpcMethod.name»(invocationContext, requestFromBinaryConverter(request), {
next: (response) => hostClient.next(responseToBinaryConverter(response)),
complete: hostClient.complete.bind(hostClient),
error: hostClient.error.bind(hostClient),
cancel: hostClient.cancel.bind(hostClient)
});
'''
def handlerConverters(Method rpcMethod, PlexusGenConfig genConfig) '''
const responseToBinaryConverter = (from: «responseType(rpcMethod, genConfig)») => Arrays.toArrayBuffer(«responseTypeImpl(rpcMethod, genConfig)».encode(from).finish());
const requestFromBinaryConverter = (from: ArrayBuffer) => {
const decoded = «requestTypeImpl(rpcMethod, genConfig)».decode(new Uint8Array(from));
return «requestTypeImpl(rpcMethod, genConfig)».toObject(decoded);
};
'''
}
Loading

0 comments on commit ef59e88

Please sign in to comment.