From 813e129352612dfe054f2a42715a03e8894b6375 Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 29 Nov 2021 16:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4=20proto=20go=E5=92=8C=20cfg=20go=E7=9A=84=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Common/Source/Utils/TypeUtil.cs | 6 +- src/Luban.Job.Cfg/Source/Defs/DefBean.cs | 20 ++++- .../GoDeserializeJsonUndering2Visitor.cs | 54 ++++++------ .../GoDeserializeJsonUnderingVisitor.cs | 88 +++++++++---------- .../Source/Defs/DefBeanBase.cs | 6 +- .../TypeVisitors/GoDeserializeBinVisitor.cs | 2 +- .../GoDeserializeUnderingVisitor.cs | 42 ++++----- .../GoSerializeUnderingVisitor.cs | 2 +- .../TypeVisitors/GoTypeUnderingNameVisitor.cs | 2 +- .../Templates/config/go_bin/bean.tpl | 63 ++++++------- .../Templates/config/go_json/bean.tpl | 56 +++++------- src/Luban.Server/Templates/proto/go/bean.tpl | 15 ++-- 12 files changed, 175 insertions(+), 181 deletions(-) diff --git a/src/Luban.Common/Source/Utils/TypeUtil.cs b/src/Luban.Common/Source/Utils/TypeUtil.cs index 4ddcd12..e3a1ab8 100644 --- a/src/Luban.Common/Source/Utils/TypeUtil.cs +++ b/src/Luban.Common/Source/Utils/TypeUtil.cs @@ -113,17 +113,17 @@ namespace Luban.Common.Utils public static string MakeGoFullName(string module, string name) { - return MakeGoNamespace(module) + "_" + name; + return MakeGoNamespace(module) + name; } public static string MakePyFullName(string module, string name) { - return module.Replace('.', '_') + "_" + name; + return module.Replace('.', '_') + name; } public static string MakeRustFullName(string module, string name) { - return MakeGoNamespace(module) + "_" + name; + return MakeGoNamespace(module) + name; } public static string MakeNamespace(string module, string subModule) diff --git a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs index c5aed36..b768148 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs @@ -32,17 +32,33 @@ namespace Luban.Job.Cfg.Defs } #if !LUBAN_LITE + public override string GoBinImport + { + get + { + var imports = new HashSet(); + if (IsAbstractType || this.HierarchyExportFields.Count > 0) + { + imports.Add("errors"); + } + foreach (var f in HierarchyExportFields) + { + f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports); + } + return string.Join('\n', imports.Select(im => $"import \"{im}\"")); + } + } public string GoJsonImport { get { var imports = new HashSet(); - if (IsAbstractType) + if (IsAbstractType || this.HierarchyExportFields.Count > 0) { imports.Add("errors"); } - foreach (var f in Fields) + foreach (var f in HierarchyExportFields) { f.CType.Apply(TypeVisitors.GoJsonImport.Ins, imports); } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs index b0f47ad..c680a3b 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs @@ -79,39 +79,14 @@ namespace Luban.Job.Cfg.TypeVisitors return DeserializeString(type, varName, bufName); } - public string Accept(TBytes type, string varName, string bufName) - { - //return $"{{ if {varName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}"; - throw new System.NotSupportedException(); - } - public string Accept(TText type, string varName, string bufName) { return $"{{var _ok_ bool; var __json_text__ map[string]interface{{}}; if __json_text__, _ok_ = {bufName}.(map[string]interface{{}}) ; !_ok_ {{ err = errors.New(\"{varName} error\"); return }}; {DeserializeString(type, "_", $"__json_text__[\"{DText.KEY_NAME}\"]")}; {DeserializeString(type, varName, $"__json_text__[\"{DText.TEXT_NAME}\"]")} }}"; } - public string Accept(TBean type, string varName, string bufName) - { - return $"{{ var _ok_ bool; var _x_ map[string]interface{{}}; if _x_, _ok_ = {bufName}.(map[string]interface{{}}); !_ok_ {{ err = errors.New(\"{varName} error\"); return }}; if {varName}, err = {($"New{ type.Bean.GoFullName}(_x_)")}; err != nil {{ return }} }}"; - } - - public string Accept(TArray type, string varName, string bufName) - { - throw new System.NotSupportedException(); - } - - public string Accept(TList type, string varName, string bufName) - { - throw new System.NotSupportedException(); - } - - public string Accept(TSet type, string varName, string bufName) - { - throw new System.NotSupportedException(); - } - - public string Accept(TMap type, string varName, string bufName) + public string Accept(TBytes type, string varName, string bufName) { + //return $"{{ if {varName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}"; throw new System.NotSupportedException(); } @@ -155,5 +130,30 @@ namespace Luban.Job.Cfg.TypeVisitors { return DeserializeNumber(type, varName, bufName); } + + public string Accept(TBean type, string varName, string bufName) + { + return $"{{ var _ok_ bool; var _x_ map[string]interface{{}}; if _x_, _ok_ = {bufName}.(map[string]interface{{}}); !_ok_ {{ err = errors.New(\"{varName} error\"); return }}; if {varName}, err = {($"Deserialize{ type.Bean.GoFullName}(_x_)")}; err != nil {{ return }} }}"; + } + + public string Accept(TArray type, string varName, string bufName) + { + throw new System.NotSupportedException(); + } + + public string Accept(TList type, string varName, string bufName) + { + throw new System.NotSupportedException(); + } + + public string Accept(TSet type, string varName, string bufName) + { + throw new System.NotSupportedException(); + } + + public string Accept(TMap type, string varName, string bufName) + { + throw new System.NotSupportedException(); + } } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs index 3087187..5aba279 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs @@ -79,20 +79,61 @@ namespace Luban.Job.Cfg.TypeVisitors return DeserializeString(type, varName, fieldName, bufName); } + public string Accept(TText type, string varName, string fieldName, string bufName) + { + return $"{{var _ok_ bool; var __json_text__ map[string]interface{{}}; if __json_text__, _ok_ = {bufName}[\"{fieldName}\"].(map[string]interface{{}}) ; !_ok_ {{ err = errors.New(\"{varName} error\"); return }}; {DeserializeString(type, "_", DText.KEY_NAME, "__json_text__")}; {DeserializeString(type, varName, DText.TEXT_NAME, "__json_text__")} }}"; + } + public string Accept(TBytes type, string varName, string fieldName, string bufName) { //return $"{{ if {varName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}"; throw new System.NotSupportedException(); } - public string Accept(TText type, string varName, string fieldName, string bufName) + public string Accept(TVector2 type, string varName, string fieldName, string bufName) { - return $"{{var _ok_ bool; var __json_text__ map[string]interface{{}}; if __json_text__, _ok_ = {bufName}[\"{fieldName}\"].(map[string]interface{{}}) ; !_ok_ {{ err = errors.New(\"{varName} error\"); return }}; {DeserializeString(type, "_", DText.KEY_NAME, "__json_text__")}; {DeserializeString(type, varName, DText.TEXT_NAME, "__json_text__")} }}"; + return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}[""{fieldName}""].(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} + var _x_, _y_ float32; + {TFloat.Ins.Apply(this, "_x_", "x", "_v_")} + {TFloat.Ins.Apply(this, "_y_", "y", "_v_")} + {varName} = math.NewVector2(_x_, _y_) + }} +"; + } + + public string Accept(TVector3 type, string varName, string fieldName, string bufName) + { + return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}[""{fieldName}""].(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} + var _x_, _y_, _z_ float32; + {TFloat.Ins.Apply(this, "_x_", "x", "_v_")} + {TFloat.Ins.Apply(this, "_y_", "y", "_v_")} + {TFloat.Ins.Apply(this, "_z_", "z", "_v_")} + {varName} = math.NewVector3(_x_, _y_, _z_) + }} +"; + } + + public string Accept(TVector4 type, string varName, string fieldName, string bufName) + { + return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}[""{fieldName}""].(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} + var _x_, _y_, _z_, _w_ float32; + {TFloat.Ins.Apply(this, "_x_", "x", "_v_")} + {TFloat.Ins.Apply(this, "_y_", "y", "_v_")} + {TFloat.Ins.Apply(this, "_z_", "z", "_v_")} + {TFloat.Ins.Apply(this, "_w_", "w", "_v_")} + {varName} = math.NewVector4(_x_, _y_, _z_, _w_) + }} +"; + } + + public string Accept(TDateTime type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); } public string Accept(TBean type, string varName, string fieldName, string bufName) { - return $"{{ var _ok_ bool; var _x_ map[string]interface{{}}; if _x_, _ok_ = {bufName}[\"{fieldName}\"].(map[string]interface{{}}); !_ok_ {{ err = errors.New(\"{fieldName} error\"); return }}; if {varName}, err = {($"New{ type.Bean.GoFullName}(_x_)")}; err != nil {{ return }} }}"; + return $"{{ var _ok_ bool; var _x_ map[string]interface{{}}; if _x_, _ok_ = {bufName}[\"{fieldName}\"].(map[string]interface{{}}); !_ok_ {{ err = errors.New(\"{fieldName} error\"); return }}; if {varName}, err = {($"Deserialize{ type.Bean.GoFullName}(_x_)")}; err != nil {{ return }} }}"; } @@ -149,46 +190,5 @@ namespace Luban.Job.Cfg.TypeVisitors }} }}"; } - - public string Accept(TVector2 type, string varName, string fieldName, string bufName) - { - return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}[""{fieldName}""].(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} - var _x_, _y_ float32; - {TFloat.Ins.Apply(this, "_x_", "x", "_v_")} - {TFloat.Ins.Apply(this, "_y_", "y", "_v_")} - {varName} = math.NewVector2(_x_, _y_) - }} -"; - } - - public string Accept(TVector3 type, string varName, string fieldName, string bufName) - { - return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}[""{fieldName}""].(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} - var _x_, _y_, _z_ float32; - {TFloat.Ins.Apply(this, "_x_", "x", "_v_")} - {TFloat.Ins.Apply(this, "_y_", "y", "_v_")} - {TFloat.Ins.Apply(this, "_z_", "z", "_v_")} - {varName} = math.NewVector3(_x_, _y_, _z_) - }} -"; - } - - public string Accept(TVector4 type, string varName, string fieldName, string bufName) - { - return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}[""{fieldName}""].(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} - var _x_, _y_, _z_, _w_ float32; - {TFloat.Ins.Apply(this, "_x_", "x", "_v_")} - {TFloat.Ins.Apply(this, "_y_", "y", "_v_")} - {TFloat.Ins.Apply(this, "_z_", "z", "_v_")} - {TFloat.Ins.Apply(this, "_w_", "w", "_v_")} - {varName} = math.NewVector4(_x_, _y_, _z_, _w_) - }} -"; - } - - public string Accept(TDateTime type, string varName, string fieldName, string bufName) - { - return DeserializeNumber(type, varName, fieldName, bufName); - } } } diff --git a/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs b/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs index aef6cd1..494c6b5 100644 --- a/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs @@ -59,16 +59,16 @@ namespace Luban.Job.Common.Defs } #if !LUBAN_LITE - public string GoBinImport + public virtual string GoBinImport { get { var imports = new HashSet(); - if (IsAbstractType) + if (IsAbstractType || this.HierarchyFields.Count > 0) { imports.Add("errors"); } - foreach (var f in Fields) + foreach (var f in HierarchyFields) { f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports); } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs index c02f549..82b7071 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeBinVisitor.cs @@ -11,7 +11,7 @@ namespace Luban.Job.Common.TypeVisitors { 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(GoSerializeUnderingVisitor.Ins, "__x__", bufName)}; {fieldName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; + 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, err)}; {fieldName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; } else { diff --git a/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs index 7c7b876..6f3cb7c 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs @@ -9,102 +9,102 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TBool type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadBool(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadBool(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TByte type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadByte(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadByte(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TShort type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadShort(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadShort(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TFshort type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadFshort(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadFshort(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TInt type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TFint type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadFint(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadFint(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TLong type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadLong(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadLong(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TFlong type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadFlong(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadFlong(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TFloat type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadFloat(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadFloat(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TDouble type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadDouble(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadDouble(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TEnum type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TString type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); 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 }} }}"; + return $"{{ if _, {err} = {bufName}.ReadString(); {err} != nil {{ return }}; if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TBytes type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadBytes(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadBytes(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TVector2 type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadVector2(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadVector2(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TVector3 type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadVector3(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadVector3(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TVector4 type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadVector4(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadVector4(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TDateTime type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); return }} }}"; } public string Accept(TBean type, string fieldName, string bufName, string err) { - return $"{{ if {fieldName}, {err} = {($"Deserialize{type.Bean.GoFullName}({bufName})")}; {err} != nil {{ return }} }}"; + return $"{{ if {fieldName}, {err} = {($"Deserialize{type.Bean.GoFullName}({bufName})")}; {err} != nil {{ {err} = errors.New(\"{fieldName} error\"); 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_) }} }}"; + return $@"{{{fieldName} = make([]{elementType.Apply(GoTypeNameVisitor.Ins)}, 0); var _n_ int; if _n_, {err} = {bufName}.ReadSize(); {err} != nil {{ {err} = errors.New(""{fieldName} error""); 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) @@ -124,7 +124,7 @@ namespace Luban.Job.Common.TypeVisitors 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_}} }}"; + return $@"{{ {fieldName} = make({type.Apply(GoTypeNameVisitor.Ins)}); var _n_ int; if _n_, {err} = {bufName}.ReadSize(); {err} != nil {{ {err} = errors.New(""{fieldName} error""); 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.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs index 6445dd6..78c5f19 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoSerializeUnderingVisitor.cs @@ -94,7 +94,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TDateTime type, string fieldName, string bufName) { - throw new System.NotSupportedException(); + return $"{bufName}.WriteInt({fieldName})"; } public string Accept(TBean type, string fieldName, string bufName) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs index 720aea4..26411e5 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/GoTypeUnderingNameVisitor.cs @@ -79,7 +79,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TBean type) { - return type.Bean.IsAbstractType ? $"serialization.ISerializable" : $"*{type.Bean.GoFullName}"; + return type.Bean.IsAbstractType ? $"interface{{}}" : $"*{type.Bean.GoFullName}"; } public string Accept(TArray type) diff --git a/src/Luban.Server/Templates/config/go_bin/bean.tpl b/src/Luban.Server/Templates/config/go_bin/bean.tpl index d9a4128..a3628fc 100644 --- a/src/Luban.Server/Templates/config/go_bin/bean.tpl +++ b/src/Luban.Server/Templates/config/go_bin/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_export_fields hierarchy_not_abstract_children = x.hierarchy_not_abstract_children ~}} @@ -15,59 +15,50 @@ 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) { + // not support +} + +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 Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (interface{}, error) { var id int32 + var err error if id, err = _buf.ReadInt() ; err != nil { - return + return nil, err } 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, errors.New("{{child.full_name}}") } else { return _v, nil } {{~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 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/config/go_json/bean.tpl b/src/Luban.Server/Templates/config/go_json/bean.tpl index 0a80cfb..b9b120d 100644 --- a/src/Luban.Server/Templates/config/go_json/bean.tpl +++ b/src/Luban.Server/Templates/config/go_json/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_export_fields hierarchy_not_abstract_children = x.hierarchy_not_abstract_children ~}} @@ -11,22 +11,26 @@ package {{x.top_module}} {{x.go_json_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}})Deserialize(_buf map[string]interface{}) (err error) { + {{~for field in hierarchy_fields ~}} + {{go_deserialize_json_field field.ctype ("_v." + field.convention_name) field.name '_buf'}} + {{~end~}} + return +} {{~if is_abstract_type~}} -func New{{go_full_name}}(_buf map[string]interface{}) (_v interface{}, err error) { +func Deserialize{{go_full_name}}(_buf map[string]interface{}) (interface{}, error) { var id string var _ok_ bool if id, _ok_ = _buf["__type__"].(string) ; !_ok_ { @@ -34,36 +38,18 @@ func New{{go_full_name}}(_buf map[string]interface{}) (_v interface{}, err error } switch id { {{~for child in hierarchy_not_abstract_children~}} - case "{{child.name}}": return New{{child.go_full_name}}(_buf); + case "{{child.name}}": _v := &{{child.go_full_name}}{}; if err := _v.Deserialize(_buf); err != nil { return nil, errors.New("{{child.full_name}}") } else { return _v, nil } {{~end~}} default: return nil, errors.New("unknown type id") } - return -} - -func New{{go_full_name}}_Body(_buf map[string]interface{}) (_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_json_field field.ctype ("_v." + field.convention_name) field.name '_buf'}} - {{~end~}} - return } {{~else~}} -func New{{go_full_name}}(_buf map[string]interface{}) (_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_json_field field.ctype ("_v." + field.convention_name) field.name '_buf'}} - {{~end~}} - return +func Deserialize{{go_full_name}}(_buf map[string]interface{}) (*{{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/bean.tpl b/src/Luban.Server/Templates/proto/go/bean.tpl index ce7c070..78994ab 100644 --- a/src/Luban.Server/Templates/proto/go/bean.tpl +++ b/src/Luban.Server/Templates/proto/go/bean.tpl @@ -22,17 +22,17 @@ type {{go_full_name}} struct { const TypeId_{{go_full_name}} = {{x.id}} -func ({{go_full_name}}) GetTypeId() int32 { +func (*{{go_full_name}}) GetTypeId() int32 { return {{x.id}} } -func (_v {{go_full_name}})Serialize(_buf *serialization.ByteBuf) { +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) { +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~}} @@ -40,9 +40,10 @@ func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) { } {{~if is_abstract_type~}} -func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { - _buf.WriteInt(_v.GetTypeId()) - _v.Serialize(_buf) +func Serialize{{go_full_name}}(_v interface{}, _buf *serialization.ByteBuf) { + _b := _v.(serialization.ISerializable) + _buf.WriteInt(_b.GetTypeId()) + _b.Serialize(_buf) } func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) { @@ -52,7 +53,7 @@ func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization. } switch id { {{~for child in hierarchy_not_abstract_children~}} - case {{child.id}}: _v = {{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return } + 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") }