【调整】调整 proto go和 cfg go的生成

main
walon 2021-11-29 16:24:02 +08:00
parent a9de6b924b
commit 813e129352
12 changed files with 175 additions and 181 deletions

View File

@ -113,17 +113,17 @@ namespace Luban.Common.Utils
public static string MakeGoFullName(string module, string name) public static string MakeGoFullName(string module, string name)
{ {
return MakeGoNamespace(module) + "_" + name; return MakeGoNamespace(module) + name;
} }
public static string MakePyFullName(string module, string 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) public static string MakeRustFullName(string module, string name)
{ {
return MakeGoNamespace(module) + "_" + name; return MakeGoNamespace(module) + name;
} }
public static string MakeNamespace(string module, string subModule) public static string MakeNamespace(string module, string subModule)

View File

@ -32,17 +32,33 @@ namespace Luban.Job.Cfg.Defs
} }
#if !LUBAN_LITE #if !LUBAN_LITE
public override string GoBinImport
{
get
{
var imports = new HashSet<string>();
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 public string GoJsonImport
{ {
get get
{ {
var imports = new HashSet<string>(); var imports = new HashSet<string>();
if (IsAbstractType) if (IsAbstractType || this.HierarchyExportFields.Count > 0)
{ {
imports.Add("errors"); imports.Add("errors");
} }
foreach (var f in Fields) foreach (var f in HierarchyExportFields)
{ {
f.CType.Apply(TypeVisitors.GoJsonImport.Ins, imports); f.CType.Apply(TypeVisitors.GoJsonImport.Ins, imports);
} }

View File

@ -79,39 +79,14 @@ namespace Luban.Job.Cfg.TypeVisitors
return DeserializeString(type, varName, bufName); 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) 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}\"]")} }}"; 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) public string Accept(TBytes 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)
{ {
//return $"{{ if {varName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}";
throw new System.NotSupportedException(); throw new System.NotSupportedException();
} }
@ -155,5 +130,30 @@ namespace Luban.Job.Cfg.TypeVisitors
{ {
return DeserializeNumber(type, varName, bufName); 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();
}
} }
} }

View File

@ -79,20 +79,61 @@ namespace Luban.Job.Cfg.TypeVisitors
return DeserializeString(type, varName, fieldName, bufName); 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) public string Accept(TBytes type, string varName, string fieldName, string bufName)
{ {
//return $"{{ if {varName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}"; //return $"{{ if {varName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}";
throw new System.NotSupportedException(); 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) 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);
}
} }
} }

View File

@ -59,16 +59,16 @@ namespace Luban.Job.Common.Defs
} }
#if !LUBAN_LITE #if !LUBAN_LITE
public string GoBinImport public virtual string GoBinImport
{ {
get get
{ {
var imports = new HashSet<string>(); var imports = new HashSet<string>();
if (IsAbstractType) if (IsAbstractType || this.HierarchyFields.Count > 0)
{ {
imports.Add("errors"); imports.Add("errors");
} }
foreach (var f in Fields) foreach (var f in HierarchyFields)
{ {
f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports); f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports);
} }

View File

@ -11,7 +11,7 @@ namespace Luban.Job.Common.TypeVisitors
{ {
if (type.IsNullable) 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 else
{ {

View File

@ -9,102 +9,102 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TBool type, string fieldName, string bufName, string err) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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_}} }}";
} }
} }
} }

View File

@ -94,7 +94,7 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TDateTime type, string fieldName, string bufName) 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) public string Accept(TBean type, string fieldName, string bufName)

View File

@ -79,7 +79,7 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TBean type) 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) public string Accept(TArray type)

View File

@ -2,7 +2,7 @@
go_full_name = x.go_full_name go_full_name = x.go_full_name
parent_def_type = x.parent_def_type parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_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 hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
~}} ~}}
@ -15,59 +15,50 @@ import (
{{x.go_bin_import}} {{x.go_bin_import}}
type {{go_full_name}} struct { type {{go_full_name}} struct {
{{~if parent_def_type~}} {{~for field in hierarchy_fields ~}}
{{parent_def_type.go_full_name}}
{{~end~}}
{{~for field in export_fields ~}}
{{field.convention_name}} {{go_define_type field.ctype}} {{field.convention_name}} {{go_define_type field.ctype}}
{{~end~}} {{~end~}}
} }
{{~if !is_abstract_type~}} const TypeId_{{go_full_name}} = {{x.id}}
func ({{go_full_name}}) GetTypeId() int {
func (*{{go_full_name}}) GetTypeId() int32 {
return {{x.id}} 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~}} {{~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 id int32
var err error
if id, err = _buf.ReadInt() ; err != nil { if id, err = _buf.ReadInt() ; err != nil {
return return nil, err
} }
switch id { switch id {
{{~for child in hierarchy_not_abstract_children~}} {{~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~}} {{~end~}}
default: return nil, errors.New("unknown type id") 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~}} {{~else~}}
func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) { func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) {
_v = &{{go_full_name}}{} v := &{{go_full_name}}{}
{{~if parent_def_type~}} if err := v.Deserialize(_buf); err == nil {
var _p *{{parent_def_type.go_full_name}} return v, nil
if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return } } else {
_v.{{parent_def_type.go_full_name}} = *_p return nil, err
{{~end~}} }
{{~for field in export_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
return
} }
{{~end~}} {{~end~}}

View File

@ -2,7 +2,7 @@
go_full_name = x.go_full_name go_full_name = x.go_full_name
parent_def_type = x.parent_def_type parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_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 hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
~}} ~}}
@ -11,22 +11,26 @@ package {{x.top_module}}
{{x.go_json_import}} {{x.go_json_import}}
type {{go_full_name}} struct { type {{go_full_name}} struct {
{{~if parent_def_type~}} {{~for field in hierarchy_fields ~}}
{{parent_def_type.go_full_name}}
{{~end~}}
{{~for field in export_fields ~}}
{{field.convention_name}} {{go_define_type field.ctype}} {{field.convention_name}} {{go_define_type field.ctype}}
{{~end~}} {{~end~}}
} }
{{~if !is_abstract_type~}} const TypeId_{{go_full_name}} = {{x.id}}
func ({{go_full_name}}) GetTypeId() int {
func (*{{go_full_name}}) GetTypeId() int32 {
return {{x.id}} 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~}} {{~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 id string
var _ok_ bool var _ok_ bool
if id, _ok_ = _buf["__type__"].(string) ; !_ok_ { 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 { switch id {
{{~for child in hierarchy_not_abstract_children~}} {{~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~}} {{~end~}}
default: return nil, errors.New("unknown type id") 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~}} {{~else~}}
func New{{go_full_name}}(_buf map[string]interface{}) (_v *{{go_full_name}}, err error) { func Deserialize{{go_full_name}}(_buf map[string]interface{}) (*{{go_full_name}}, error) {
_v = &{{go_full_name}}{} v := &{{go_full_name}}{}
{{~if parent_def_type~}} if err := v.Deserialize(_buf); err == nil {
var _p *{{parent_def_type.go_full_name}} return v, nil
if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return } } else {
_v.{{parent_def_type.go_full_name}} = *_p return nil, err
{{~end~}} }
{{~for field in export_fields ~}}
{{go_deserialize_json_field field.ctype ("_v." + field.convention_name) field.name '_buf'}}
{{~end~}}
return
} }
{{~end~}} {{~end~}}

View File

@ -22,17 +22,17 @@ type {{go_full_name}} struct {
const TypeId_{{go_full_name}} = {{x.id}} const TypeId_{{go_full_name}} = {{x.id}}
func ({{go_full_name}}) GetTypeId() int32 { func (*{{go_full_name}}) GetTypeId() int32 {
return {{x.id}} 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 ~}} {{~for field in hierarchy_fields ~}}
{{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}} {{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}} {{~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 ~}} {{~for field in hierarchy_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}} {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}}
{{~end~}} {{~end~}}
@ -40,9 +40,10 @@ func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) {
} }
{{~if is_abstract_type~}} {{~if is_abstract_type~}}
func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { func Serialize{{go_full_name}}(_v interface{}, _buf *serialization.ByteBuf) {
_buf.WriteInt(_v.GetTypeId()) _b := _v.(serialization.ISerializable)
_v.Serialize(_buf) _buf.WriteInt(_b.GetTypeId())
_b.Serialize(_buf)
} }
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) { 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 { switch id {
{{~for child in hierarchy_not_abstract_children~}} {{~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~}} {{~end~}}
default: return nil, errors.New("unknown type id") default: return nil, errors.New("unknown type id")
} }