ServParam2ClientModelEngine/CoderEngine/SwiftCodeWrite.cs

185 lines
6.3 KiB
C#
Raw Permalink Normal View History

2018-10-18 07:00:43 +08:00
using System;
using System.Linq;
using System.Text;
2018-10-18 17:59:03 +08:00
using System.Collections.Generic;
2018-10-18 07:00:43 +08:00
namespace CoderEngine
{
2018-10-18 17:59:03 +08:00
public class SwiftCodeWrite
2018-10-18 07:00:43 +08:00
{
2018-10-18 17:59:03 +08:00
private string CodeStr = "";
/// <summary>
/// 一个代码缩进
/// </summary>
private string tabstr = " ";
/// <summary>
/// 逐行添加代码
/// </summary>
/// <param name="codeline"></param>
/// <returns></returns>
public void CoderAddLine(string CodeLine)
{
CodeStr += CodeLine + "\n";
}
public string ModelToSwiftCode(string _AssemblyName, string _TypeName)
{
TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
ModelToJavaCodeWriter(tm);
return CodeStr;
}
public void ModelToJavaCodeWriter(TypeMode TopMode)
{
//如果有代码嵌套的下级类 在本类外 写入新的类
if (TopMode.TypeList != null && TopMode.TypeList.Count > 0)
{
CoderAddLine(tabstr);
CoderAddLine("import Foundation");
CoderAddLine("import ObjectMapper");
SwitchCodeDownRecursion(TopMode);
}
}
public void SwitchCodeDownRecursion(TypeMode TopMode)
{
string ClassName = "";
//如果自己就是集合类型 则取类型下名字
if (TopMode.MemberType.Name == "List`1")
{
//FullName的值为 "System.Collections.Generic.List`1[[ --集合内嵌类型名称--, CoderEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"
//其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型”
var SonParamName = TopMode.MemberType.FullName.Substring(TopMode.MemberType.FullName.IndexOf("[") + 2);
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".") + 1);
}
else
ClassName = TopMode.MemberType.Name;
CoderAddLine("");
CoderAddLine("class "+ClassName+": Mappable {");
foreach (var tl in TopMode.TypeList)
{
CoderAddLine("");
if (tl.MemberType.Name == "List`1")
{
//如果集合类型的特殊对待? 貌似Swift没有
}
else
{
}
CoderAddLine(tabstr + "var "+ tl.Name + ": "+ CSharpToSwiftTypeName(tl.MemberType) + " = "+ GetDefaultValueForType(tl.MemberType));
}
CoderAddLine("");
CoderAddLine(tabstr + "required init?(map: Map) {");
CoderAddLine("");
CoderAddLine(tabstr + "}");
CoderAddLine("");
CoderAddLine(tabstr + "//为" + ClassName + "生成mapping方法");
CoderAddLine(tabstr + "func mapping(map: Map) {");
CoderAddLine("");
foreach (var tl in TopMode.TypeList)
{
CoderAddLine(tabstr + tabstr + tl.Name + " <- map[\""+ tl.Name + "\"]");
}
CoderAddLine(tabstr + "}");
CoderAddLine("}");
foreach (var tl in TopMode.TypeList)
{
if (tl.MemberType.Name == "List`1")
{
if (tl.TypeList != null && tl.TypeList.Count > 0)
{
CoderAddLine("");
SwitchCodeDownRecursion(tl);
}
}
}
}
public string GetDefaultValueForType(Type myType)
{
switch (CSharpToSwiftTypeName(myType))
{
case "Int":
return "0";
case "Double":
return "0";
case "Bool":
return "Bool";
case "String":
return "\"\"";
default:
{
if (CSharpToSwiftTypeName(myType).Contains("["))
{
return "[]";
}
else
return "\"\"";
}
}
}
public string CSharpToSwiftTypeName(Type myType)
{
var oneinfo = CSharpToSwiftTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault();
if (oneinfo.Value != null)
{
return oneinfo.Value;
}
else
{
if (myType.Name == "List`1")
{
var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
return "[" + SonParamName.Substring(SonParamName.LastIndexOf('.') + 1) + "]";
}
else if (myType.Name == "Nullable`1")
{
var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
return SonParamName.Substring(SonParamName.LastIndexOf('.') + 1);
}
else
return "String";
}
}
/// <summary>
/// C#和Swift类型对照表
/// </summary>
public System.Collections.Generic.Dictionary<string, string> CSharpToSwiftTypeItem =
new Dictionary<string, string>() {
{ "int","Int"},
{ "Int16","Int"},
{ "Int32","Int"},
{ "Int64","Int"},
{ "int?","Int"},
{ "long","Int"},
{ "long?","Int"},
{ "float","Double"},
{ "float?","Double"},
{ "double","Double"},
{ "double?","Double"},
{ "boolean","Bool"},
{ "string","String"},
{ "DateTime","String"},
{ "DateTime?","String"},
};
2018-10-18 07:00:43 +08:00
}
}