From edd82f249c460dad849d66592750ce1a3b011981 Mon Sep 17 00:00:00 2001 From: walon Date: Sat, 27 Nov 2021 12:02:14 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E9=87=8D=E6=9E=84=E3=80=91=E9=87=8D?= =?UTF-8?q?=E6=9E=84proto=20Render=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=20=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91?= =?UTF-8?q?=E8=A7=A3=E5=86=B3proto=E4=BB=A3=E7=A0=81=E6=9C=89=E4=B8=80?= =?UTF-8?q?=E4=BA=9BUnity=E4=B8=8B=E7=BC=96=E8=AF=91=E5=99=A8=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9A=84=E8=AF=AD=E6=B3=95=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CsUnderingSerializeVisitor.cs | 2 +- .../Source/Generate/CsRender.cs | 44 +------------- .../Source/Generate/TemplateRenderBase.cs | 60 +++++++++++++++++++ .../Source/Generate/TypescriptRender.cs | 45 +------------- 4 files changed, 63 insertions(+), 88 deletions(-) create mode 100644 src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs index 2c42014..540e11b 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs @@ -99,7 +99,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TMap type, string bufName, string fieldName) { - return $"{{ {bufName}.WriteSize({fieldName}.Count); foreach((var _k, var _v) in {fieldName}) {{ {type.KeyType.Apply(this, bufName, "_k")} {type.ValueType.Apply(this, bufName, "_v")} }} }}"; + return $"{{ {bufName}.WriteSize({fieldName}.Count); foreach(var _e in {fieldName}) {{ {type.KeyType.Apply(this, bufName, "_e.Key")} {type.ValueType.Apply(this, bufName, "_e.Value")} }} }}"; } diff --git a/src/Luban.Job.Proto/Source/Generate/CsRender.cs b/src/Luban.Job.Proto/Source/Generate/CsRender.cs index 4f4d0d1..c430125 100644 --- a/src/Luban.Job.Proto/Source/Generate/CsRender.cs +++ b/src/Luban.Job.Proto/Source/Generate/CsRender.cs @@ -8,49 +8,7 @@ using System.Collections.Generic; namespace Luban.Job.Proto.Generate { [Render("cs")] - class CsRender : RenderBase + class CsRender : TemplateRenderBase { - protected override string Render(DefEnum e) - { - return RenderUtil.RenderCsEnumClass(e); - } - - protected override string Render(DefBean b) - { - var template = StringTemplateUtil.GetTemplate("proto/cs/bean"); - var result = template.RenderCode(b); - - return result; - } - - protected override string Render(DefProto p) - { - var template = StringTemplateUtil.GetTemplate("proto/cs/proto"); - var result = template.RenderCode(p); - - return result; - } - - protected override string Render(DefRpc r) - { - var template = StringTemplateUtil.GetTemplate("proto/cs/rpc"); - var result = template.RenderCode(r); - - return result; - } - - public override string RenderStubs(string name, string module, List protos, List rpcs) - { - var template = StringTemplateUtil.GetTemplate("proto/cs/stub"); - var result = template.Render(new - { - Name = name, - Namespace = module, - Protos = protos, - Rpcs = rpcs, - }); - - return result; - } } } diff --git a/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs b/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs new file mode 100644 index 0000000..3a8e354 --- /dev/null +++ b/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs @@ -0,0 +1,60 @@ +using Luban.Job.Common.Defs; +using Luban.Job.Common.Generate; +using Luban.Job.Common.Utils; +using Luban.Job.Proto.Defs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Proto.Generate +{ + abstract class TemplateRenderBase : RenderBase + { + protected string RenderTemplateDir => (this.GetType().GetCustomAttributes(typeof(RenderAttribute), false)[0] as RenderAttribute).Name; + + protected override string Render(DefEnum e) + { + return RenderUtil.RenderCsEnumClass(e); + } + + protected override string Render(DefBean b) + { + var template = StringTemplateUtil.GetTemplate($"proto/{RenderTemplateDir}/bean"); + var result = template.RenderCode(b); + + return result; + } + + protected override string Render(DefProto p) + { + var template = StringTemplateUtil.GetTemplate($"proto/{RenderTemplateDir}/proto"); + var result = template.RenderCode(p); + + return result; + } + + protected override string Render(DefRpc r) + { + var template = StringTemplateUtil.GetTemplate($"proto/{RenderTemplateDir}/rpc"); + var result = template.RenderCode(r); + + return result; + } + + public override string RenderStubs(string name, string module, List protos, List rpcs) + { + var template = StringTemplateUtil.GetTemplate($"proto/{RenderTemplateDir}/stub"); + var result = template.Render(new + { + Name = name, + Namespace = module, + Protos = protos, + Rpcs = rpcs, + }); + + return result; + } + } +} diff --git a/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs b/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs index 8362c84..954ac13 100644 --- a/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs +++ b/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace Luban.Job.Proto.Generate { [Render("typescript")] - class TypescriptRender : RenderBase + class TypescriptRender : TemplateRenderBase { public override void Render(GenContext ctx) { @@ -64,48 +64,5 @@ namespace Luban.Job.Proto.Generate ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 }); })); } - - protected override string Render(DefEnum e) - { - return RenderUtil.RenderTypescriptEnumClass(e); - } - - protected override string Render(DefBean b) - { - var template = StringTemplateUtil.GetTemplate("proto/typescript/bean"); - var result = template.RenderCode(b); - - return result; - } - - protected override string Render(DefProto p) - { - var template = StringTemplateUtil.GetTemplate("proto/typescript/proto"); - var result = template.RenderCode(p); - - return result; - } - - protected override string Render(DefRpc r) - { - var template = StringTemplateUtil.GetTemplate("proto/typescript/rpc"); - var result = template.RenderCode(r); - - return result; - } - - public override string RenderStubs(string name, string module, List protos, List rpcs) - { - var template = StringTemplateUtil.GetTemplate("proto/typescript/stub"); - var result = template.Render(new - { - Name = name, - Namespace = module, - Protos = protos, - Rpcs = rpcs, - }); - - return result; - } } }