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; - } } }