ts format (#302)

This commit is contained in:
Dima 2021-09-14 13:46:48 +03:00 committed by GitHub
parent 84141bbcc4
commit c4248640af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 117 deletions

View File

@ -66,7 +66,7 @@ object TypeScriptCommon {
def argsCallToTs(at: ArrowType): List[String] =
FuncRes.arrowArgIndices(at).map(idx => s"args[$idx]")
def callBackExprBody(at: ArrowType, callbackName: String): String = {
def callBackExprBody(at: ArrowType, callbackName: String, leftSpace: Int): String = {
val arrowArgumentsToCallbackArgumentsList =
at.domain.toList
.zipWithIndex
@ -101,16 +101,16 @@ object TypeScriptCommon {
})
.mkString(",")
s"""
| const callParams = {
| ...req.particleContext,
| tetraplets: {
| ${tetraplets}
| },
| };
| resp.retCode = ResultCodes.success;
| ${callCallbackStatementAndReturn}
|""".stripMargin
val left = " " * leftSpace
s"""${left}const callParams = {
|$left ...req.particleContext,
|$left tetraplets: {
|$left ${tetraplets}
|$left },
|$left};
|${left}resp.retCode = ResultCodes.success;
|$left${callCallbackStatementAndReturn}""".stripMargin
}
}

View File

@ -12,7 +12,6 @@ case class TypeScriptFile(res: AquaRes) {
|
|// Services
|${res.services.map(TypeScriptService(_)).map(_.generate).toList.mkString("\n\n")}
|
|// Functions
|${res.funcs.map(TypeScriptFunc(_)).map(_.generate).toList.mkString("\n\n")}
|""".stripMargin

View File

@ -23,8 +23,7 @@ case class TypeScriptFunc(func: FuncRes) {
| return resolve(opt);""".stripMargin
case pt: ProductType =>
val unwrapOpts = pt.toList.zipWithIndex.collect { case (OptionType(_), i) =>
s"""
| if(Array.isArray(opt[$i])) {
s""" if( Array.isArray(opt[$i])) {
| if (opt[$i].length === 0) { opt[$i] = null; }
| else {opt[$i] = opt[$i][0]; }
| }""".stripMargin
@ -41,8 +40,7 @@ case class TypeScriptFunc(func: FuncRes) {
}
s""" h.onEvent('$callbackServiceId', '$respFuncName', (args) => {
|$respBody
|});
|""".stripMargin
| });""".stripMargin
def generate: String = {
@ -57,10 +55,9 @@ case class TypeScriptFunc(func: FuncRes) {
case Arg(argName, _: DataType) =>
s""" h.on('$dataServiceId', '$argName', () => {return ${fixupArgName(argName)};});"""
case Arg(argName, at: ArrowType) =>
s"""
| h.use((req, resp, next) => {
s""" h.use((req, resp, next) => {
| if(req.serviceId === '${conf.callbackService}' && req.fnName === '$argName') {
| ${callBackExprBody(at, argName)}
|${callBackExprBody(at, argName, 28)}
| }
| next();
| });
@ -101,6 +98,8 @@ case class TypeScriptFunc(func: FuncRes) {
val funcTypeRes = s"Promise<$retTypeTs>"
val codeLeftSpace = " " * 20
s"""
|export function ${func.funcName}(${funcTypeOverload1}) : ${funcTypeRes};
|export function ${func.funcName}(${funcTypeOverload2}) : ${funcTypeRes};
@ -120,9 +119,8 @@ case class TypeScriptFunc(func: FuncRes) {
| const promise = new Promise<$retTypeTs>((resolve, reject) => {
| const r = new RequestFlowBuilder()
| .disableInjections()
| .withRawScript(
| `
| ${tsAir.show}
| .withRawScript(`
|${tsAir.show.linesIterator.map(codeLeftSpace + _).mkString("\n")}
| `,
| )
| .configHandler((h) => {
@ -141,15 +139,16 @@ case class TypeScriptFunc(func: FuncRes) {
| .handleTimeout(() => {
| reject('Request timed out for ${func.funcName}');
| })
|
| if (${configArgName} && ${configArgName}.ttl) {
| r.withTTL(${configArgName}.ttl)
| }
|
| request = r.build();
| });
| peer.internals.initiateFlow(request!);
| return ${returnVal};
|}
""".stripMargin
|}""".stripMargin
}
}

View File

@ -11,11 +11,9 @@ case class TypeScriptService(srv: ServiceRes) {
import TypeScriptCommon._
def fnHandler(arrow: ArrowType, memberName: String) = {
s"""
| if (req.fnName === '${memberName}') {
| ${callBackExprBody(arrow, "service." + memberName)}
| }
""".stripMargin
s"""if (req.fnName === '${memberName}') {
|${callBackExprBody(arrow, "service." + memberName, 12)}
}""".stripMargin
}
def generate: String =
@ -66,16 +64,14 @@ case class TypeScriptService(srv: ServiceRes) {
// This variable contain defines the list of lists where
// the outmost list describes the list of overloads
// and the innermost one defines the list of arguments in the overload
val registerServiceArgs = registerServiceArgsSource.
map(x => {
val registerServiceArgs = registerServiceArgsSource.map{ x =>
val args = x.mkString(", ")
s"export function ${registerName}(${args}): void;"
})
}
.mkString("\n");
val defaultServiceIdBranch = srv.defaultId.fold("")(x =>
s"""
| else {
s"""else {
| serviceId = ${x}
| }""".stripMargin
)
@ -85,7 +81,7 @@ case class TypeScriptService(srv: ServiceRes) {
| ${fnDefs}
|}
|
| ${registerServiceArgs}
|$registerServiceArgs
|export function ${registerName}(...args: any) {
| let peer: FluencePeer;
| let serviceId: any;