From 9949d2ddce5e5bf78a13df71c99a028da8b2c74b Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 8 Jul 2021 16:03:31 +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=9Ego=E8=AF=AD=E8=A8=80json=E6=95=B0=E6=8D=AE=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E6=94=AF=E6=8C=81(code=5Fgo=5Fjson)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Job.Cfg/Source/Defs/DefBean.cs | 4 + .../Source/Defs/TTypeTemplateExtends.cs | 9 +- .../Source/Generate/GoJsonCodeRender.cs | 13 +- .../Source/TypeVisitors/CollectGoImport.cs | 106 ++-------- .../TypeVisitors/GoDeserializeJson2Visitor.cs | 22 ++ .../GoDeserializeJsonUndering2Visitor.cs | 158 ++++++++++++++ .../GoDeserializeJsonUnderingVisitor.cs | 193 ++++++++++++++++++ .../TypeVisitors/GoDeserializeJsonVisitor.cs | 23 +++ ...tor.cs => GoDeserializeUnderingVisitor.cs} | 4 +- .../TypeVisitors/GoDeserializeVisitor.cs | 4 +- 10 files changed, 434 insertions(+), 102 deletions(-) create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs rename src/Luban.Job.Cfg/Source/TypeVisitors/{GoUnderingDeserializeVisitor.cs => GoDeserializeUnderingVisitor.cs} (97%) diff --git a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs index 8413894..311dd7a 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs @@ -36,6 +36,10 @@ namespace Luban.Job.Cfg.Defs get { var imports = new HashSet(); + if (IsAbstractType) + { + imports.Add("errors"); + } foreach (var f in Fields) { f.CType.Apply(CollectGoImport.Ins, imports); diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index 4b18ee5..e89e623 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -88,14 +88,19 @@ namespace Luban.Job.Cfg.Defs return type.Apply(GoDeserializeVisitor.Ins, name, bufName); } + public static string GoDeserializeJsonField(TType type, string name, string fieldName, string bufName) + { + return type.Apply(GoDeserializeJsonVisitor.Ins, name, fieldName, bufName); + } + public static string TsJsonConstructor(string fieldName, string jsonFieldName, TType type) { - return type.Apply(TypescriptJsonConstructorVisitor.Ins, $"{jsonFieldName}", fieldName); + return type.Apply(TypescriptJsonConstructorVisitor.Ins, jsonFieldName, fieldName); } public static string TsBinConstructor(string fieldName, string byteBufName, TType type) { - return type.Apply(TypescriptBinConstructorVisitor.Ins, $"{byteBufName}", fieldName); + return type.Apply(TypescriptBinConstructorVisitor.Ins, byteBufName, fieldName); } public static string TsRecursiveResolve(DefField field, string tables) diff --git a/src/Luban.Job.Cfg/Source/Generate/GoJsonCodeRender.cs b/src/Luban.Job.Cfg/Source/Generate/GoJsonCodeRender.cs index 5f2335a..9a97140 100644 --- a/src/Luban.Job.Cfg/Source/Generate/GoJsonCodeRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/GoJsonCodeRender.cs @@ -50,19 +50,20 @@ func New{{go_full_name}}(_buf map[string]interface{}) (_v *{{go_full_name}}, err _v.{{parent_def_type.go_full_name}} = *_p {{~end~}} {{~for field in export_fields ~}} - {{go_deserialize_field field '_buf'}} + {{go_deserialize_json_field field.ctype (""_v."" + field.go_style_name) field.name '_buf'}} {{~end~}} return } {{~if is_abstract_type~}} func NewChild{{go_full_name}}(_buf map[string]interface{}) (_v interface{}, err error) { - var id int32 - if id, err = _buf.ReadInt() ; err != nil { - return + var id string + var _ok_ bool + if id, _ok_ = _buf[""__type__""].(string) ; !_ok_ { + return nil, errors.New(""type id missing"") } switch id { {{~for child in hierarchy_not_abstract_children~}} - case {{child.id}}: return New{{child.go_full_name}}(_buf); + case ""{{child.name}}"": return New{{child.go_full_name}}(_buf); {{~end~}} default: return nil, errors.New(""unknown type id"") } @@ -150,7 +151,7 @@ func New{{go_full_name}}(_buf []map[string]interface{}) (*{{go_full_name}}, erro if len(_buf) != 1 { return nil, errors.New("" size != 1 "") } else { - if _v, err2 := {{go_deserialize_type value_type '_buf'}}; err2 != nil { + if _v, err2 := {{go_deserialize_type value_type '_buf[0]'}}; err2 != nil { return nil, err2 } else { return &{{go_full_name}}{_data:_v}, nil diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CollectGoImport.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CollectGoImport.cs index 89bdb89..59cc7e2 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CollectGoImport.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CollectGoImport.cs @@ -4,126 +4,52 @@ using System.Collections.Generic; namespace Luban.Job.Cfg.TypeVisitors { - class CollectGoImport : ITypeActionVisitor> + class CollectGoImport : DecoratorActionVisitor> { public static CollectGoImport Ins { get; } = new CollectGoImport(); - public void Accept(TBool type, HashSet x) + public override void DoAccept(TType type, HashSet x) { - + x.Add("errors"); } - public void Accept(TByte type, HashSet x) - { - - } - - public void Accept(TShort type, HashSet x) - { - - } - - public void Accept(TFshort type, HashSet x) - { - - } - - public void Accept(TInt type, HashSet x) - { - - } - - public void Accept(TFint type, HashSet x) - { - - } - - public void Accept(TLong type, HashSet x) - { - - } - - public void Accept(TFlong type, HashSet x) - { - - } - - public void Accept(TFloat type, HashSet x) - { - - } - - public void Accept(TDouble type, HashSet x) - { - - } - - public void Accept(TEnum type, HashSet x) - { - - } - - public void Accept(TString type, HashSet x) - { - - } - - public void Accept(TBytes type, HashSet x) - { - - } - - public void Accept(TText type, HashSet x) - { - - } - - public void Accept(TBean type, HashSet x) - { - - } - - public void Accept(TArray type, HashSet x) + public override void Accept(TArray type, HashSet x) { type.ElementType.Apply(this, x); } - public void Accept(TList type, HashSet x) - { - - type.ElementType.Apply(this, x); - } - - public void Accept(TSet type, HashSet x) + public override void Accept(TList type, HashSet x) { type.ElementType.Apply(this, x); } - public void Accept(TMap type, HashSet x) + public override void Accept(TSet type, HashSet x) { + type.ElementType.Apply(this, x); + } + public override void Accept(TMap type, HashSet x) + { type.KeyType.Apply(this, x); type.ValueType.Apply(this, x); } - public void Accept(TVector2 type, HashSet x) + public override void Accept(TVector2 type, HashSet x) { + x.Add("errors"); x.Add("bright/math"); } - public void Accept(TVector3 type, HashSet x) + public override void Accept(TVector3 type, HashSet x) { + x.Add("errors"); x.Add("bright/math"); } - public void Accept(TVector4 type, HashSet x) + public override void Accept(TVector4 type, HashSet x) { + x.Add("errors"); x.Add("bright/math"); } - - public void Accept(TDateTime type, HashSet x) - { - - } } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs new file mode 100644 index 0000000..b14f052 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJson2Visitor.cs @@ -0,0 +1,22 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class GoDeserializeJson2Visitor : DecoratorFuncVisitor + { + public static GoDeserializeJson2Visitor Ins { get; } = new(); + + public override string DoAccept(TType type, string varName, string bufName) + { + 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__ }}}}"; + } + else + { + return type.Apply(GoDeserializeJsonUndering2Visitor.Ins, varName, bufName); + } + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs new file mode 100644 index 0000000..3720c8d --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUndering2Visitor.cs @@ -0,0 +1,158 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class GoDeserializeJsonUndering2Visitor : ITypeFuncVisitor + { + public static GoDeserializeJsonUndering2Visitor Ins { get; } = new(); + + public string Accept(TBool type, string varName, string bufName) + { + return $"{{ var _ok_ bool; if {varName}, _ok_ = {bufName}.(bool); !_ok_ {{ err = errors.New(\"{varName} error\"); return }} }}"; + } + + private string DeserializeNumber(TType type, string varName, string bufName) + { + return $"{{ var _ok_ bool; var _x_ float64; if _x_, _ok_ = {bufName}.(float64); !_ok_ {{ err = errors.New(\"{varName} error\"); return }}; {varName} = {type.Apply(GoTypeUnderingNameVisitor.Ins)}(_x_) }}"; + } + + public string Accept(TByte type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TShort type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TFshort type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TInt type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TFint type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TLong type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TFlong type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TFloat type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TDouble type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + public string Accept(TEnum type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + + + private string DeserializeString(TType type, string varName, string bufName) + { + return $"{{ if {varName}, _ok_ = {bufName}.(string); !_ok_ {{ err = errors.New(\"{varName} error\"); return }} }}"; + } + + public string Accept(TString type, string varName, string 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) + { + return DeserializeString(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 = {(type.Bean.IsAbstractType ? $"NewChild{type.Bean.GoFullName}(_x_)" : $"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) + { + throw new System.NotSupportedException(); + } + + public string Accept(TVector2 type, string varName, string bufName) + { + return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}.(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{varName} error""); return }} + var _x_, _y_ float32; + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_x_", "x", "_v_")} + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_y_", "y", "_v_")} + {varName} = math.NewVector2(_x_, _y_) + }} +"; + } + + public string Accept(TVector3 type, string varName, string bufName) + { + return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}.(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{varName} error""); return }} + var _x_, _y_, _z_ float32; + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_x_", "x", "_v_")} + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_y_", "y", "_v_")} + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_z_", "z", "_v_")} + {varName} = math.NewVector3(_x_, _y_, _z_) + }} +"; + } + + public string Accept(TVector4 type, string varName, string bufName) + { + return $@"{{ var _ok_ bool; var _v_ map[string]interface{{}}; if _v_, _ok_ = {bufName}.(map[string]interface{{}}); !_ok_ {{ err = errors.New(""{varName} error""); return }} + var _x_, _y_, _z_, _w_ float32; + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_x_", "x", "_v_")} + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_y_", "y", "_v_")} + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_z_", "z", "_v_")} + {TFloat.Ins.Apply(GoDeserializeJsonUnderingVisitor.Ins, "_w_", "w", "_v_")} + {varName} = math.NewVector4(_x_, _y_, _z_, _w_) + }} +"; + } + + public string Accept(TDateTime type, string varName, string bufName) + { + return DeserializeNumber(type, varName, bufName); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs new file mode 100644 index 0000000..50a8d44 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonUnderingVisitor.cs @@ -0,0 +1,193 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class GoDeserializeJsonUnderingVisitor : ITypeFuncVisitor + { + public static GoDeserializeJsonUnderingVisitor Ins { get; } = new(); + + public string Accept(TBool type, string varName, string fieldName, string bufName) + { + return $"{{ var _ok_ bool; if {varName}, _ok_ = {bufName}[\"{fieldName}\"].(bool); !_ok_ {{ err = errors.New(\"{fieldName} error\"); return }} }}"; + } + + private string DeserializeNumber(TType type, string varName, string fieldName, string bufName) + { + return $"{{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = {bufName}[\"{fieldName}\"].(float64); !_ok_ {{ err = errors.New(\"{fieldName} error\"); return }}; {varName} = {type.Apply(GoTypeUnderingNameVisitor.Ins)}(_tempNum_) }}"; + } + + public string Accept(TByte type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TShort type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TFshort type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TInt type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TFint type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TLong type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TFlong type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TFloat type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TDouble type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + public string Accept(TEnum type, string varName, string fieldName, string bufName) + { + return DeserializeNumber(type, varName, fieldName, bufName); + } + + + private string DeserializeString(TType type, string varName, string fieldName, string bufName) + { + return $"{{ var _ok_ bool; if {varName}, _ok_ = {bufName}[\"{fieldName}\"].(string); !_ok_ {{ err = errors.New(\"{fieldName} error\"); return }} }}"; + } + + public string Accept(TString type, string varName, string fieldName, string bufName) + { + return DeserializeString(type, varName, fieldName, bufName); + } + + 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) + { + return DeserializeString(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 = {(type.Bean.IsAbstractType ? $"NewChild{type.Bean.GoFullName}(_x_)" : $"New{ type.Bean.GoFullName} (_x_)")}; err != nil {{ return }} }}"; + } + + + private string GenList(TType elementType, string varName, string fieldName, string bufName) + { + return $@" {{ + var _arr_ []interface{{}} + var _ok_ bool + if _arr_, _ok_ = {bufName}[""{ fieldName}""].([]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} + + { varName} = make([]{elementType.Apply(GoTypeNameVisitor.Ins)}, 0, len(_arr_)) + + for _, _e_ := range _arr_ {{ + var _list_v_ {elementType.Apply(GoTypeNameVisitor.Ins)} + {elementType.Apply(GoDeserializeJson2Visitor.Ins, "_list_v_", "_e_")} + {varName} = append({varName}, _list_v_) + }} + }} +"; + } + + public string Accept(TArray type, string varName, string fieldName, string bufName) + { + return GenList(type.ElementType, varName, fieldName, bufName); + } + + public string Accept(TList type, string varName, string fieldName, string bufName) + { + return GenList(type.ElementType, varName, fieldName, bufName); + } + + public string Accept(TSet type, string varName, string fieldName, string bufName) + { + return GenList(type.ElementType, varName, fieldName, bufName); + } + + public string Accept(TMap type, string varName, string fieldName, string bufName) + { + return $@"{{ + var _arr_ []interface{{}} + var _ok_ bool + if _arr_, _ok_ = {bufName}[""{ fieldName}""].([]interface{{}}); !_ok_ {{ err = errors.New(""{fieldName} error""); return }} + + {varName} = make({type.Apply(GoTypeNameVisitor.Ins)}) + + for _, _e_ := range _arr_ {{ + var _kv_ []interface{{}} + if _kv_, _ok_ = _e_.([]interface{{}}); !_ok_ || len(_kv_) != 2 {{ err = errors.New(""{fieldName} error""); return }} + var _key_ {type.KeyType.Apply(GoTypeNameVisitor.Ins)} + {type.KeyType.Apply(GoDeserializeJson2Visitor.Ins, "_key_", "_kv_[0]")} + var _value_ {type.ValueType.Apply(GoTypeNameVisitor.Ins)} + {type.ValueType.Apply(GoDeserializeJson2Visitor.Ins, "_value_", "_kv_[1]")} + {varName}[_key_] = _value_ + }} + }}"; + } + + 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.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs new file mode 100644 index 0000000..3f8b3fa --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeJsonVisitor.cs @@ -0,0 +1,23 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class GoDeserializeJsonVisitor : DecoratorFuncVisitor + { + public static GoDeserializeJsonVisitor Ins { get; } = new GoDeserializeJsonVisitor(); + + public override string DoAccept(TType type, string varName, string fieldName, string bufName) + { + 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__ }}}}"; + } + else + { + return type.Apply(GoDeserializeJsonUnderingVisitor.Ins, varName, fieldName, bufName); + } + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoUnderingDeserializeVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs similarity index 97% rename from src/Luban.Job.Cfg/Source/TypeVisitors/GoUnderingDeserializeVisitor.cs rename to src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs index 94e19ba..7583a14 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeUnderingVisitor.cs @@ -3,9 +3,9 @@ using Luban.Job.Common.TypeVisitors; namespace Luban.Job.Cfg.TypeVisitors { - class GoUnderingDeserializeVisitor : ITypeFuncVisitor + class GoDeserializeUnderingVisitor : ITypeFuncVisitor { - public static GoUnderingDeserializeVisitor Ins { get; } = new GoUnderingDeserializeVisitor(); + public static GoDeserializeUnderingVisitor Ins { get; } = new GoDeserializeUnderingVisitor(); public string Accept(TBool type, string fieldName, string bufName) { diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeVisitor.cs index bfd6d68..ebe92e2 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/GoDeserializeVisitor.cs @@ -11,11 +11,11 @@ namespace Luban.Job.Cfg.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(GoUnderingDeserializeVisitor.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(GoDeserializeUnderingVisitor.Ins, "__x__", bufName)}; {fieldName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}"; } else { - return type.Apply(GoUnderingDeserializeVisitor.Ins, (string)fieldName, bufName); + return type.Apply(GoDeserializeUnderingVisitor.Ins, (string)fieldName, bufName); } } }