在TST文件中添加条件逻辑以生成ts代码

时间:2018-09-26 11:48:18

标签: angular typescript

我有一个tst文件,该文件生成用于我的angular客户项目的ts文件。下面提供了tst和ts文件的代码。我需要放置一个条件语句以基于HttpRef类型生成return语句。我们有两种类型,即_phttp和_nghttp。如果您在下面看到tst文件的代码段,则尝试将if条件放入。我遇到的问题是它在ts文件中将if和else条件生成为完整块。我只需要生成基于if或else条件的返回条件。有人可以告诉我如何实现吗?

tst的片段

@Injectable()
export class $StubName {
       constructor($HttpImports) { } 
    $Classes(c=>c.BaseClass!=null&&c.BaseClass.Name=="ApiController"&&!c.Attributes.Any(a=>a.Name=="TsIgnore"))[
    $Methods(m=>!m.Attributes.Any(a=>a.Name=="TsIgnore"))[
    $name($Parameters[$name$IfOptional: $ParameterType][, ]): Observable<ApiResult<$ReturnType>> {

       if(this.$HttpRef =="_phttp")
         return true;
       else
        return this.$HttpRef.$HttpMethod(`/platform/$Url`,$RequestData||{},{observe: 'response'}).pipe(map((ret:HttpResponse<$ReturnType>) => <ApiResult<$ReturnType>>({  data: ret.body , originalResponse: ret })));
       }]
    $Methods(m=>m.Attributes.Any(a=>a.Name=="TsIgnore"))[
    static $name_url ($Parameters(p=>p.Type.IsPrimitive)[$name: $ParameterType][, ]):{method:string, url:string} {
        return {method: '$HttpMethod', url:`/platform/$UnescapedUrl`};
       }]]
}

完整的tst文件

${
    // Enable extension methods by adding using Typewriter.Extensions.*
    using Typewriter.Extensions.Types;
    using Typewriter.Extensions.WebApi;



    // Uncomment the constructor to change template settings.
    Template(Settings settings)
    {
        settings.IncludeCurrentProject();
    }
     string TypeName(Type t)
    {
    if (t.Name == "void") return "void";
     if (t.OriginalName == "byte[]") return "ArrayBuffer";
     if (t.Name.StartsWith("BinaryTransferObject"))
     {
     if (!t.IsGeneric)
     return t.Name;
     else return "BinaryTransferObject<"+TypeName(t.TypeArguments[0])+">";
     }
    if (t.IsPrimitive&&!t.IsEnum || t.Name == "any") return t.Name;
    return "Dto."+t.Name;
    }
    string ParameterType(Parameter p) {
   return TypeName(p.Type);

     }
    string IfOptional(Parameter p) =>( p.Type.IsNullable || p.Attributes.Any(a=>a.Name =="TsOptional")? "?": "");
  string ReturnType(Method m) =>new []{"IHttpActionResult", "HttpResponseMessage", "any"}.Contains(m.Type.Name) ? "any" : TypeName(m.Type);
  string StubName(File f)=>f.Name.Replace(".cs","").Replace("Controller","")+"Proxy";

  string ProxyImport (File f){
   var api= f.Classes.FirstOrDefault(c=>c.BaseClass!= null && c.BaseClass.Name=="ApiController");
   if (api == null) return null;
    if (api.Attributes.Any(i=>i.Name=="AllowAnonymous")) return "import { HttpClient } from '@angular/common/http';";
    if (api.Attributes.Any(i=>i.Name=="Authorize")) return"import { ProxyHttp } from '../ajaxproxy';";
    return @"import { HttpClient } from '@angular/common/http';
import { ProxyHttp  } from '../ajaxproxy';";
   }

   string HttpImports (File f)
   {
   var api= f.Classes.FirstOrDefault(c=>c.BaseClass!= null && c.BaseClass.Name=="ApiController");
   if (api == null) return null;
    if (api.Attributes.Any(i=>i.Name=="AllowAnonymous")) return "private _nghttp: HttpClient";
    if (api.Attributes.Any(i=>i.Name=="Authorize")) return "private _phttp: ProxyHttp";
    return "private _nghttp: HttpClient, private _phttp: ProxyHttp";
   }


   string HttpRef(Method m)
   {
    var api = (Class)m.Parent;
      if (api.Attributes.Any(i=>i.Name=="AllowAnonymous")) return "_nghttp";
    if (api.Attributes.Any(i=>i.Name=="Authorize")) return "_phttp";
     if (m.Attributes.Any(i=>i.Name=="AllowAnonymous")) return "_nghttp";
    if (m.Attributes.Any(i=>i.Name=="Authorize")) return "_phttp";
    return "#ERROR";
   }

   string UnescapedUrl(Method m)
   { return m.Url().Replace("encodeURIComponent(","").Replace(")",""); }

    // More info: http://frhagn.github.io/Typewriter/
}

import * as Dto from '../dtos'; 
import { HttpClient, HttpResponse } from '@angular/common/http';
import { ProxyHttp  } from '../ajaxproxy';
import { Injectable } from '@angular/core';
import { BinaryTransferObject } from '../binary-transfer-object';
import { Observable } from 'rxjs';
import { ApiResult } from '../apiresult';
import { map } from 'rxjs/operators'; 
export { ApiResult };


@Injectable()
export class $StubName {
       constructor($HttpImports) { } 
    $Classes(c=>c.BaseClass!=null&&c.BaseClass.Name=="ApiController"&&!c.Attributes.Any(a=>a.Name=="TsIgnore"))[
    $Methods(m=>!m.Attributes.Any(a=>a.Name=="TsIgnore"))[
    $name($Parameters[$name$IfOptional: $ParameterType][, ]): Observable<ApiResult<$ReturnType>> {

        return this.$HttpRef.$HttpMethod(`/platform/$Url`,$RequestData||{}).pipe(map((ret:HttpResponse<$ReturnType>) => <ApiResult<$ReturnType>>({  data: ret.body , originalResponse: ret })));
       }]
    $Methods(m=>m.Attributes.Any(a=>a.Name=="TsIgnore"))[
    static $name_url ($Parameters(p=>p.Type.IsPrimitive)[$name: $ParameterType][, ]):{method:string, url:string} {
        return {method: '$HttpMethod', url:`/platform/$UnescapedUrl`};
       }]]
}

生成的controller.ts文件的示例

如果看到下面的代码,则将在下面生成if和else块条件语句。我需要return true语句或else中提到的return语句。我不想看到if和else语句

 export class LocalisationProxy {
       constructor(private _nghttp: HttpClient, private _phttp: ProxyHttp) { } 


  getLatestTranslationFilesAndMerge(cultureId: number): Observable<ApiResult<any>> {

     if(this._nghttp =="_phttp")
     return true;
   else
        return this._nghttp.get(`/platform/localisation/${cultureId}`,null||{},{observe: 'response'}).pipe(map((ret:HttpResponse<any>) => <ApiResult<any>>({  data: ret.body , originalResponse: ret })));
   }
getTranslationDocument(documentId: number): Observable<ApiResult<any>> {

     if(this._phttp =="_phttp")
     return true;
   else
        return this._phttp.get(`/platform/localisation/document/${documentId}`,null||{},{observe: 'response'}).pipe(map((ret:HttpResponse<any>) => <ApiResult<any>>({  data: ret.body , originalResponse: ret })));
   }

    }

0 个答案:

没有答案