Generator: fix many whitespace nits (especially in generated code)

This commit is contained in:
Andres G. Aragoneses 2020-04-15 15:58:30 +08:00
parent 02c58b78dc
commit 7be6122381
4 changed files with 130 additions and 49 deletions

View file

@ -4,10 +4,12 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using TeleSharp.TL; using TeleSharp.TL;
namespace /* NAMESPACE */ namespace /* NAMESPACE */
{ {
[TLObject(/*Constructor*/)] [TLObject(/*Constructor*/)]
public class /* NAME */ : /* PARENT */ public class /* NAME */ : /* PARENT */
{ {
public override int Constructor public override int Constructor
@ -20,10 +22,10 @@ namespace /* NAMESPACE */
/* PARAMS */ /* PARAMS */
public void ComputeFlags() public void ComputeFlags()
{ {
/* COMPUTE */ /* COMPUTE */
} }
public override void DeserializeBody(BinaryReader br) public override void DeserializeBody(BinaryReader br)
{ {
@ -32,7 +34,7 @@ namespace /* NAMESPACE */
public override void SerializeBody(BinaryWriter bw) public override void SerializeBody(BinaryWriter bw)
{ {
bw.Write(Constructor); bw.Write(Constructor);
/* SERIALIZE */ /* SERIALIZE */
} }
} }

View file

@ -4,7 +4,9 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using TeleSharp.TL; using TeleSharp.TL;
namespace /* NAMESPACE */ namespace /* NAMESPACE */
{ {
public abstract class /* NAME */ : TLObject public abstract class /* NAME */ : TLObject

View file

@ -4,10 +4,12 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using TeleSharp.TL; using TeleSharp.TL;
namespace /* NAMESPACE */ namespace /* NAMESPACE */
{ {
[TLObject(/*Constructor*/)] [TLObject(/*Constructor*/)]
public class /* NAME */ : /* PARENT */ public class /* NAME */ : /* PARENT */
{ {
public override int Constructor public override int Constructor
@ -20,10 +22,10 @@ namespace /* NAMESPACE */
/* PARAMS */ /* PARAMS */
public void ComputeFlags() public void ComputeFlags()
{ {
/* COMPUTE */ /* COMPUTE */
} }
public override void DeserializeBody(BinaryReader br) public override void DeserializeBody(BinaryReader br)
{ {
@ -32,12 +34,13 @@ namespace /* NAMESPACE */
public override void SerializeBody(BinaryWriter bw) public override void SerializeBody(BinaryWriter bw)
{ {
bw.Write(Constructor); bw.Write(Constructor);
/* SERIALIZE */ /* SERIALIZE */
} }
public override void DeserializeResponse(BinaryReader br)
{ public override void DeserializeResponse(BinaryReader br)
/* DESERIALIZEResp */ {
} /* DESERIALIZEResp */
}
} }
} }

View file

@ -155,15 +155,23 @@ namespace TeleSharp.Generator
temp = temp.Replace("/* NAME */", GetNameofClass(c.Predicate, false)); temp = temp.Replace("/* NAME */", GetNameofClass(c.Predicate, false));
#endregion #endregion
#region Fields #region Fields
string fields = ""; string fields = String.Empty;
bool first = true;
foreach (var tmp in c.Params) foreach (var tmp in c.Params)
{ {
fields += $" public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)} " + "{get;set;}" + Environment.NewLine; if (!first) {
fields += Environment.NewLine + " ";
} else {
first = false;
}
fields += $"public {CheckForFlagBase (tmp.Type, GetTypeName (tmp.Type))} {CheckForKeywordAndPascalCase (tmp.Name)}" + " { get; set; }";
} }
if (fields == String.Empty)
fields = "// no fields";
temp = temp.Replace("/* PARAMS */", fields); temp = temp.Replace("/* PARAMS */", fields);
#endregion #endregion
#region ComputeFlagFunc #region ComputeFlagFunc
if (!c.Params.Any(x => x.Name == "Flags")) temp = temp.Replace("/* COMPUTE */", ""); if (!c.Params.Any(x => x.Name == "Flags")) temp = temp.Replace("/* COMPUTE */", "// do nothing");
else else
{ {
var compute = "Flags = 0;" + Environment.NewLine; var compute = "Flags = 0;" + Environment.NewLine;
@ -182,22 +190,45 @@ namespace TeleSharp.Generator
} }
#endregion #endregion
#region SerializeFunc #region SerializeFunc
var serialize = ""; var serialize = String.Empty;
first = true;
if (c.Params.Any(x => x.Name == "Flags")) serialize += "ComputeFlags();" + Environment.NewLine + "bw.Write(Flags);" + Environment.NewLine; if (c.Params.Any (x => x.Name == "Flags")) {
serialize += "ComputeFlags();" +
Environment.NewLine + " " +
"bw.Write(Flags);";
first = false;
}
foreach (var p in c.Params.Where(x => x.Name != "Flags")) foreach (var p in c.Params.Where(x => x.Name != "Flags"))
{ {
serialize += WriteWriteCode(p) + Environment.NewLine; var code = WriteWriteCode (p);
if (String.IsNullOrEmpty(code))
continue;
if (!first) {
serialize += Environment.NewLine + " ";
} else {
first = false;
}
serialize += code;
} }
if (serialize == String.Empty)
serialize = "// do nothing";
temp = temp.Replace("/* SERIALIZE */", serialize); temp = temp.Replace("/* SERIALIZE */", serialize);
#endregion #endregion
#region DeSerializeFunc #region DeSerializeFunc
var deserialize = ""; var deserialize = String.Empty;
first = true;
foreach (var p in c.Params) foreach (var p in c.Params)
{ {
deserialize += WriteReadCode(p) + Environment.NewLine; if (!first) {
deserialize += Environment.NewLine + " ";
} else {
first = false;
}
deserialize += WriteReadCode(p);
} }
if (deserialize == String.Empty)
deserialize = "// do nothing";
temp = temp.Replace("/* DESERIALIZE */", deserialize); temp = temp.Replace("/* DESERIALIZE */", deserialize);
#endregion #endregion
writer.Write(temp); writer.Write(temp);
@ -232,15 +263,26 @@ namespace TeleSharp.Generator
#endregion #endregion
#region Fields #region Fields
string fields = ""; string fields = "";
bool first = true;
foreach (var tmp in c.Params) foreach (var tmp in c.Params)
{ {
fields += $" public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)} " + "{get;set;}" + Environment.NewLine; if (!first) {
fields += Environment.NewLine + " ";
} else {
first = false;
}
fields += $"public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)}" + " { get; set; }";
} }
fields += $" public {CheckForFlagBase(c.Type, GetTypeName(c.Type))} Response" + "{ get; set;}" + Environment.NewLine; if (!first) {
fields += Environment.NewLine + " ";
} else {
first = false;
}
fields += $"public {CheckForFlagBase(c.Type, GetTypeName(c.Type))} Response" + " { get; set; }";
temp = temp.Replace("/* PARAMS */", fields); temp = temp.Replace("/* PARAMS */", fields);
#endregion #endregion
#region ComputeFlagFunc #region ComputeFlagFunc
if (!c.Params.Any(x => x.Name == "Flags")) temp = temp.Replace("/* COMPUTE */", ""); if (!c.Params.Any(x => x.Name == "Flags")) temp = temp.Replace("/* COMPUTE */", "// do nothing");
else else
{ {
var compute = "Flags = 0;" + Environment.NewLine; var compute = "Flags = 0;" + Environment.NewLine;
@ -259,28 +301,50 @@ namespace TeleSharp.Generator
} }
#endregion #endregion
#region SerializeFunc #region SerializeFunc
var serialize = ""; var serialize = String.Empty;
first = true;
if (c.Params.Any(x => x.Name == "Flags")) serialize += "ComputeFlags();" + Environment.NewLine + "bw.Write(Flags);" + Environment.NewLine; if (c.Params.Any (x => x.Name == "Flags")) {
serialize += "ComputeFlags();" + Environment.NewLine +
" " + "bw.Write(Flags);";
first = false;
}
foreach (var p in c.Params.Where(x => x.Name != "Flags")) foreach (var p in c.Params.Where(x => x.Name != "Flags"))
{ {
serialize += WriteWriteCode(p) + Environment.NewLine; var code = WriteWriteCode (p);
if (String.IsNullOrEmpty (code))
continue;
if (!first) {
serialize += Environment.NewLine + " ";
} else {
first = false;
}
serialize += code;
} }
if (serialize == String.Empty)
serialize = "// do nothing else";
temp = temp.Replace("/* SERIALIZE */", serialize); temp = temp.Replace("/* SERIALIZE */", serialize);
#endregion #endregion
#region DeSerializeFunc #region DeSerializeFunc
var deserialize = ""; var deserialize = String.Empty;
first = true;
foreach (var p in c.Params) foreach (var p in c.Params)
{ {
deserialize += WriteReadCode(p) + Environment.NewLine; if (!first) {
deserialize += Environment.NewLine + " ";
} else {
first = false;
}
deserialize += WriteReadCode (p);
} }
if (deserialize == String.Empty)
deserialize = "// do nothing";
temp = temp.Replace("/* DESERIALIZE */", deserialize); temp = temp.Replace("/* DESERIALIZE */", deserialize);
#endregion #endregion
#region DeSerializeRespFunc #region DeSerializeRespFunc
var deserializeResp = ""; var deserializeResp = "";
TlParam p2 = new TlParam() { Name = "Response", Type = c.Type }; TlParam p2 = new TlParam() { Name = "Response", Type = c.Type };
deserializeResp += WriteReadCode(p2) + Environment.NewLine; deserializeResp += WriteReadCode(p2);
temp = temp.Replace("/* DESERIALIZEResp */", deserializeResp); temp = temp.Replace("/* DESERIALIZEResp */", deserializeResp);
#endregion #endregion
writer.Write(temp); writer.Write(temp);
@ -289,6 +353,7 @@ namespace TeleSharp.Generator
} }
} }
} }
public static string FormatName(string input) public static string FormatName(string input)
{ {
if (string.IsNullOrEmpty(input)) if (string.IsNullOrEmpty(input))
@ -305,6 +370,7 @@ namespace TeleSharp.Generator
} }
return input.First().ToString().ToUpper() + input.Substring(1); return input.First().ToString().ToUpper() + input.Substring(1);
} }
public static string CheckForKeywordAndPascalCase(string name) public static string CheckForKeywordAndPascalCase(string name)
{ {
name = name.Replace("_", " "); name = name.Replace("_", " ");
@ -314,6 +380,7 @@ namespace TeleSharp.Generator
if (keywords.Contains(name)) return "@" + name; if (keywords.Contains(name)) return "@" + name;
return name; return name;
} }
public static string GetNameofClass(string type, bool isinterface = false, bool ismethod = false) public static string GetNameofClass(string type, bool isinterface = false, bool ismethod = false)
{ {
if (!ismethod) if (!ismethod)
@ -335,18 +402,22 @@ namespace TeleSharp.Generator
return "TLRequest" + FormatName(type); return "TLRequest" + FormatName(type);
} }
} }
private static bool IsFlagBase(string type) private static bool IsFlagBase(string type)
{ {
return type.IndexOf("?") != -1; return type.IndexOf("?") != -1;
} }
private static int GetBitMask(string type) private static int GetBitMask(string type)
{ {
return (int)Math.Pow((double)2, (double)int.Parse(type.Split('?')[0].Split('.')[1])); return (int)Math.Pow((double)2, (double)int.Parse(type.Split('?')[0].Split('.')[1]));
} }
private static bool IsTrueFlag(string type) private static bool IsTrueFlag(string type)
{ {
return type.Split('?')[1] == "true"; return type.Split('?')[1] == "true";
} }
public static string GetNameSpace(string type) public static string GetNameSpace(string type)
{ {
if (type.IndexOf('.') != -1) if (type.IndexOf('.') != -1)
@ -354,6 +425,7 @@ namespace TeleSharp.Generator
else else
return "TeleSharp.TL"; return "TeleSharp.TL";
} }
public static string CheckForFlagBase(string type, string result) public static string CheckForFlagBase(string type, string result)
{ {
if (type.IndexOf('?') == -1) if (type.IndexOf('?') == -1)
@ -366,6 +438,7 @@ namespace TeleSharp.Generator
else return result; else return result;
} }
} }
public static string GetTypeName(string type) public static string GetTypeName(string type)
{ {
switch (type.ToLower()) switch (type.ToLower())
@ -401,7 +474,6 @@ namespace TeleSharp.Generator
if (type.IndexOf('.') != -1 && type.IndexOf('?') == -1) if (type.IndexOf('.') != -1 && type.IndexOf('?') == -1)
{ {
if (interfacesList.Any(x => x.ToLower() == (type).ToLower())) if (interfacesList.Any(x => x.ToLower() == (type).ToLower()))
return FormatName(type.Split('.')[0]) + "." + "TLAbs" + type.Split('.')[1]; return FormatName(type.Split('.')[0]) + "." + "TLAbs" + type.Split('.')[1];
else if (classesList.Any(x => x.ToLower() == (type).ToLower())) else if (classesList.Any(x => x.ToLower() == (type).ToLower()))
@ -422,9 +494,8 @@ namespace TeleSharp.Generator
{ {
return GetTypeName(type.Split('?')[1]); return GetTypeName(type.Split('?')[1]);
} }
} }
public static string LookTypeInLists(string src) public static string LookTypeInLists(string src)
{ {
if (interfacesList.Any(x => x.ToLower() == src.ToLower())) if (interfacesList.Any(x => x.ToLower() == src.ToLower()))
@ -434,6 +505,7 @@ namespace TeleSharp.Generator
else else
return src; return src;
} }
public static string WriteWriteCode(TlParam p, bool flag = false) public static string WriteWriteCode(TlParam p, bool flag = false)
{ {
switch (p.Type.ToLower()) switch (p.Type.ToLower())
@ -444,18 +516,18 @@ namespace TeleSharp.Generator
case "long": case "long":
return flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});"; return flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});";
case "string": case "string":
return $"StringUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"; return $"StringUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}, bw);";
case "bool": case "bool":
return flag ? $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}.Value,bw);" : $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"; return flag ? $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}.Value, bw);" : $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}, bw);";
case "true": case "true":
return $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"; return $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}, bw);";
case "bytes": case "bytes":
return $"BytesUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"; return $"BytesUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}, bw);";
case "double": case "double":
return flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});"; return flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});";
default: default:
if (!IsFlagBase(p.Type)) if (!IsFlagBase(p.Type))
return $"ObjectUtils.SerializeObject({CheckForKeywordAndPascalCase(p.Name)},bw);"; return $"ObjectUtils.SerializeObject({CheckForKeywordAndPascalCase(p.Name)}, bw);";
else else
{ {
if (IsTrueFlag(p.Type)) if (IsTrueFlag(p.Type))
@ -463,7 +535,8 @@ namespace TeleSharp.Generator
else else
{ {
TlParam p2 = new TlParam() { Name = p.Name, Type = p.Type.Split('?')[1] }; TlParam p2 = new TlParam() { Name = p.Name, Type = p.Type.Split('?')[1] };
return $"if ((Flags & {GetBitMask(p.Type).ToString()}) != 0)" + Environment.NewLine + return $"if ((Flags & {GetBitMask(p.Type).ToString()}) != 0)" +
Environment.NewLine + " " +
WriteWriteCode(p2, true); WriteWriteCode(p2, true);
} }
} }
@ -503,9 +576,10 @@ namespace TeleSharp.Generator
else else
{ {
TlParam p2 = new TlParam() { Name = p.Name, Type = p.Type.Split('?')[1] }; TlParam p2 = new TlParam() { Name = p.Name, Type = p.Type.Split('?')[1] };
return $"if ((Flags & {GetBitMask(p.Type).ToString()}) != 0)" + Environment.NewLine + return $"if ((Flags & {GetBitMask(p.Type).ToString()}) != 0)" +
WriteReadCode(p2) + Environment.NewLine + Environment.NewLine + " " +
"else" + Environment.NewLine + WriteReadCode(p2) + Environment.NewLine + " " +
"else" + Environment.NewLine + " " +
$"{CheckForKeywordAndPascalCase(p.Name)} = null;" + Environment.NewLine; $"{CheckForKeywordAndPascalCase(p.Name)} = null;" + Environment.NewLine;
} }
} }