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

View File

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

View File

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

View File

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