From a9de6b924ba67ea3c5cdaf237186bee29b7e6bf1 Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 29 Nov 2021 14:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20proto=20go=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Client/Luban.Client.csproj | 4 - src/Luban.ClientServer/Program.cs | 2 - src/Luban.Common/Luban.Common.csproj | 1 + .../Source/Utils/CommandLineUtil.cs | 30 ++++ .../DataVisitors/ToErlangLiteralVisitor.cs | 2 +- .../DataVisitors/ToLiteralVisitorBase.cs | 2 +- .../DataVisitors/ToLuaLiteralVisitor.cs | 2 +- .../DataVisitors/ToPythonLiteralVisitor.cs | 2 +- .../Source/DataVisitors/ToStringVisitor.cs | 2 +- .../DataVisitors/ToXmlLiteralVisitor.cs | 2 +- src/Luban.Job.Cfg/Source/Defs/DefBean.cs | 16 -- .../TypeVisitors/GoDeserializeJson2Visitor.cs | 2 +- .../TypeVisitors/GoDeserializeJsonVisitor.cs | 2 +- .../GoDeserializeUnderingVisitor.cs | 152 ------------------ .../Source/Utils/TTypeTemplateExtends.cs | 15 -- .../Source/Defs/DefBeanBase.cs | 19 +++ .../Source/Defs/TTypeTemplateCommonExtends.cs | 21 +++ .../Source/TypeVisitors/GoBinImport.cs | 5 +- .../TypeVisitors/GoDeserializeBinVisitor.cs | 10 +- .../GoDeserializeUnderingVisitor.cs | 130 +++++++++++++++ .../TypeVisitors/GoIsPointerTypeVisitor.cs} | 7 +- .../TypeVisitors/GoSerializeBinVisitor.cs | 22 +++ .../GoSerializeUnderingVisitor.cs | 130 +++++++++++++++ .../Source/TypeVisitors/GoTypeNameVisitor.cs | 6 +- .../TypeVisitors/GoTypeUnderingNameVisitor.cs | 6 +- .../Source/TypeVisitors/ITypeFuncVisitor.cs | 72 ++++----- .../Source/Generate/TemplateRenderBase.cs | 2 +- src/Luban.Server/Luban.Server.csproj | 1 - src/Luban.Server/Source/Program.cs | 25 +-- src/Luban.Server/Templates/proto/go/bean.tpl | 72 ++++----- src/Luban.Server/Templates/proto/go/proto.tpl | 72 ++++----- src/Luban.Server/Templates/proto/go/rpc.tpl | 1 + src/Luban.Server/Templates/proto/go/stub.tpl | 2 +- 33 files changed, 492 insertions(+), 347 deletions(-) create mode 100644 src/Luban.Common/Source/Utils/CommandLineUtil.cs delete mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs rename src/{Luban.Job.Cfg => Luban.Job.Common}/Source/TypeVisitors/GoBinImport.cs (89%) rename src/{Luban.Job.Cfg => Luban.Job.Common}/Source/TypeVisitors/GoDeserializeBinVisitor.cs (51%) create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs rename src/{Luban.Job.Cfg/Source/TypeVisitors/IsGoPointerTypeVisitor.cs => Luban.Job.Common/Source/TypeVisitors/GoIsPointerTypeVisitor.cs} (78%) create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/GoSerializeBinVisitor.cs create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs rename src/{Luban.Job.Cfg => Luban.Job.Common}/Source/TypeVisitors/GoTypeNameVisitor.cs (64%) rename src/{Luban.Job.Cfg => Luban.Job.Common}/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs (91%) diff --git a/src/Luban.Client/Luban.Client.csproj b/src/Luban.Client/Luban.Client.csproj index 448e6b3..3927c30 100644 --- a/src/Luban.Client/Luban.Client.csproj +++ b/src/Luban.Client/Luban.Client.csproj @@ -22,10 +22,6 @@ - - - - diff --git a/src/Luban.ClientServer/Program.cs b/src/Luban.ClientServer/Program.cs index 60ce2f0..55a5666 100644 --- a/src/Luban.ClientServer/Program.cs +++ b/src/Luban.ClientServer/Program.cs @@ -214,8 +214,6 @@ Options: LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel)); s_logger = NLog.LogManager.GetCurrentClassLogger(); - - int processorCount = System.Environment.ProcessorCount; ThreadPool.SetMinThreads(Math.Max(4, processorCount), 5); ThreadPool.SetMaxThreads(Math.Max(16, processorCount * 4), 10); diff --git a/src/Luban.Common/Luban.Common.csproj b/src/Luban.Common/Luban.Common.csproj index 9c34014..69d555e 100644 --- a/src/Luban.Common/Luban.Common.csproj +++ b/src/Luban.Common/Luban.Common.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Luban.Common/Source/Utils/CommandLineUtil.cs b/src/Luban.Common/Source/Utils/CommandLineUtil.cs new file mode 100644 index 0000000..a1d9d26 --- /dev/null +++ b/src/Luban.Common/Source/Utils/CommandLineUtil.cs @@ -0,0 +1,30 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Common.Utils +{ + public static class CommandLineUtil + { + public static T ParseOptions(String[] args) + { + var helpWriter = new StringWriter(); + var parser = new Parser(ps => + { + ps.HelpWriter = helpWriter; + }); + + var result = parser.ParseArguments(args); + if (result.Tag == ParserResultType.NotParsed) + { + Console.Error.WriteLine(helpWriter.ToString()); + Environment.Exit(1); + } + return ((Parsed)result).Value; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs index ae4ded6..e930187 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToErlangLiteralVisitor.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DText type) { - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; return $"#{{{DText.KEY_NAME}=>\"{type.Key}\",{DText.TEXT_NAME}=>\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}"; } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs index 39f3f67..e0abaa0 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLiteralVisitorBase.cs @@ -92,7 +92,7 @@ namespace Luban.Job.Cfg.DataVisitors public virtual string Accept(DDateTime type) { - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; return type.GetUnixTime(ass.TimeZone).ToString(); } } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs index 6eaf58a..47b0dd9 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToLuaLiteralVisitor.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DText type) { - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}=\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}"; } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs index 10be55c..7db072d 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToPythonLiteralVisitor.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DText type) { - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}"; } diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs index de67d24..da74d4f 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToStringVisitor.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DText type) { #if !LUBAN_LITE - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; return $"\"{type.Key}#{type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)}\""; #else return $"\"{type.Key}#{type.RawValue}\""; diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs index fb42d60..ba00de1 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ToXmlLiteralVisitor.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors public override string Accept(DText type) { - var ass = DefAssembly.LocalAssebmly as DefAssembly; + var ass = DefAssembly.LocalAssebmly; return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}='{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}'}}"; } diff --git a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs index e746046..c5aed36 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs @@ -32,22 +32,6 @@ namespace Luban.Job.Cfg.Defs } #if !LUBAN_LITE - public string GoBinImport - { - get - { - var imports = new HashSet(); - if (IsAbstractType) - { - imports.Add("errors"); - } - foreach (var f in Fields) - { - f.CType.Apply(TypeVisitors.GoBinImport.Ins, imports); - } - return string.Join('\n', imports.Select(im => $"import \"{im}\"")); - } - } public string GoJsonImport { diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs index b14f052..5fdf21a 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs @@ -11,7 +11,7 @@ namespace Luban.Job.Cfg.TypeVisitors { if (type.IsNullable) { - return $"{{ if {bufName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", bufName)}; {varName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; + return $"{{ if {bufName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", bufName)}; {varName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; } else { diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs index 3f8b3fa..3182171 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.TypeVisitors if (type.IsNullable) { var jsonObjName = $"__json_{fieldName}__"; - return $"{{ var _ok_ bool; var {jsonObjName} interface{{}}; if {jsonObjName}, _ok_ = {bufName}[\"{fieldName}\"]; !_ok_ || {jsonObjName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", jsonObjName)}; {varName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; + return $"{{ var _ok_ bool; var {jsonObjName} interface{{}}; if {jsonObjName}, _ok_ = {bufName}[\"{fieldName}\"]; !_ok_ || {jsonObjName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", jsonObjName)}; {varName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; } else { diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs deleted file mode 100644 index 51dc13d..0000000 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs +++ /dev/null @@ -1,152 +0,0 @@ -using Luban.Job.Common.Types; -using Luban.Job.Common.TypeVisitors; - -namespace Luban.Job.Cfg.TypeVisitors -{ - class GoDeserializeUnderingVisitor : ITypeFuncVisitor - { - public static GoDeserializeUnderingVisitor Ins { get; } = new GoDeserializeUnderingVisitor(); - - public string Accept(TBool type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadBool(); err != nil {{ return }} }}"; - } - - public string Accept(TByte type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadByte(); err != nil {{ return }} }}"; - } - - public string Accept(TShort type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadShort(); err != nil {{ return }} }}"; - } - - public string Accept(TFshort type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadFshort(); err != nil {{ return }} }}"; - } - - public string Accept(TInt type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadInt(); err != nil {{ return }} }}"; - } - - public string Accept(TFint type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadFint(); err != nil {{ return }} }}"; - } - - public string Accept(TLong type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadLong(); err != nil {{ return }} }}"; - } - - public string Accept(TFlong type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadFlong(); err != nil {{ return }} }}"; - } - - public string Accept(TFloat type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadFloat(); err != nil {{ return }} }}"; - } - - public string Accept(TDouble type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadDouble(); err != nil {{ return }} }}"; - } - - public string Accept(TEnum type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadInt(); err != nil {{ return }} }}"; - } - - public string Accept(TString type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadString(); err != nil {{ return }} }}"; - } - - public string Accept(TBytes type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}"; - } - - public string Accept(TText type, string fieldName, string bufName) - { - return $"{{ if _, err = {bufName}.ReadString(); err != nil {{ return }}; if {fieldName}, err = {bufName}.ReadString(); err != nil {{ return }} }}"; - } - - public string Accept(TBean type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {($"New{type.Bean.GoFullName}({bufName})")}; err != nil {{ return }} }}"; - } - - - private string GenList(TType elementType, string fieldName, string bufName) - { - return $@" {{ - {fieldName} = make([]{elementType.Apply(GoTypeNameVisitor.Ins)}, 0) - var _n_ int - if _n_, err = {bufName}.ReadSize(); err != nil {{return}} - for i := 0 ; i < _n_ ; i++ {{ - var _e_ {elementType.Apply(GoTypeNameVisitor.Ins)} - {elementType.Apply(GoDeserializeBinVisitor.Ins, "_e_", bufName)} - {fieldName} = append({fieldName}, _e_) - }} - }} -"; - } - - public string Accept(TArray type, string fieldName, string bufName) - { - return GenList(type.ElementType, fieldName, bufName); - } - - public string Accept(TList type, string fieldName, string bufName) - { - return GenList(type.ElementType, fieldName, bufName); - } - - public string Accept(TSet type, string fieldName, string bufName) - { - return GenList(type.ElementType, fieldName, bufName); - } - - public string Accept(TMap type, string fieldName, string bufName) - { - return $@"{{ - {fieldName} = make({type.Apply(GoTypeNameVisitor.Ins)}) - var _n_ int - if _n_, err = {bufName}.ReadSize(); err != nil {{return}} - for i := 0 ; i < _n_ ; i++ {{ - var _key_ {type.KeyType.Apply(GoTypeNameVisitor.Ins)} - {type.KeyType.Apply(GoDeserializeBinVisitor.Ins, "_key_", bufName)} - var _value_ {type.ValueType.Apply(GoTypeNameVisitor.Ins)} - {type.ValueType.Apply(GoDeserializeBinVisitor.Ins, "_value_", bufName)} - {fieldName}[_key_] = _value_ - }} - }}"; - } - - public string Accept(TVector2 type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadVector2(); err != nil {{ return }} }}"; - } - - public string Accept(TVector3 type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadVector3(); err != nil {{ return }} }}"; - } - - public string Accept(TVector4 type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadVector4(); err != nil {{ return }} }}"; - } - - public string Accept(TDateTime type, string fieldName, string bufName) - { - return $"{{ if {fieldName}, err = {bufName}.ReadInt(); err != nil {{ return }} }}"; - } - } -} diff --git a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs index ccc9dbd..4fb3ee1 100644 --- a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs @@ -133,21 +133,6 @@ namespace Luban.Job.Cfg.Utils } } - public static string GoDefineType(TType type) - { - return type.Apply(GoTypeNameVisitor.Ins); - } - - public static string GoDeserializeType(TBean type, string bufName) - { - return $"New{type.Bean.GoFullName}({bufName})"; - } - - public static string GoDeserializeField(TType type, string name, string bufName) - { - return type.Apply(GoDeserializeBinVisitor.Ins, name, bufName); - } - public static string GoDeserializeJsonField(TType type, string name, string fieldName, string bufName) { return type.Apply(GoDeserializeJsonVisitor.Ins, name, fieldName, bufName); diff --git a/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs b/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs index 2ae8289..aef6cd1 100644 --- a/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs @@ -58,6 +58,25 @@ namespace Luban.Job.Common.Defs } } +#if !LUBAN_LITE + public string GoBinImport + { + get + { + var imports = new HashSet(); + if (IsAbstractType) + { + imports.Add("errors"); + } + foreach (var f in Fields) + { + f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports); + } + return string.Join('\n', imports.Select(im => $"import \"{im}\"")); + } + } +#endif + protected abstract DefFieldBase CreateField(Field f, int idOffset); public void CollectHierarchyNotAbstractChildren(List children) diff --git a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs index cfe6c6f..fff48dd 100644 --- a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs +++ b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs @@ -184,6 +184,27 @@ namespace Luban.Job.Common.Defs return type.Apply(ErlangDefineTypeNameVisitor.Ins); } + + public static string GoDefineType(TType type) + { + return type.Apply(GoTypeNameVisitor.Ins); + } + + public static string GoDeserializeType(TBean type, string bufName) + { + return $"Deserialize{type.Bean.GoFullName}({bufName})"; + } + + public static string GoSerializeField(TType type, string name, string bufName) + { + return type.Apply(GoSerializeBinVisitor.Ins, name, bufName); + } + + public static string GoDeserializeField(TType type, string name, string bufName, string err) + { + return type.Apply(GoDeserializeBinVisitor.Ins, name, bufName, err); + } + public static bool HasTag(dynamic obj, string attrName) { return obj.HasTag(attrName); diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoBinImport.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoBinImport.cs similarity index 89% rename from src/Luban.Job.Cfg/Source/TypeVisitors/GoBinImport.cs rename to src/Luban.Job.Common/Source/TypeVisitors/GoBinImport.cs index e627243..a2a419e 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoBinImport.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoBinImport.cs @@ -1,10 +1,9 @@ using Luban.Job.Common.Types; -using Luban.Job.Common.TypeVisitors; using System.Collections.Generic; -namespace Luban.Job.Cfg.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class GoBinImport : DecoratorActionVisitor> + public class GoBinImport : DecoratorActionVisitor> { public static GoBinImport Ins { get; } = new(); diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeBinVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs similarity index 51% rename from src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeBinVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs index 9d8dce5..c02f549 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeBinVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs @@ -1,21 +1,21 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Cfg.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class GoDeserializeBinVisitor : DecoratorFuncVisitor + public class GoDeserializeBinVisitor : DecoratorFuncVisitor { public static GoDeserializeBinVisitor Ins { get; } = new GoDeserializeBinVisitor(); - public override string DoAccept(TType type, string fieldName, string bufName) + public override string DoAccept(TType type, string fieldName, string bufName, string err) { if (type.IsNullable) { - return $"{{ var __exists__ bool; if __exists__, err = {bufName}.ReadBool(); err != nil {{ return }}; if __exists__ {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeUnderingVisitor.Ins, "__x__", bufName)}; {fieldName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; + return $"{{ var __exists__ bool; if __exists__, {err} = {bufName}.ReadBool(); {err} != nil {{ return }}; if __exists__ {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoSerializeUnderingVisitor.Ins, "__x__", bufName)}; {fieldName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; } else { - return type.Apply(GoDeserializeUnderingVisitor.Ins, (string)fieldName, bufName); + return type.Apply(GoDeserializeUnderingVisitor.Ins, fieldName, bufName, err); } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs new file mode 100644 index 0000000..7c7b876 --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs @@ -0,0 +1,130 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Common.TypeVisitors +{ + public class GoDeserializeUnderingVisitor : ITypeFuncVisitor + { + public static GoDeserializeUnderingVisitor Ins { get; } = new(); + + public string Accept(TBool type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadBool(); {err} != nil {{ return }} }}"; + } + + public string Accept(TByte type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadByte(); {err} != nil {{ return }} }}"; + } + + public string Accept(TShort type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadShort(); {err} != nil {{ return }} }}"; + } + + public string Accept(TFshort type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadFshort(); {err} != nil {{ return }} }}"; + } + + public string Accept(TInt type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}"; + } + + public string Accept(TFint type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadFint(); {err} != nil {{ return }} }}"; + } + + public string Accept(TLong type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadLong(); {err} != nil {{ return }} }}"; + } + + public string Accept(TFlong type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadFlong(); {err} != nil {{ return }} }}"; + } + + public string Accept(TFloat type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadFloat(); {err} != nil {{ return }} }}"; + } + + public string Accept(TDouble type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadDouble(); {err} != nil {{ return }} }}"; + } + + public string Accept(TEnum type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}"; + } + + public string Accept(TString type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ return }} }}"; + } + + public string Accept(TText type, string fieldName, string bufName, string err) + { + return $"{{ if _, {err} = {bufName}.ReadString(); {err} != nil {{ return }}; if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ return }} }}"; + } + + public string Accept(TBytes type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadBytes(); {err} != nil {{ return }} }}"; + } + + public string Accept(TVector2 type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadVector2(); {err} != nil {{ return }} }}"; + } + + public string Accept(TVector3 type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadVector3(); {err} != nil {{ return }} }}"; + } + + public string Accept(TVector4 type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadVector4(); {err} != nil {{ return }} }}"; + } + + public string Accept(TDateTime type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}"; + } + + public string Accept(TBean type, string fieldName, string bufName, string err) + { + return $"{{ if {fieldName}, {err} = {($"Deserialize{type.Bean.GoFullName}({bufName})")}; {err} != nil {{ return }} }}"; + } + + private string GenList(TType elementType, string fieldName, string bufName, string err) + { + return $@"{{{fieldName} = make([]{elementType.Apply(GoTypeNameVisitor.Ins)}, 0); var _n_ int; if _n_, {err} = {bufName}.ReadSize(); {err} != nil {{return}}; for i := 0 ; i < _n_ ; i++ {{ var _e_ {elementType.Apply(GoTypeNameVisitor.Ins)}; {elementType.Apply(GoDeserializeBinVisitor.Ins, "_e_", bufName, err)}; {fieldName} = append({fieldName}, _e_) }} }}"; + } + + public string Accept(TArray type, string fieldName, string bufName, string err) + { + return GenList(type.ElementType, fieldName, bufName, err); + } + + public string Accept(TList type, string fieldName, string bufName, string err) + { + return GenList(type.ElementType, fieldName, bufName, err); + } + + public string Accept(TSet type, string fieldName, string bufName, string err) + { + return GenList(type.ElementType, fieldName, bufName, err); + } + + public string Accept(TMap type, string fieldName, string bufName, string err) + { + return $@"{{ {fieldName} = make({type.Apply(GoTypeNameVisitor.Ins)}); var _n_ int; if _n_, {err} = {bufName}.ReadSize(); {err} != nil {{return}}; for i := 0 ; i < _n_ ; i++ {{ var _key_ {type.KeyType.Apply(GoTypeNameVisitor.Ins)}; {type.KeyType.Apply(GoDeserializeBinVisitor.Ins, "_key_", bufName, err)}; var _value_ {type.ValueType.Apply(GoTypeNameVisitor.Ins)}; {type.ValueType.Apply(GoDeserializeBinVisitor.Ins, "_value_", bufName, err)}; {fieldName}[_key_] = _value_}} }}"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/IsGoPointerTypeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoIsPointerTypeVisitor.cs similarity index 78% rename from src/Luban.Job.Cfg/Source/TypeVisitors/IsGoPointerTypeVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/GoIsPointerTypeVisitor.cs index 50b5125..3d2aee2 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/IsGoPointerTypeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoIsPointerTypeVisitor.cs @@ -1,11 +1,10 @@ using Luban.Job.Common.Types; -using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Cfg.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class IsGoPointerTypeVisitor : DecoratorFuncVisitor + public class GoIsPointerTypeVisitor : DecoratorFuncVisitor { - public static IsGoPointerTypeVisitor Ins { get; } = new(); + public static GoIsPointerTypeVisitor Ins { get; } = new(); public override bool DoAccept(TType type) { diff --git a/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeBinVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeBinVisitor.cs new file mode 100644 index 0000000..17d2de5 --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeBinVisitor.cs @@ -0,0 +1,22 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Common.TypeVisitors +{ + public class GoSerializeBinVisitor : DecoratorFuncVisitor + { + public static GoSerializeBinVisitor Ins { get; } = new(); + + public override string DoAccept(TType type, string fieldName, string bufName) + { + if (type.IsNullable) + { + return $"if {bufName} != nil {{ {bufName}.WriteBool(true); {type.Apply(GoSerializeUnderingVisitor.Ins, (type.Apply(GoIsPointerTypeVisitor.Ins) ? $"*{fieldName}" : fieldName), bufName)} }} else {{ {bufName}.WriteBool(false) }}"; + } + else + { + return type.Apply(GoSerializeUnderingVisitor.Ins, fieldName, bufName); + } + } + } +} diff --git a/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs new file mode 100644 index 0000000..6445dd6 --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs @@ -0,0 +1,130 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Common.TypeVisitors +{ + public class GoSerializeUnderingVisitor : ITypeFuncVisitor + { + public static GoSerializeUnderingVisitor Ins { get; } = new(); + + public string Accept(TBool type, string fieldName, string bufName) + { + return $"{bufName}.WriteBool({fieldName})"; + } + + public string Accept(TByte type, string fieldName, string bufName) + { + return $"{bufName}.WriteByte({fieldName})"; + } + + public string Accept(TShort type, string fieldName, string bufName) + { + return $"{bufName}.WriteShort({fieldName})"; + } + + public string Accept(TFshort type, string fieldName, string bufName) + { + return $"{bufName}.WriteFshort({fieldName})"; + } + + public string Accept(TInt type, string fieldName, string bufName) + { + return $"{bufName}.WriteInt({fieldName})"; + } + + public string Accept(TFint type, string fieldName, string bufName) + { + return $"{bufName}.WriteFint({fieldName})"; + } + + public string Accept(TLong type, string fieldName, string bufName) + { + return $"{bufName}.WriteLong({fieldName})"; + } + + public string Accept(TFlong type, string fieldName, string bufName) + { + return $"{bufName}.WriteFlong({fieldName})"; + } + + public string Accept(TFloat type, string fieldName, string bufName) + { + return $"{bufName}.WriteFloat({fieldName})"; + } + + public string Accept(TDouble type, string fieldName, string bufName) + { + return $"{bufName}.WriteDouble({fieldName})"; + } + + public string Accept(TEnum type, string fieldName, string bufName) + { + return $"{bufName}.WriteInt({fieldName})"; + } + + public string Accept(TString type, string fieldName, string bufName) + { + return $"{bufName}.WriteString({fieldName})"; + } + + public string Accept(TText type, string fieldName, string bufName) + { + throw new System.NotSupportedException(); + } + + public string Accept(TBytes type, string fieldName, string bufName) + { + return $"{bufName}.WriteBytes({fieldName})"; + } + + public string Accept(TVector2 type, string fieldName, string bufName) + { + return $"{bufName}.WriteVector2({fieldName})"; + } + + public string Accept(TVector3 type, string fieldName, string bufName) + { + return $"{bufName}.WriteVector3({fieldName})"; + } + + public string Accept(TVector4 type, string fieldName, string bufName) + { + return $"{bufName}.WriteVector4({fieldName})"; + } + + public string Accept(TDateTime type, string fieldName, string bufName) + { + throw new System.NotSupportedException(); + } + + public string Accept(TBean type, string fieldName, string bufName) + { + return $"Serialize{type.Bean.GoFullName}({fieldName}, {bufName})"; + } + + private string GenList(TType elementType, string fieldName, string bufName) + { + return $@"{{ {bufName}.WriteSize(len({fieldName})); for _, _e_ := range({fieldName}) {{ {elementType.Apply(GoSerializeBinVisitor.Ins, "_e_", bufName)} }} }}"; + } + + public string Accept(TArray type, string fieldName, string bufName) + { + return GenList(type.ElementType, fieldName, bufName); + } + + public string Accept(TList type, string fieldName, string bufName) + { + return GenList(type.ElementType, fieldName, bufName); + } + + public string Accept(TSet type, string fieldName, string bufName) + { + return GenList(type.ElementType, fieldName, bufName); + } + + public string Accept(TMap type, string fieldName, string bufName) + { + return $@"{{{bufName}.WriteSize(len({fieldName})); for _k_, _v_ := range({fieldName}) {{ {type.KeyType.Apply(GoSerializeBinVisitor.Ins, "_k_", bufName)}; {type.ValueType.Apply(GoSerializeBinVisitor.Ins, "_v_", bufName)} }} }}"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoTypeNameVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoTypeNameVisitor.cs similarity index 64% rename from src/Luban.Job.Cfg/Source/TypeVisitors/GoTypeNameVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/GoTypeNameVisitor.cs index ba638ea..20ae310 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoTypeNameVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoTypeNameVisitor.cs @@ -1,16 +1,16 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Cfg.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class GoTypeNameVisitor : DecoratorFuncVisitor + public class GoTypeNameVisitor : DecoratorFuncVisitor { public static GoTypeNameVisitor Ins { get; } = new GoTypeNameVisitor(); public override string DoAccept(TType type) { var s = type.Apply(GoTypeUnderingNameVisitor.Ins); - return type.Apply(IsGoPointerTypeVisitor.Ins) ? "*" + s : s; + return type.Apply(GoIsPointerTypeVisitor.Ins) ? "*" + s : s; } } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs similarity index 91% rename from src/Luban.Job.Cfg/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs rename to src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs index b90c6a9..720aea4 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs @@ -1,9 +1,9 @@ using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; -namespace Luban.Job.Cfg.TypeVisitors +namespace Luban.Job.Common.TypeVisitors { - class GoTypeUnderingNameVisitor : ITypeFuncVisitor + public class GoTypeUnderingNameVisitor : ITypeFuncVisitor { public static GoTypeUnderingNameVisitor Ins { get; } = new GoTypeUnderingNameVisitor(); @@ -79,7 +79,7 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TBean type) { - return type.Bean.IsAbstractType ? $"interface{{}}" : $"*{type.Bean.GoFullName}"; + return type.Bean.IsAbstractType ? $"serialization.ISerializable" : $"*{type.Bean.GoFullName}"; } public string Accept(TArray type) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/ITypeFuncVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/ITypeFuncVisitor.cs index 1b755c1..d5ac39d 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/ITypeFuncVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/ITypeFuncVisitor.cs @@ -28,9 +28,17 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TString type); + TR Accept(TText type); + TR Accept(TBytes type); - TR Accept(TText type); + TR Accept(TVector2 type); + + TR Accept(TVector3 type); + + TR Accept(TVector4 type); + + TR Accept(TDateTime type); TR Accept(TBean type); @@ -41,14 +49,6 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TSet type); TR Accept(TMap type); - - TR Accept(TVector2 type); - - TR Accept(TVector3 type); - - TR Accept(TVector4 type); - - TR Accept(TDateTime type); } public interface ITypeFuncVisitor @@ -77,9 +77,17 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TString type, T x); + TR Accept(TText type, T x); + TR Accept(TBytes type, T x); - TR Accept(TText type, T x); + TR Accept(TVector2 type, T x); + + TR Accept(TVector3 type, T x); + + TR Accept(TVector4 type, T x); + + TR Accept(TDateTime type, T x); TR Accept(TBean type, T x); @@ -90,14 +98,6 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TSet type, T x); TR Accept(TMap type, T x); - - TR Accept(TVector2 type, T x); - - TR Accept(TVector3 type, T x); - - TR Accept(TVector4 type, T x); - - TR Accept(TDateTime type, T x); } public interface ITypeFuncVisitor @@ -126,9 +126,17 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TString type, T x, T2 y); + TR Accept(TText type, T x, T2 y); + TR Accept(TBytes type, T x, T2 y); - TR Accept(TText type, T x, T2 y); + TR Accept(TVector2 type, T x, T2 y); + + TR Accept(TVector3 type, T x, T2 y); + + TR Accept(TVector4 type, T x, T2 y); + + TR Accept(TDateTime type, T x, T2 y); TR Accept(TBean type, T x, T2 y); @@ -139,14 +147,6 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TSet type, T x, T2 y); TR Accept(TMap type, T x, T2 y); - - TR Accept(TVector2 type, T x, T2 y); - - TR Accept(TVector3 type, T x, T2 y); - - TR Accept(TVector4 type, T x, T2 y); - - TR Accept(TDateTime type, T x, T2 y); } public interface ITypeFuncVisitor @@ -175,9 +175,17 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TString type, T x, T2 y, T3 z); + TR Accept(TText type, T x, T2 y, T3 z); + TR Accept(TBytes type, T x, T2 y, T3 z); - TR Accept(TText type, T x, T2 y, T3 z); + TR Accept(TVector2 type, T x, T2 y, T3 z); + + TR Accept(TVector3 type, T x, T2 y, T3 z); + + TR Accept(TVector4 type, T x, T2 y, T3 z); + + TR Accept(TDateTime type, T x, T2 y, T3 z); TR Accept(TBean type, T x, T2 y, T3 z); @@ -188,13 +196,5 @@ namespace Luban.Job.Common.TypeVisitors TR Accept(TSet type, T x, T2 y, T3 z); TR Accept(TMap type, T x, T2 y, T3 z); - - TR Accept(TVector2 type, T x, T2 y, T3 z); - - TR Accept(TVector3 type, T x, T2 y, T3 z); - - TR Accept(TVector4 type, T x, T2 y, T3 z); - - TR Accept(TDateTime type, T x, T2 y, T3 z); } } diff --git a/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs b/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs index 10fc8d9..15df6ce 100644 --- a/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs +++ b/src/Luban.Job.Proto/Source/Generate/TemplateRenderBase.cs @@ -17,7 +17,7 @@ namespace Luban.Job.Proto.Generate protected override string Render(DefEnum e) { var template = StringTemplateUtil.GetTemplate($"common/{RenderTemplateDir}/enum"); - var result = template.Render(e); + var result = template.RenderCode(e); return result; } diff --git a/src/Luban.Server/Luban.Server.csproj b/src/Luban.Server/Luban.Server.csproj index c3eece8..ec2b479 100644 --- a/src/Luban.Server/Luban.Server.csproj +++ b/src/Luban.Server/Luban.Server.csproj @@ -35,7 +35,6 @@ - diff --git a/src/Luban.Server/Source/Program.cs b/src/Luban.Server/Source/Program.cs index b81d945..2d1b742 100644 --- a/src/Luban.Server/Source/Program.cs +++ b/src/Luban.Server/Source/Program.cs @@ -21,25 +21,8 @@ namespace Luban.Server [Option('t', "template_search_path", Required = false, HelpText = "additional template search path")] public string TemplateSearchPath { get; set; } - [Option("timezone", Required = false, HelpText = "default timezone")] - public string DefaultTimeZone { get; set; } = "Asia/Shanghai"; - } - - private static CommandLineOptions ParseOptions(String[] args) - { - var helpWriter = new StringWriter(); - var parser = new Parser(ps => - { - ps.HelpWriter = helpWriter; - }); - - var result = parser.ParseArguments(args); - if (result.Tag == ParserResultType.NotParsed) - { - Console.Error.WriteLine(helpWriter.ToString()); - Environment.Exit(1); - } - return ((Parsed)result).Value; + [Option("i10n:default_timezone", Required = false, HelpText = "default timezone")] + public string L10nDefaultTimeZone { get; set; } = "Asia/Shanghai"; } static void Main(string[] args) @@ -47,7 +30,7 @@ namespace Luban.Server ConsoleWindow.EnableQuickEditMode(false); Console.OutputEncoding = System.Text.Encoding.UTF8; - var options = ParseOptions(args); + var options = CommandLineUtil.ParseOptions(args); if (!string.IsNullOrEmpty(options.TemplateSearchPath)) { @@ -59,7 +42,7 @@ namespace Luban.Server System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); - TimeZoneUtil.DefaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(options.DefaultTimeZone); + TimeZoneUtil.DefaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(options.L10nDefaultTimeZone); GenServer.Ins.Start(false, options.Port, ProtocolStub.Factories); diff --git a/src/Luban.Server/Templates/proto/go/bean.tpl b/src/Luban.Server/Templates/proto/go/bean.tpl index f1788dd..ce7c070 100644 --- a/src/Luban.Server/Templates/proto/go/bean.tpl +++ b/src/Luban.Server/Templates/proto/go/bean.tpl @@ -2,7 +2,7 @@ go_full_name = x.go_full_name parent_def_type = x.parent_def_type is_abstract_type = x.is_abstract_type - export_fields = x.export_fields + hierarchy_fields = x.hierarchy_fields hierarchy_not_abstract_children = x.hierarchy_not_abstract_children -}} @@ -15,59 +15,59 @@ import ( {{x.go_bin_import}} type {{go_full_name}} struct { - {{~if parent_def_type~}} - {{parent_def_type.go_full_name}} - {{~end~}} - {{~for field in export_fields ~}} + {{~for field in hierarchy_fields ~}} {{field.convention_name}} {{go_define_type field.ctype}} {{~end~}} } -{{~if !is_abstract_type~}} -func ({{go_full_name}}) GetTypeId() int { +const TypeId_{{go_full_name}} = {{x.id}} + +func ({{go_full_name}}) GetTypeId() int32 { return {{x.id}} } -{{~end~}} + +func (_v {{go_full_name}})Serialize(_buf *serialization.ByteBuf) { + {{~for field in hierarchy_fields ~}} + {{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}} + {{~end~}} +} + +func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) { + {{~for field in hierarchy_fields ~}} + {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}} + {{~end~}} + return +} {{~if is_abstract_type~}} -func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v interface{}, err error) { +func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { + _buf.WriteInt(_v.GetTypeId()) + _v.Serialize(_buf) +} + +func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) { var id int32 if id, err = _buf.ReadInt() ; err != nil { return } switch id { {{~for child in hierarchy_not_abstract_children~}} - case {{child.id}}: return New{{child.go_full_name}}(_buf) + case {{child.id}}: _v = {{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return } {{~end~}} default: return nil, errors.New("unknown type id") } - return } - -func New{{go_full_name}}_Body(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) { - _v = &{{go_full_name}}{} -{{~if parent_def_type~}} - var _p *{{parent_def_type.go_full_name}} - if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return } - _v.{{parent_def_type.go_full_name}} = *_p -{{~end~}} - {{~for field in export_fields ~}} - {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}} - {{~end~}} - return -} - {{~else~}} -func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) { - _v = &{{go_full_name}}{} -{{~if parent_def_type~}} - var _p *{{parent_def_type.go_full_name}} - if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return } - _v.{{parent_def_type.go_full_name}} = *_p -{{~end~}} - {{~for field in export_fields ~}} - {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}} - {{~end~}} - return +func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { + _v.Serialize(_buf) +} + +func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) { + v := &{{go_full_name}}{} + if err := v.Deserialize(_buf); err == nil { + return v, nil + } else { + return nil, err + } } {{~end~}} diff --git a/src/Luban.Server/Templates/proto/go/proto.tpl b/src/Luban.Server/Templates/proto/go/proto.tpl index f1788dd..ce7c070 100644 --- a/src/Luban.Server/Templates/proto/go/proto.tpl +++ b/src/Luban.Server/Templates/proto/go/proto.tpl @@ -2,7 +2,7 @@ go_full_name = x.go_full_name parent_def_type = x.parent_def_type is_abstract_type = x.is_abstract_type - export_fields = x.export_fields + hierarchy_fields = x.hierarchy_fields hierarchy_not_abstract_children = x.hierarchy_not_abstract_children -}} @@ -15,59 +15,59 @@ import ( {{x.go_bin_import}} type {{go_full_name}} struct { - {{~if parent_def_type~}} - {{parent_def_type.go_full_name}} - {{~end~}} - {{~for field in export_fields ~}} + {{~for field in hierarchy_fields ~}} {{field.convention_name}} {{go_define_type field.ctype}} {{~end~}} } -{{~if !is_abstract_type~}} -func ({{go_full_name}}) GetTypeId() int { +const TypeId_{{go_full_name}} = {{x.id}} + +func ({{go_full_name}}) GetTypeId() int32 { return {{x.id}} } -{{~end~}} + +func (_v {{go_full_name}})Serialize(_buf *serialization.ByteBuf) { + {{~for field in hierarchy_fields ~}} + {{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}} + {{~end~}} +} + +func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) { + {{~for field in hierarchy_fields ~}} + {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}} + {{~end~}} + return +} {{~if is_abstract_type~}} -func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v interface{}, err error) { +func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { + _buf.WriteInt(_v.GetTypeId()) + _v.Serialize(_buf) +} + +func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) { var id int32 if id, err = _buf.ReadInt() ; err != nil { return } switch id { {{~for child in hierarchy_not_abstract_children~}} - case {{child.id}}: return New{{child.go_full_name}}(_buf) + case {{child.id}}: _v = {{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return } {{~end~}} default: return nil, errors.New("unknown type id") } - return } - -func New{{go_full_name}}_Body(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) { - _v = &{{go_full_name}}{} -{{~if parent_def_type~}} - var _p *{{parent_def_type.go_full_name}} - if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return } - _v.{{parent_def_type.go_full_name}} = *_p -{{~end~}} - {{~for field in export_fields ~}} - {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}} - {{~end~}} - return -} - {{~else~}} -func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) { - _v = &{{go_full_name}}{} -{{~if parent_def_type~}} - var _p *{{parent_def_type.go_full_name}} - if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return } - _v.{{parent_def_type.go_full_name}} = *_p -{{~end~}} - {{~for field in export_fields ~}} - {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}} - {{~end~}} - return +func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { + _v.Serialize(_buf) +} + +func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) { + v := &{{go_full_name}}{} + if err := v.Deserialize(_buf); err == nil { + return v, nil + } else { + return nil, err + } } {{~end~}} diff --git a/src/Luban.Server/Templates/proto/go/rpc.tpl b/src/Luban.Server/Templates/proto/go/rpc.tpl index a73b61c..54a2b1f 100644 --- a/src/Luban.Server/Templates/proto/go/rpc.tpl +++ b/src/Luban.Server/Templates/proto/go/rpc.tpl @@ -1 +1,2 @@ +package {{x.top_module}} // rpc {{x.full_name}} \ No newline at end of file diff --git a/src/Luban.Server/Templates/proto/go/stub.tpl b/src/Luban.Server/Templates/proto/go/stub.tpl index d067d0b..5faea50 100644 --- a/src/Luban.Server/Templates/proto/go/stub.tpl +++ b/src/Luban.Server/Templates/proto/go/stub.tpl @@ -1 +1 @@ -// stub \ No newline at end of file +package {{namespace}}