【特性】新增go语言json数据加载支持(code_go_json)
parent
1aeeca2e08
commit
9949d2ddce
|
|
@ -36,6 +36,10 @@ namespace Luban.Job.Cfg.Defs
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var imports = new HashSet<string>();
|
var imports = new HashSet<string>();
|
||||||
|
if (IsAbstractType)
|
||||||
|
{
|
||||||
|
imports.Add("errors");
|
||||||
|
}
|
||||||
foreach (var f in Fields)
|
foreach (var f in Fields)
|
||||||
{
|
{
|
||||||
f.CType.Apply(CollectGoImport.Ins, imports);
|
f.CType.Apply(CollectGoImport.Ins, imports);
|
||||||
|
|
|
||||||
|
|
@ -88,14 +88,19 @@ namespace Luban.Job.Cfg.Defs
|
||||||
return type.Apply(GoDeserializeVisitor.Ins, name, bufName);
|
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)
|
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)
|
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)
|
public static string TsRecursiveResolve(DefField field, string tables)
|
||||||
|
|
|
||||||
|
|
@ -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
|
_v.{{parent_def_type.go_full_name}} = *_p
|
||||||
{{~end~}}
|
{{~end~}}
|
||||||
{{~for field in export_fields ~}}
|
{{~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~}}
|
{{~end~}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
{{~if is_abstract_type~}}
|
{{~if is_abstract_type~}}
|
||||||
func NewChild{{go_full_name}}(_buf map[string]interface{}) (_v interface{}, err error) {
|
func NewChild{{go_full_name}}(_buf map[string]interface{}) (_v interface{}, err error) {
|
||||||
var id int32
|
var id string
|
||||||
if id, err = _buf.ReadInt() ; err != nil {
|
var _ok_ bool
|
||||||
return
|
if id, _ok_ = _buf[""__type__""].(string) ; !_ok_ {
|
||||||
|
return nil, errors.New(""type id missing"")
|
||||||
}
|
}
|
||||||
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.name}}"": return New{{child.go_full_name}}(_buf);
|
||||||
{{~end~}}
|
{{~end~}}
|
||||||
default: return nil, errors.New(""unknown type id"")
|
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 {
|
if len(_buf) != 1 {
|
||||||
return nil, errors.New("" size != 1 "")
|
return nil, errors.New("" size != 1 "")
|
||||||
} else {
|
} 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
|
return nil, err2
|
||||||
} else {
|
} else {
|
||||||
return &{{go_full_name}}{_data:_v}, nil
|
return &{{go_full_name}}{_data:_v}, nil
|
||||||
|
|
|
||||||
|
|
@ -4,126 +4,52 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Luban.Job.Cfg.TypeVisitors
|
namespace Luban.Job.Cfg.TypeVisitors
|
||||||
{
|
{
|
||||||
class CollectGoImport : ITypeActionVisitor<HashSet<string>>
|
class CollectGoImport : DecoratorActionVisitor<HashSet<string>>
|
||||||
{
|
{
|
||||||
public static CollectGoImport Ins { get; } = new CollectGoImport();
|
public static CollectGoImport Ins { get; } = new CollectGoImport();
|
||||||
|
|
||||||
public void Accept(TBool type, HashSet<string> x)
|
public override void DoAccept(TType type, HashSet<string> x)
|
||||||
{
|
{
|
||||||
|
x.Add("errors");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TByte type, HashSet<string> x)
|
public override void Accept(TArray type, HashSet<string> x)
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TShort type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TFshort type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TInt type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TFint type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TLong type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TFlong type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TFloat type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TDouble type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TEnum type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TString type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TBytes type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TText type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TBean type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TArray type, HashSet<string> x)
|
|
||||||
{
|
{
|
||||||
type.ElementType.Apply(this, x);
|
type.ElementType.Apply(this, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TList type, HashSet<string> x)
|
public override void Accept(TList type, HashSet<string> x)
|
||||||
{
|
|
||||||
|
|
||||||
type.ElementType.Apply(this, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Accept(TSet type, HashSet<string> x)
|
|
||||||
{
|
{
|
||||||
type.ElementType.Apply(this, x);
|
type.ElementType.Apply(this, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TMap type, HashSet<string> x)
|
public override void Accept(TSet type, HashSet<string> x)
|
||||||
{
|
{
|
||||||
|
type.ElementType.Apply(this, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Accept(TMap type, HashSet<string> x)
|
||||||
|
{
|
||||||
type.KeyType.Apply(this, x);
|
type.KeyType.Apply(this, x);
|
||||||
type.ValueType.Apply(this, x);
|
type.ValueType.Apply(this, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TVector2 type, HashSet<string> x)
|
public override void Accept(TVector2 type, HashSet<string> x)
|
||||||
{
|
{
|
||||||
|
x.Add("errors");
|
||||||
x.Add("bright/math");
|
x.Add("bright/math");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TVector3 type, HashSet<string> x)
|
public override void Accept(TVector3 type, HashSet<string> x)
|
||||||
{
|
{
|
||||||
|
x.Add("errors");
|
||||||
x.Add("bright/math");
|
x.Add("bright/math");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TVector4 type, HashSet<string> x)
|
public override void Accept(TVector4 type, HashSet<string> x)
|
||||||
{
|
{
|
||||||
|
x.Add("errors");
|
||||||
x.Add("bright/math");
|
x.Add("bright/math");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Accept(TDateTime type, HashSet<string> x)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
using Luban.Job.Common.Types;
|
||||||
|
using Luban.Job.Common.TypeVisitors;
|
||||||
|
|
||||||
|
namespace Luban.Job.Cfg.TypeVisitors
|
||||||
|
{
|
||||||
|
class GoDeserializeJson2Visitor : DecoratorFuncVisitor<string, string, string>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,158 @@
|
||||||
|
using Luban.Job.Common.Types;
|
||||||
|
using Luban.Job.Common.TypeVisitors;
|
||||||
|
|
||||||
|
namespace Luban.Job.Cfg.TypeVisitors
|
||||||
|
{
|
||||||
|
class GoDeserializeJsonUndering2Visitor : ITypeFuncVisitor<string, string, string>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,193 @@
|
||||||
|
using Luban.Job.Common.Types;
|
||||||
|
using Luban.Job.Common.TypeVisitors;
|
||||||
|
|
||||||
|
namespace Luban.Job.Cfg.TypeVisitors
|
||||||
|
{
|
||||||
|
class GoDeserializeJsonUnderingVisitor : ITypeFuncVisitor<string, string, string, string>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
using Luban.Job.Common.Types;
|
||||||
|
using Luban.Job.Common.TypeVisitors;
|
||||||
|
|
||||||
|
namespace Luban.Job.Cfg.TypeVisitors
|
||||||
|
{
|
||||||
|
class GoDeserializeJsonVisitor : DecoratorFuncVisitor<string, string, string, string>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,9 +3,9 @@ using Luban.Job.Common.TypeVisitors;
|
||||||
|
|
||||||
namespace Luban.Job.Cfg.TypeVisitors
|
namespace Luban.Job.Cfg.TypeVisitors
|
||||||
{
|
{
|
||||||
class GoUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
|
class GoDeserializeUnderingVisitor : ITypeFuncVisitor<string, string, string>
|
||||||
{
|
{
|
||||||
public static GoUnderingDeserializeVisitor Ins { get; } = new GoUnderingDeserializeVisitor();
|
public static GoDeserializeUnderingVisitor Ins { get; } = new GoDeserializeUnderingVisitor();
|
||||||
|
|
||||||
public string Accept(TBool type, string fieldName, string bufName)
|
public string Accept(TBool type, string fieldName, string bufName)
|
||||||
{
|
{
|
||||||
|
|
@ -11,11 +11,11 @@ namespace Luban.Job.Cfg.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(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
|
else
|
||||||
{
|
{
|
||||||
return type.Apply(GoUnderingDeserializeVisitor.Ins, (string)fieldName, bufName);
|
return type.Apply(GoDeserializeUnderingVisitor.Ins, (string)fieldName, bufName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue