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

@ -14,7 +14,7 @@ object TypeScriptCommon {
case pt: ProductType =>
"[" + pt.toList.map(typeToTs).mkString(", ") + "]"
case st: StructType =>
s"{${st.fields.map(typeToTs).toNel.map(kv => kv._1 + ":" + kv._2).toList.mkString(";")}}"
s"{ ${st.fields.map(typeToTs).toNel.map(kv => kv._1 + ": " + kv._2).toList.mkString("; ")} }"
case st: ScalarType if ScalarType.number(st) => "number"
case ScalarType.bool => "boolean"
case ScalarType.string => "string"
@ -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

@ -15,34 +15,32 @@ case class TypeScriptFunc(func: FuncRes) {
val respBody = func.returnType match {
case Some(x) => x match {
case OptionType(_) =>
""" let [opt] = args;
| if (Array.isArray(opt)) {
| if (opt.length === 0) { resolve(null); }
| opt = opt[0];
| }
| return resolve(opt);""".stripMargin
""" let [opt] = args;
| if (Array.isArray(opt)) {
| if (opt.length === 0) { resolve(null); }
| opt = opt[0];
| }
| return resolve(opt);""".stripMargin
case pt: ProductType =>
val unwrapOpts = pt.toList.zipWithIndex.collect { case (OptionType(_), i) =>
s"""
| if(Array.isArray(opt[$i])) {
| if (opt[$i].length === 0) { opt[$i] = null; }
| else {opt[$i] = opt[$i][0]; }
| }""".stripMargin
s""" if( Array.isArray(opt[$i])) {
| if (opt[$i].length === 0) { opt[$i] = null; }
| else {opt[$i] = opt[$i][0]; }
| }""".stripMargin
}.mkString
s""" let opt: any = args;
|$unwrapOpts
| return resolve(opt);""".stripMargin
s""" let opt: any = args;
|$unwrapOpts
| return resolve(opt);""".stripMargin
case _ =>
""" const [res] = args;
| resolve(res);""".stripMargin
""" const [res] = args;
| resolve(res);""".stripMargin
}
case None => ""
}
s"""h.onEvent('$callbackServiceId', '$respFuncName', (args) => {
| $respBody
|});
|""".stripMargin
s""" h.onEvent('$callbackServiceId', '$respFuncName', (args) => {
|$respBody
| });""".stripMargin
def generate: String = {
@ -53,17 +51,16 @@ case class TypeScriptFunc(func: FuncRes) {
val setCallbacks = func.args.collect { // Product types are not handled
case Arg(argName, OptionType(_)) =>
s"""h.on('$dataServiceId', '$argName', () => {return ${fixupArgName(argName)} === null ? [] : [${fixupArgName(argName)}];});"""
s""" h.on('$dataServiceId', '$argName', () => {return ${fixupArgName(argName)} === null ? [] : [${fixupArgName(argName)}];});"""
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) =>
s"""
| h.use((req, resp, next) => {
| if(req.serviceId === '${conf.callbackService}' && req.fnName === '$argName') {
| ${callBackExprBody(at, argName)}
| }
| next();
| });
s""" h.use((req, resp, next) => {
| if(req.serviceId === '${conf.callbackService}' && req.fnName === '$argName') {
|${callBackExprBody(at, argName, 28)}
| }
| next();
| });
""".stripMargin
}
.mkString("\n")
@ -86,7 +83,7 @@ case class TypeScriptFunc(func: FuncRes) {
var funcTypeOverload1 = argsTypescript.mkString(", ")
var funcTypeOverload2 = ("peer: FluencePeer" :: argsTypescript).mkString(", ")
val argsLets = args.map(arg => s"let ${fixupArgName(arg.name)}: any;").mkString("\n")
val argsLets = args.map(arg => s" let ${fixupArgName(arg.name)}: any;").mkString("\n")
val argsFormAssingment = args
.map(arg => fixupArgName(arg.name))
@ -96,60 +93,62 @@ case class TypeScriptFunc(func: FuncRes) {
// argument upnacking has two forms.
// One starting from the first (by index) argument,
// One starting from zero
val argsAssignmentStartingFrom1 = argsFormAssingment.map((name, ix) => s"${name} = args[${ix + 1}];").mkString("\n")
val argsAssignmentStartingFrom0 = argsFormAssingment.map((name, ix) => s"${name} = args[${ix}];").mkString("\n")
val argsAssignmentStartingFrom1 = argsFormAssingment.map((name, ix) => s" ${name} = args[${ix + 1}];").mkString("\n")
val argsAssignmentStartingFrom0 = argsFormAssingment.map((name, ix) => s" ${name} = args[${ix}];").mkString("\n")
val funcTypeRes = s"Promise<$retTypeTs>"
val codeLeftSpace = " " * 20
s"""
| export function ${func.funcName}(${funcTypeOverload1}) : ${funcTypeRes};
| export function ${func.funcName}(${funcTypeOverload2}) : ${funcTypeRes};
| export function ${func.funcName}(...args: any) {
| let peer: FluencePeer;
| ${argsLets}
| let config: any;
| if (FluencePeer.isInstance(args[0])) {
| peer = args[0];
| ${argsAssignmentStartingFrom1}
| } else {
| peer = Fluence.getPeer();
| ${argsAssignmentStartingFrom0}
| }
|
| let request: RequestFlow;
| const promise = new Promise<$retTypeTs>((resolve, reject) => {
| const r = new RequestFlowBuilder()
| .disableInjections()
| .withRawScript(
| `
| ${tsAir.show}
| `,
| )
| .configHandler((h) => {
| ${conf.relayVarName.fold("") { r =>
|export function ${func.funcName}(${funcTypeOverload1}) : ${funcTypeRes};
|export function ${func.funcName}(${funcTypeOverload2}) : ${funcTypeRes};
|export function ${func.funcName}(...args: any) {
| let peer: FluencePeer;
|${argsLets}
| let config: any;
| if (FluencePeer.isInstance(args[0])) {
| peer = args[0];
|${argsAssignmentStartingFrom1}
| } else {
| peer = Fluence.getPeer();
|${argsAssignmentStartingFrom0}
| }
|
| let request: RequestFlow;
| const promise = new Promise<$retTypeTs>((resolve, reject) => {
| const r = new RequestFlowBuilder()
| .disableInjections()
| .withRawScript(`
|${tsAir.show.linesIterator.map(codeLeftSpace + _).mkString("\n")}
| `,
| )
| .configHandler((h) => {
| ${conf.relayVarName.fold("") { r =>
s"""h.on('${conf.getDataService}', '$r', () => {
| return peer.getStatus().relayPeerId;
| });""".stripMargin }}
| $setCallbacks
| $returnCallback
| h.onEvent('${conf.errorHandlingService}', '${conf.errorFuncName}', (args) => {
| const [err] = args;
| reject(err);
| });
| })
| .handleScriptError(reject)
| .handleTimeout(() => {
| reject('Request timed out for ${func.funcName}');
| })
| if(${configArgName} && ${configArgName}.ttl) {
| r.withTTL(${configArgName}.ttl)
| }
| request = r.build();
| return peer.getStatus().relayPeerId;
| });""".stripMargin }}
|$setCallbacks
|$returnCallback
| h.onEvent('${conf.errorHandlingService}', '${conf.errorFuncName}', (args) => {
| const [err] = args;
| reject(err);
| });
| })
| .handleScriptError(reject)
| .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,27 +64,25 @@ 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 {
| serviceId = ${x}
|}""".stripMargin
s"""else {
| serviceId = ${x}
| }""".stripMargin
)
s"""
| export interface ${serviceTypeName} {
| ${fnDefs}
| }
|export interface ${serviceTypeName} {
| ${fnDefs}
|}
|
| ${registerServiceArgs}
| export function ${registerName}(...args: any) {
|$registerServiceArgs
|export function ${registerName}(...args: any) {
| let peer: FluencePeer;
| let serviceId: any;
| let service: any;
@ -115,16 +111,16 @@ case class TypeScriptService(srv: ServiceRes) {
| service = args[2];
| }
|
| peer.internals.callServiceHandler.use((req, resp, next) => {
| if (req.serviceId !== serviceId) {
| next();
| return;
| }
|
| ${fnHandlers}
|
| next();
| });
| }
| peer.internals.callServiceHandler.use((req, resp, next) => {
| if (req.serviceId !== serviceId) {
| next();
| return;
| }
|
| ${fnHandlers}
|
| next();
| });
|}
""".stripMargin
}